Skip to content

TwilioTokenService

Defined in: lib/token/twilio-token.service.ts:65

Mints Access Tokens for Twilio’s client-side SDKs: Voice, Video, Conversations, Sync, TaskRouter and Playback.

Access Tokens are short-lived credentials your server issues so a browser or mobile client can talk to Twilio directly, without ever holding your account credentials.

Tokens are signed with an API key, never an auth token, so the client the token is minted for must have been registered with apiKey and apiSecret.

https://www.twilio.com/docs/iam/access-tokens

Minting a Voice token

@Controller('token')
export class TokenController {
constructor(private readonly tokens: TwilioTokenService) {}
@Get('voice')
voice(@Query('identity') identity: string) {
return {
token: this.tokens.createVoiceToken({
identity,
outgoingApplicationSid: 'APxxxxxxxx',
incomingAllow: true,
}),
};
}
}
new TwilioTokenService(moduleRef): TwilioTokenService;

Defined in: lib/token/twilio-token.service.ts:66

Parameter Type
moduleRef ModuleRef

TwilioTokenService

createChatToken(options, clientName?): string;

Defined in: lib/token/twilio-token.service.ts:141

Mint a token granting access to Conversations.

Parameter Type Description
options TwilioChatTokenOptions Grant and JWT settings.
clientName? string Named client to sign with. Omit for the default client.

string

tokens.createChatToken({ identity: 'alice', serviceSid: 'ISxxxxxxxx' });

createSyncToken(options, clientName?): string;

Defined in: lib/token/twilio-token.service.ts:171

Mint a token granting access to Sync.

Parameter Type Description
options TwilioSyncTokenOptions Grant and JWT settings.
clientName? string Named client to sign with. Omit for the default client.

string

tokens.createSyncToken({ identity: 'alice', serviceSid: 'ISxxxxxxxx' });

createToken(options, clientName?): string;

Defined in: lib/token/twilio-token.service.ts:205

Mint a token carrying grants you construct yourself.

Use this for TaskRouter and Playback, which have no dedicated helper, or to put several grants on one token.

Parameter Type Description
options TwilioCustomTokenOptions Grants and JWT settings.
clientName? string Named client to sign with. Omit for the default client.

string

BadRequestException When no grants are supplied, the identity is missing, the TTL exceeds 24 hours, or the client has no API key.

A token carrying both Video and Chat

import { jwt } from 'twilio';
tokens.createToken({
identity: 'alice',
grants: [
new jwt.AccessToken.VideoGrant({ room: 'daily-standup' }),
new jwt.AccessToken.ChatGrant({ serviceSid: 'ISxxxxxxxx' }),
],
});

createVideoToken(options, clientName?): string;

Defined in: lib/token/twilio-token.service.ts:123

Mint a token granting access to Programmable Video.

Parameter Type Description
options TwilioVideoTokenOptions Grant and JWT settings. Omit room to allow any room.
clientName? string Named client to sign with. Omit for the default client.

string

tokens.createVideoToken({ identity: 'alice', room: 'daily-standup' });

createVoiceToken(options, clientName?): string;

Defined in: lib/token/twilio-token.service.ts:82

Mint a token granting access to Programmable Voice.

Parameter Type Description
options TwilioVoiceTokenOptions Grant and JWT settings.
clientName? string Named client to sign with. Omit for the default client.

string

BadRequestException When the identity contains characters Voice rejects, the TTL exceeds 24 hours, or the client has no API key.

tokens.createVoiceToken({ identity: 'alice', incomingAllow: true });