LiveKit Server API
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
- gotcha The `livekit-api` library is built on `asyncio`. All service methods return awaitable objects. Calling these methods without `await` or outside an `async` context will lead to runtime errors.
- gotcha Access tokens generated by `api.AccessToken` require explicit grants (e.g., `api.VideoGrants`) to define what actions a participant can perform. Incorrectly configured grants are a common cause of 'permission denied' errors.
- gotcha Hardcoding `LIVEKIT_HOST`, `LIVEKIT_API_KEY`, and `LIVEKIT_API_SECRET` directly in your code is a significant security risk and makes deployments brittle. These are sensitive credentials.
- breaking Significant changes in the underlying LiveKit server API or protocol can lead to unexpected behavior or API breakage if the `livekit-api` SDK version is not kept reasonably up-to-date with the server version.
Install
-
pip install livekit-api
Imports
- RoomServiceClient
from livekit.api import RoomServiceClient
- AccessToken
from livekit.api import AccessToken
- VideoGrants
from livekit.api import VideoGrants
- CreateRoomRequest
from livekit.api import CreateRoomRequest
Quickstart
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())