i want to make a jwt token with node and dispatch ...
# ask-ai
c
i want to make a jwt token with node and dispatch it to a specific agent name
t
Thanks for reaching out, ZaidKaraymeh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To create a JWT token with Node.js and dispatch it to a specific agent name in LiveKit, let's walk through these steps together: 1. Generate the JWT Token: You'll want to use the
livekit-server-sdk
to create an access token. Make sure to include a
RoomConfiguration
with a
RoomAgentDispatch
entry that specifies the target
agentName
. Here’s a simple example to guide you:
Copy code
import { AccessToken } from 'livekit-server-sdk';
import { RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';

const at = new AccessToken('your-api-key', 'your-api-secret', {
  identity: 'my-participant',
});

at.addGrant({
  roomJoin: true,
  room: 'my-room',
});

at.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({
      agentName: 'test-agent',
      metadata: '{"user_id": "12345"}',
    }),
  ],
});

const token = await at.toJwt();
console.log('Access token:', token);
1. Configure the Agent: It’s important to make sure the agent is registered with the same
agentName
in its
WorkerOptions
. This way, automatic dispatch is disabled, allowing for explicit assignment. 2. Optional: If you want, you can also dispatch agents programmatically using the
AgentDispatchClient
. It’s a handy option when needed. If you have any questions or would like me to provide more examples, please don’t hesitate to ask. I’m here to help! Sources: Authentication | LiveKit Docs | Agent dispatch | LiveKit Docs | Dispatching agents | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | templates.yaml