LiveKit Server API

1.1.0 · active · verified Fri Apr 10

livekit-api provides Python bindings for interacting with the LiveKit server API, enabling developers to manage rooms, participants, and access tokens for real-time video/audio applications. It's part of the broader LiveKit Python SDKs. The current version is 1.1.0, with frequent updates across the LiveKit SDK ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a LiveKit room and generate an access token for a participant using `livekit-api`. It uses environment variables for LiveKit host and credentials for security. The operations are asynchronous and should be run within an asyncio event loop.

import os
import asyncio
from livekit import api

host = os.environ.get("LIVEKIT_HOST", "http://localhost:7880")
api_key = os.environ.get("LIVEKIT_API_KEY", "devkey")
api_secret = os.environ.get("LIVEKIT_API_SECRET", "secret")

async def main():
    if not api_key or not api_secret:
        print("LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set.")
        return

    # Initialize LiveKit client
    svc = api.RoomServiceClient(host, api_key, api_secret)

    room_name = "my-test-room"
    try:
        room = await svc.create_room(
            api.CreateRoomRequest(
                name=room_name,
                empty_timeout=60 * 10, # 10 minutes
            )
        )
        print(f"Room '{room.name}' created.")
    except api.ApiException as e:
        if e.status == 409: # Room already exists
            print(f"Room '{room_name}' already exists.")
        else:
            raise

    # Generate an access token for a participant
    token = api.AccessToken(api_key, api_secret) \
        .with_identity("user1") \
        .with_name("Test User") \
        .with_grants(api.VideoGrants(room_join=True, room=room_name)) \
        .to_jwt()

    print(f"Access Token: {token}")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →