LiveKit token
Generates a LiveKit JWT token for a participant to join a specific room.
When authentication is enabled (auth.id is present), this handler:
- Creates the room if it doesn’t exist
- Sets
room.metadata.auth_idto the authenticated tenant’s ID - Issues the token only after metadata is verified/set
Arguments
state- Shared application state containing LiveKit configurationrequest- Token request with room name and participant details
Returns
Response- JSON response with token or error status
Errors
- 400 Bad Request - Invalid request data (empty fields)
- 403 Forbidden - Room exists with a different tenant’s
auth_id - 500 Internal Server Error - LiveKit service not configured, room creation failed, or token generation failed
Room creation behavior
Whenauth.id is present (authenticated mode):
- If the room doesn’t exist, Sayna creates it automatically.
- The room’s
metadata.auth_idis set to your tenant ID. - The token is issued only after ownership is verified or established.
Authorization
| Scenario | Behavior |
|---|---|
| Room doesn’t exist | Room is created with metadata.auth_id set to your tenant ID. |
| Room exists, you own it | Token is issued normally. |
| Room exists, different owner | 403 Forbidden is returned. |
| Unauthenticated mode | Token is issued without ownership checks. |
Error responses
| Status | Condition |
|---|---|
400 Bad Request | Missing or empty required fields. |
403 Forbidden | Room exists with a different tenant’s auth_id. |
500 Internal Server Error | LiveKit not configured or token generation failed. |
Authorizations
Authentication token for protected endpoints. Can be provided as Authorization: Bearer <token> or ?api_key=<token>. Required when AUTH_REQUIRED is enabled.
Body
Request body for generating a LiveKit token
Example
{
"room_name": "conversation-room-123",
"participant_name": "Alice Smith",
"participant_identity": "user-alice-456"
}Unique identifier for the participant (e.g., "user-123")
"user-alice-456"
Display name for the participant (e.g., "John Doe")
"Alice Smith"
The LiveKit room name to generate a token for
"conversation-room-123"
Response
Token generated successfully. Room is created if it doesn't exist and metadata.auth_id is set.
Response containing the generated LiveKit token
Example
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"room_name": "conversation-room-123",
"participant_identity": "user-alice-456",
"livekit_url": "ws://localhost:7880"
}The LiveKit server URL to connect to
"ws://localhost:7880"
Echo back the participant identity for client confirmation
"user-alice-456"
Echo back the room name for client confirmation
"conversation-room-123"
The generated JWT token for LiveKit
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."