Skip to content

JWT Tokens

Tom edited this page Jul 10, 2017 · 9 revisions

Home | Documentation | Util | Capability


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

Setup Work

require 'rubygems' # not necessary with ruby 1.9 but included for completeness
require 'twilio-ruby'

# put your own account credentials here:
account_sid = 'AC043dcf9844e13758bc3a36a84c29761'
auth_token = '62ea81de3a5b413254eb263595357c69'

# set up
capability = Twilio::Util::Capability.new account_sid, auth_token

Allow Incoming Calls

# Allow incoming calls, and give the client a name.
capability.allow_client_incoming 'andrew'

# Generate the token string
@token = capability.generate

Allow Outgoing Calls

# Allow outgoing calls to an application.
capability.allow_client_outgoing 'AP89a0180a1a4ddf1da954efca349b7a20'

# Generate the token string
@token = capability.generate

Allow Outgoing Calls With Parameters

You can send parameters to your Application's VoiceUrl by passing a hash to #allow_client_outgoing. 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.
capability.allow_client_outgoing 'AP89a0180a1a4ddf1da954efca349b7a20', params

# Generate the token string
@token = capability.generate

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.

# Allow incoming calls, and give the client a name.
capability.allow_client_incoming 'andrew'

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

# Allow outgoing calls to an application and pass the user id to your server.
capability.allow_client_outgoing 'AP89a0180a1a4ddf1da954efca349b7a20', params

# Generate the token string
@token = capability.generate

###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 = capability.generate(5)
Clone this wiki locally