<@U084APK40NS> Is there an api to track the consum...
# ask-ai
b
@millions-hair-24956 Is there an api to track the consumption and pricing ?
g
Currently, there is no specific API in LiveKit to track consumption and pricing directly. However, LiveKit provides an Analytics API that can be used to get information about sessions and participants, which might help in estimating usage. This API requires a LiveKit token with the
roomList
grant and is available to LiveKit Cloud customers with a Scale plan or higher. For pricing details, LiveKit's billing is based on resources consumed, such as outbound transfer and connection minutes, as detailed in the Billing documentation. If you need more specific information on tracking consumption or pricing, please let me know. Sources: - Analytics API - Billing 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
b
@millions-hair-24956 How can i determine the pricing from the Analytics API ? can you provide a function that does that
g
It might be possible to estimate pricing using the Analytics API by retrieving session details and calculating based on the resources consumed, such as bandwidth and connection minutes. However, the Analytics API itself does not directly provide pricing information. You would need to apply the pricing model from the Billing documentation to the data retrieved from the API. Here's a basic example of how you might start to implement such a function in JavaScript:
Copy code
async function calculatePricing(projectId, token) {
  const endpoint = `https://cloud-api.livekit.io/api/project/${projectId}/sessions/`;
  try {
    const response = await fetch(endpoint, {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${token}`,
        'Content-Type': 'application/json',
      },
    });
    if (!response.ok) throw new Error('Network response was not ok');
    const data = await response.json();

    let totalCost = 0;
    data.sessions.forEach(session => {
      const bandwidthCost = (session.bandwidthIn + session.bandwidthOut) * 0.01; // Example rate
      const connectionCost = session.numParticipants * session.duration * 0.02; // Example rate
      totalCost += bandwidthCost + connectionCost;
    });

    console.log(`Total estimated cost: $${totalCost.toFixed(2)}`);
  } catch (error) {
    console.log('There was a problem:', error.message);
  }
}

calculatePricing('your_project_id', 'your_access_token');
This function fetches session data and calculates a hypothetical cost based on example rates. You would need to replace these rates with actual values from your billing agreement. Would you like more details on this? Sources: - Billing documentation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.