Skip to content

JWT Tokens

Kevin Sylvestre edited this page Aug 29, 2019 · 9 revisions

Client Capability Tokens

Twilio Client Capability Tokens are required for setting up a device to send and receive calls via Twilio Client.

Setup Work

require 'twilio-ruby'

# put your own account credentials here:
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'

# set up
capability = Twilio::JWT::ClientCapability.new account_sid, auth_token

Allow Incoming Calls

# Allow incoming calls, and give the client a name.
incoming_scope = Twilio::JWT::ClientCapability::IncomingClientScope.new 'john'
capability.add_scope(incoming_scope)

# Generate the token string
@token = capability.to_s

Allow Outgoing Calls

# Allow outgoing calls to an application.
outgoing_scope = Twilio::JWT::ClientCapability::OutgoingClientScope.new 'AP11111111111111111111111111111111', 'john'
capability.add_scope(outgoing_scope)

# Generate the token string
@token = capability.to_s

Allow Outgoing Calls With Parameters

You can send parameters to your Application's VoiceUrl by passing a hash in the initialization. Here we pass along a hypothetical user id.

params = {'user_id' => @user.id}

# Allow outgoing calls to an application and pass the user id to your server.
outgoingScope = Twilio::JWT::ClientCapability::OutgoingClientScope.new 'AP11111111111111111111111111111111', 'john', params
capability.add_scope(outgoingScope)

# Generate the token string
@token = capability.to_s 

The user_id parameter and its value will be sent to your Application's VoiceUrl along with the other parameters that Twilio usually sends, like From, To and CallSid.

Allow Multiple Capabilities

You can generate a Capability Token that supports multiple capabilities, so that your Twilio Client device can make and receive calls.

client_name = 'john'

# Allow incoming calls, and give the client a name.
incomingScope = Twilio::JWT::ClientCapability::IncomingClientScope.new client_name
capability.add_scope(incomingScope)

# Allow outgoing calls to an application and pass the user id to your server.
params = {'user_id' => @user.id}
outgoingScope = Twilio::JWT::ClientCapability::OutgoingClientScope.new 'AP11111111111111111111111111111111', client_name, params
capability.add_scope(outgoingScope)

# Generate the token string
@token = capability.to_s 

Change the TTL of the Token

By default all tokens generated with the Twilio helper libraries expire after one hour (3600s). But you should configure this expiration to be as short as possible for your application.

# Generate the token string with 5 seconds of expiration
token = Twilio::JWT::ClientCapability.new(@account_sid, @auth_token, ttl: 5)

Access Tokens

require 'twilio-ruby'

# Required for any Twilio Access Token
account_sid = 'ACxxxxxxxxxxxx'
api_key = 'SKxxxxxxxxxxxx'
api_secret = 'xxxxxxxxxxxxxx'

# Required for Video
identity = 'user'

# Create Video grant for our token
video_grant = Twilio::JWT::AccessToken::VideoGrant.new
video_grant.room = 'cool room'

# Create an Access Token
token = Twilio::JWT::AccessToken.new(
  account_sid,
  api_key,
  api_secret,
  [video_grant],
  identity: identity
)

# Generate the token
puts token.to_jwt