worried-kite-20319
09/12/2025, 8:59 PMtall-airline-16756
09/12/2025, 8:59 PMtall-airline-16756
09/12/2025, 8:59 PMimport { AccessToken } from 'livekit-server-sdk';
const at = new AccessToken('api-key', 'secret-key', {
identity: 'user-identity',
metadata: 'custom metadata string', // Pass metadata here
});
at.addGrant({
room: 'room-name',
roomJoin: true,
});
const token = await at.toJwt();
console.log('access token', token);
Method 2: Set metadata using the setter
import { AccessToken } from 'livekit-server-sdk';
const at = new AccessToken('api-key', 'secret-key', {
identity: 'user-identity',
});
at.metadata = 'custom metadata string'; // Set metadata using setter
at.addGrant({
room: 'room-name',
roomJoin: true,
});
const token = await at.toJwt();
Just a few helpful notes:
• The metadata
field expects a string. If you want to share more complex data, you can always serialize it as JSON.
• This metadata is accessible to other participants through the Participant.metadata
property in client SDKs—pretty handy!
• If you’d like to update metadata after the connection is established, just make sure to enable canUpdateOwnMetadata
in the video grant.
If you have any questions or need a hand with anything else, please don’t hesitate to ask. I’m here to help!
Sources: Docs::Home | examples/agent-dispatch/index.ts | web/src/app/api/token/route.ts | web/app/api/connection-details/route.tsworried-kite-20319
09/12/2025, 9:24 PMtall-airline-16756
09/12/2025, 9:25 PMParticipant.metadata
property. Here's a simple example:
// JavaScript\nroom.on(RoomEvent.ParticipantMetadataChanged, (oldMetadata, participant) => {\n console.log('Metadata changed from', oldMetadata, 'to', participant.metadata);\n});
2. Update Metadata from Server APIs
You can also update or retrieve a participant's metadata using the server API. For instance, in Node.js:
roomServiceClient.updateParticipant('room', 'identity', {\n metadata: 'updated metadata',\n});
3. Metadata Characteristics
• The metadata
field is a single string. If you want to store structured data, you can serialize it as JSON.
• Keep in mind, metadata is limited to 64 KiB and is synchronized with all participants.
• For real-time updates, just listen for metadata change events in your client application.
If you have any questions or want me to provide more examples, please feel free to ask. I'm here to help!
Sources: Docs::Homeworried-kite-20319
09/12/2025, 9:32 PMtall-airline-16756
09/12/2025, 9:33 PMworried-kite-20319
09/12/2025, 9:42 PMtall-airline-16756
09/12/2025, 9:42 PMconst at = new AccessToken('api-key', 'secret-key', {\n identity: 'user-identity',\n metadata: JSON.stringify({ user_id: '12345', language: 'es', department: 'support' }),\n});
2. Dispatch Agents Based on Metadata
Next, you can set up the room to dispatch specific agents using that metadata. Here's a simple example in Python:
token = (\n AccessToken()\n .with_identity("my_participant")\n .with_grants(VideoGrants(room_join=True, room=room_name))\n .with_room_config(RoomConfiguration(\n agents=[\n RoomAgentDispatch(\n agent_name="sales-agent",\n metadata='{"user_id": "12345", "language": "es", "department": "support"}'\n ),\n ],\n ))\n .to_jwt()\n)
3. Use Metadata in the Agent's Entrypoint
On the server side, your agent can access this metadata to decide how to route the participant. Here's how that might look:
async def entrypoint(ctx: JobContext):\n metadata = json.loads(ctx.job.metadata)\n user_language = metadata.get("language")\n department = metadata.get("department")\n\n if department == "support":\n agent = SupportAgent(language=user_language)\n elif department == "sales":\n agent = SalesAgent()
Just a couple of quick notes to keep in mind:
• Since metadata is passed as a string, using JSON helps keep your data structured nicely.
• If you specify an agent_name
, automatic dispatching will be turned off.
• The room will be created automatically if it doesn't already exist when dispatching.
I hope this helps clarify things! If you have any questions or want to dive deeper into any part, just let me know—I'm here to help!
Sources: Agent dispatch | LiveKit Docs | examples/agent_dispatch.py | Job lifecycle | LiveKit Docs | External data and RAG | LiveKit Docs | Participant attributes and metadata | LiveKit Docs