how to dispatch an agent upon creating the access ...
# ask-ai
c
how to dispatch an agent upon creating the access key
t
Thanks for reaching out, Nathan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to dispatch an agent right when you create an access key, you can do that by using the RoomConfiguration in the access token to specify which agent should be dispatched as soon as a participant connects. Here’s a quick rundown: 1. First, create an access token and set up a RoomConfiguration that includes the agent you want to dispatch. For example:
Copy code
import { RoomAgentDispatch, RoomConfiguration } from '@livekit/protocol';
import { AccessToken } from 'livekit-server-sdk';

const token = new AccessToken();
token.identity = 'participant-id';
token.addGrant({ roomJoin: true, room: 'room-name' });
token.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({
      agentName: 'agent-name',
      metadata: '{"key": "value"}',
    }),
  ],
});
const jwt = token.toJwt();
console.log(jwt);
1. Then, when the participant connects to the room, the agent you specified will be dispatched automatically—pretty neat, right? If you’d rather have more hands-on control, you can also dispatch an agent manually using the AgentDispatchClient like this:
Copy code
import { AgentDispatchClient } from 'livekit-server-sdk';

const client = new AgentDispatchClient('LIVEKIT_URL');
const dispatch = await client.createDispatch('room-name', 'agent-name', {
  metadata: '{"key": "value"}',
});
console.log(dispatch);
Feel free to reach out if you want me to walk you through any of this or if you have other questions—I’m here to help! Sources: packages/livekit-server-sdk/src/AgentDispatchClient.ts | Dispatching agents | LiveKit Docs | livekit/livekit_agent_dispatch.twirp.go