<#C088ZNU7QQ5|ask-ai> how to pull duration of call...
# ask-ai
g
#ask-ai how to pull duration of call from livekit using analytics api
t
Thanks for reaching out, Vaishak. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to guide you through pulling the duration of a call from LiveKit using the Analytics API. Here’s a simple step-by-step approach: 1. Generate an Access Token: First, create a token with the
roomList
grant using your LiveKit API key and secret. 2. List Sessions: Next, query the Analytics API endpoint
https://cloud-api.livekit.io/api/project/{project_id}/sessions
using your token. You can use query parameters like
start
,
end
,
limit
, and
page
to narrow down the results. 3. Calculate Duration: From the response, look for
createdAt
(which shows when the session started) and
endedAt
(when it ended). Subtracting
createdAt
from
endedAt
will give you the call duration. Here’s an example snippet from the response:
Copy code
{
  "sessions": [
    {
      "sessionId": "string",
      "roomName": "string",
      "createdAt": "2025-09-29T13:59:40Z",
      "endedAt": "2025-09-29T14:59:40Z",
      "numParticipants": 2
    }
  ]
}
This approach lets you easily retrieve and calculate call durations programmatically. If you have any questions or need a hand with any step, please feel free to ask—I’m here to help! Sources: Analytics API | LiveKit docs
g
#ask-ai is there any other way to get the duration
t
Yes, there are alternative methods to retrieve call duration in LiveKit: 1. Using Webhooks: Listen for the
ROOM_ENDED
event and calculate the duration by comparing its
timestamp
with the session's creation time. 2. Using SIP API: For SIP-based calls, you can use the
max_call_duration
parameter to enforce or monitor call durations. Additionally, SIP-specific events like
SIP_CALL_STARTED
and
SIP_CALL_ENDED
can be used to compute durations. 3. Using Observability Data Hooks: Subscribe to session metrics and use timestamps from the first and last events to infer the session duration. Let me know if you'd like detailed steps or examples for any of these methods! Sources: Analytics API | LiveKit docs | livekit-protocol/livekit/protocol/analytics.pyi | livekit/livekit_analytics.pb.go | SIP APIs | LiveKit docs | Observability data hooks | LiveKit docs