what is a token in livekit for room, if the room i...
# ask-ai
b
what is a token in livekit for room, if the room is already created then can we use a token to join it?
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
In LiveKit, a token is a unique access token that each participant must use to connect to a room. The token encodes important information such as: • The room name • The participant's identity • The participant's permissions Yes, tokens can be used to join an already created room. This is their primary purpose—to authenticate participants and allow them to join existing rooms. If a room doesn’t exist yet, it will be automatically created when the first participant joins with a valid token. To create a token for joining a room, you can use the
AccessToken
class from the LiveKit server SDK. Here’s an example:
Copy code
import { AccessToken } from 'livekit-server-sdk';

const roomName = 'name-of-room';
const participantName = 'user-name';

const at = new AccessToken('api-key', 'secret-key', {
  identity: participantName,
});
at.addGrant({ roomJoin: true, room: roomName });
const token = await at.toJwt();
By default, tokens expire after 6 hours, but this can be customized by setting a TTL (time to live) value. Sources: Connecting to LiveKit | LiveKit Docs | LiveKit JS Server SDK - v2.9.7
b
give me python code to create a token with all possible options
t
It seems that a comprehensive Python example for creating a token with all possible options is not readily available in the documentation. However, I can guide you on how to create a token using the LiveKit server SDK in Python, and you can customize it further based on your requirements. Here’s a basic example to get you started:
Copy code
from livekit_server_sdk import AccessToken, VideoGrant

# Define your API key and secret key
api_key = "your-api-key"
secret_key = "your-secret-key"

# Define room and participant details
room_name = "example-room"
participant_identity = "example-user"

# Create a VideoGrant object with permissions
grant = VideoGrant(
    room_join=True,  # Allow joining the room
    room=room_name,  # Specify the room name
    can_publish=True,  # Allow publishing tracks
    can_subscribe=True,  # Allow subscribing to tracks
    can_publish_data=True  # Allow publishing data
)

# Create an AccessToken object
token = AccessToken(
    api_key=api_key,
    secret_key=secret_key,
    identity=participant_identity,
    grant=grant
)

# Optionally, set additional options
token.ttl = 3600  # Token expiration time in seconds (1 hour)

# Generate the token
jwt_token = token.to_jwt()

print("Generated Token:", jwt_token)
This example includes options for joining a room, publishing tracks, subscribing to tracks, and publishing data. You can modify the permissions and add other options as needed. Let me know if you need further assistance!
b
what is room_create option in videogrants
t
The
room_create
option in
VideoGrants
is a boolean parameter that grants permission to create rooms in LiveKit. When this permission is set to
True
, the token holder is allowed to create rooms in the LiveKit system. Let me know if you need further clarification or assistance! Sources: livekit.api.access_token API documentation
b
for a token, how much is the default time for it to be expired, and can we set our own expiry date?
t
The default expiration time for a LiveKit access token is 6 hours. However, you can set your own custom expiration time by specifying a
ttl
(time-to-live) parameter when creating the token. The
ttl
can be defined in seconds or as a string describing a time span, such as
'2 days'
or
'10h'
. For example:
Copy code
const at = new AccessToken('api-key', 'secret-key', {
  identity: participantName,
  ttl: '10h'  // Sets expiration to 10 hours
});
Let me know if you need further assistance! Sources: LiveKit JS Server SDK - v2.9.7 | AccessTokenOptions | LiveKit JS Server SDK - v2.9.7