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
Example
Section titled “Example”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, }), }; }}Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new TwilioTokenService(moduleRef): TwilioTokenService;Defined in: lib/token/twilio-token.service.ts:66
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
moduleRef |
ModuleRef |
Returns
Section titled “Returns”TwilioTokenService
Methods
Section titled “Methods”createChatToken()
Section titled “createChatToken()”createChatToken(options, clientName?): string;Defined in: lib/token/twilio-token.service.ts:141
Mint a token granting access to Conversations.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options |
TwilioChatTokenOptions |
Grant and JWT settings. |
clientName? |
string |
Named client to sign with. Omit for the default client. |
Returns
Section titled “Returns”string
Example
Section titled “Example”tokens.createChatToken({ identity: 'alice', serviceSid: 'ISxxxxxxxx' });createSyncToken()
Section titled “createSyncToken()”createSyncToken(options, clientName?): string;Defined in: lib/token/twilio-token.service.ts:171
Mint a token granting access to Sync.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options |
TwilioSyncTokenOptions |
Grant and JWT settings. |
clientName? |
string |
Named client to sign with. Omit for the default client. |
Returns
Section titled “Returns”string
Example
Section titled “Example”tokens.createSyncToken({ identity: 'alice', serviceSid: 'ISxxxxxxxx' });createToken()
Section titled “createToken()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options |
TwilioCustomTokenOptions |
Grants and JWT settings. |
clientName? |
string |
Named client to sign with. Omit for the default client. |
Returns
Section titled “Returns”string
Throws
Section titled “Throws”BadRequestException When no grants are supplied, the identity is missing, the TTL exceeds 24 hours, or the client has no API key.
Example
Section titled “Example”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()
Section titled “createVideoToken()”createVideoToken(options, clientName?): string;Defined in: lib/token/twilio-token.service.ts:123
Mint a token granting access to Programmable Video.
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”string
Example
Section titled “Example”tokens.createVideoToken({ identity: 'alice', room: 'daily-standup' });createVoiceToken()
Section titled “createVoiceToken()”createVoiceToken(options, clientName?): string;Defined in: lib/token/twilio-token.service.ts:82
Mint a token granting access to Programmable Voice.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options |
TwilioVoiceTokenOptions |
Grant and JWT settings. |
clientName? |
string |
Named client to sign with. Omit for the default client. |
Returns
Section titled “Returns”string
Throws
Section titled “Throws”BadRequestException When the identity contains characters Voice rejects, the TTL exceeds 24 hours, or the client has no API key.
Example
Section titled “Example”tokens.createVoiceToken({ identity: 'alice', incomingAllow: true });