Twilio API: Access Tokens
Access Tokens are short-lived tokens that you can use to authenticate Twilio Client SDKs like Voice, Conversations, Sync, and Video.
You create them on your server to verify a client’s identity and grant access to client API features. All tokens have a limited lifetime, configurable up to 24 hours. However, a best practice is to generate Access Tokens for the shortest amount of time feasible for your application.
Table of Contents
How to create Access Tokens
Twilio Access Tokens are based on the JSON Web Token standard. You can read about the details of the JWT format for Access Tokens here, but if you’re using one of Twilio’s official helper libraries you can use our token-generation functionality without having to know how they’re constructed.
Let’s see how you can create an Access Token in our application.
Step 1: Create an API Key
First, you need to create an API key. This contains a secret which will be used to sign Access Tokens. You can create API keys from the Twilio Console or using the REST API. When you create the API key, you’ll be shown the Key’s secret. For security, you will only be shown the secret at this time, so you should store it with the key’s SID in a secure location for the next step.
Step 2: Generate an Access Token
Now use your new API key’s secret to generate an Access Token using a Twilio Helper Library. Each token is granted access to specific client features. Here is an example which demonstrates how to generate tokens that grant access to Conversations, Voice, and Video:
Step 3: Authenticate
Now you’re ready to use your token. For client-side SDKs like Conversations and video, you will need to get the stringified token to your client-side code via Ajax or some other means. Refer to the Identity and Access Tokens guides in the product documentation for video or Conversations for more details.
Managing the lifecycle of Access Tokens
Your application will use API keys to manage the lifecycle of Access Tokens as follows:
- Create an API key and an Access Token as shown in the steps above.
- Delete the API key to revoke all of the Access Tokens that it was used to generate.
The JSON Web Token format
Each Access Token is a JSON Web Token (JWT), an encoded JSON object with three parts: the header, the payload, and the signature. The following is a JWT token generated for Conversations using code similar to the example above:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS3h4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4LTE0NTA0NzExNDciLCJpc3MiOiJTS3h4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4Iiwic3ViIjoiQUN4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eCIsIm5iZiI6MTQ1MDQ3MTE0NywiZXhwIjoxNDUwNDc0NzQ3LCJncmFudHMiOnsiaWRlbnRpdHkiOiJ1c2VyQGV4YW1wbGUuY29tIiwiaXBfbWVzc2FnaW5nIjp7InNlcnZpY2Vfc2lkIjoiSVN4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eCIsImVuZHBvaW50X2lkIjoiSGlwRmxvd1NsYWNrRG9ja1JDOnVzZXJAZXhhbXBsZS5jb206c29tZWlvc2RldmljZSJ9fX0.IHx8KeH1acIfwnd8EIin3QBGPbfnF-yVnSFp5NpQJi0
If we inspect it with the debugger at jwt.io, we can further explore its content.
Header
{ "typ": "JWT", "alg": "HS256", "cty": "twilio-fpa;v=1" }
The header
section encodes the format of the token:
typ
is the type of token. Its value must be"JWT"
.alg
is the algorithm used to encode the token. Its value must be"HS256"
.cty
is the content-type and encodes the version of the Access Token. Its value must be"twilio-fpa;v=1"
.
Payload
{ "jti": "SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-1450471147", "iss": "SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "sub": "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "nbf": 1450471147, "exp": 1450474747, "grants": { "identity": "user@example.com", "chat": { "service_sid": "ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" } } }
The payload
section describes the authorization granted:
jti
is a unique identifier for the token. Your application can choose this identifier. The default helper library implementation includes the SID of the API key used to generate the token, and a unique random string.iss
is the issuer — the API key containing the secret used to sign the token.sub
is the SID of the Twilio Account to which access is scoped.nbf
is the timestamp at which the token was generated.exp
is the timestamp at which the token will expire. Tokens have a maximum age of 24 hours.grants
is the list of permissions that the token grants. Client SDK (Conversations, Video) grant values will vary from SDK to SDK.
Signature
The signature
section is a signed hash that serves to prove the authenticity of the token. It is the result of hashing the JWT header and payload together with your API key secret, which should only be known to your application and Twilio.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.