is there a way to query how many participants are ...
# ask-ai
i
is there a way to query how many participants are currently active during a call?
t
Thanks for reaching out, Ed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you find out how many active participants are in a LiveKit call! You can easily get this information using the Analytics API. There's a handy field called
numActiveParticipants
in the session data that shows the real-time number of active participants. To access this, you just need to call the
listLiveKitSessions()
endpoint. When you do, the response will include details about each session, like this:
Copy code
{
  "sessions": [
    {
      "sessionId": "...",
      "roomName": "...",
      "numActiveParticipants": 5,
      ...
    }
  ]
}
If you’d like, I’m here to guide you through the implementation or answer any other questions you have. Just let me know! Sources: Subscribing to tracks | LiveKit docs | Analytics API | LiveKit docs | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi
i
in python
t
I'd love to help you query the number of active participants in a LiveKit call using Python! You can do this easily with the Analytics API. Here’s a friendly step-by-step guide to get you started: 1. Generate an Access Token: First, use the LiveKit Python SDK to create a token with the
roomList
grant. Here’s how you can do it:
Copy code
from livekit import AccessToken

api_key = "your_api_key"
api_secret = "your_api_secret"

token = AccessToken(api_key, api_secret)
token.add_grant(room_list=True)
access_token = token.to_jwt()
1. Query the Analytics API: Next, use the token you just generated to make an HTTP request to the
listLiveKitSessions
endpoint. Check out this example:
Copy code
import requests

def get_sessions_with_active_participants(project_id, access_token):
    endpoint = f"https://cloud-api.livekit.io/api/project/{project_id}/sessions"
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json'
    }
    response = requests.get(endpoint, headers=headers)
    response.raise_for_status()
    return response.json()['sessions']

# Example usage
project_id = "p_your_project_id"
sessions = get_sessions_with_active_participants(project_id, access_token)

for session in sessions:
    print(f"Room: {session['roomName']}, Active Participants: {session['numActiveParticipants']}")
Just remember to swap out
your_api_key
,
your_api_secret
, and
p_your_project_id
with your actual credentials and project ID. If you hit any bumps or have more questions, I’m here to help—just let me know! Sources: Analytics API | LiveKit docs