How to add video grants in JS
# ask-ai
f
How to add video grants in JS
t
Thanks for reaching out, Ayush. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd be happy to guide you through adding video grants in JavaScript for LiveKit. Here’s a step-by-step approach to help you get started: 1. Import Required Modules: First, import
AccessToken
and
VideoGrant
from the
livekit-server-sdk
package.
import { AccessToken, VideoGrant } from 'livekit-server-sdk';
2. Initialize the Access Token: Next, create an
AccessToken
using your LiveKit API key and secret.
const at = new AccessToken('api-key', 'secret-key', { identity: 'participant-identity' });
3. Define the Video Grant: Here, you specify the permissions and room details in a
VideoGrant
object.
const videoGrant = { room: 'my-room', roomJoin: true, canPublish: true, canSubscribe: true };
4. Add the Video Grant: Then, attach the
VideoGrant
to your access token using the
addGrant()
method.
at.addGrant(videoGrant);
5. Generate the JWT Token: Finally, generate the signed JWT token by calling
toJwt()
.
const token = await at.toJwt(); console.log('access token', token);
To bring it all together, here’s a complete example for you to try out:
Copy code
import { AccessToken, VideoGrant } from 'livekit-server-sdk';

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

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

const videoGrant = { room: roomName, roomJoin: true, canPublish: true, canPublishData: true, canSubscribe: true };

at.addGrant(videoGrant);

const token = await at.toJwt();
console.log('access token', token);
If you have any questions or need a hand with any part of this, please don’t hesitate to ask. I’m here to help! Sources: Docs::Home | Authentication | LiveKit Docs | packages/livekit-server-sdk/src/grants.test.ts