Creating Access Tokens

In the last guide, we covered SDK client initialization mechanics and the need for a generated Access Token. This Access Token is the credential your SDK client endpoints must use to identify and authenticate themselves with the Chat Service.

This Token is generated on your server or backend as you authenticate your user and is then utilized by the Chat SDK client to authorize with the Chat Service.

Create an Access Token

On your server, we must decide, based on the token request that was sent to us, who the user is and what they should be allowed to do.

To figure out who the user is (their identity), you might use your existing login system, using session cookies, an API token, or whatever mechanism you use to secure API requests or pages today. Who the user is and how you authorize their use will vary from app to app.

If you determine that the user should indeed be allowed to access your Chat application, you will grant your user access to Chat by generating an Access Token as part of your authentication flow. You will then return the token to the user client for use in the Chat SDK.

When creating an Access Token for Programmable Chat, the following information is needed:

Twilio Account Sid

This is the Account Sid of your Twilio account and must be the account in which you have created your Programmable Chat Service. Manage your Chat Services.

Programmable Chat Service Sid

This is the Chat Service Sid where your Users, Channels, Messages and other chat related data resides. This is the Chat Service you grant the SDK client access to.

Twilio API Key Sid

This is the Sid of an API created for your Twilio Account, which is used to sign the Access Token cryptographically. You can create these API keys here.

Twilio API Secret

This is the secret part of the API Key above, also managed here.

Identity

The identity of your User. For example, user@some.domain.com.

Endpoint ID

Note: This is only needed if you are using any of the following prior Chat SDK versions:

  • JS SDK: <= 0.11.x
  • iOS SDK: <= 0.16.x
  • Android: <= 0.11.x

Versions of the SDK newer than these automatically generate endpoint_id for you and will ignore this property.

An "endpoint" in Chat is a unique app, device, and user combination that can receive a message in a disambiguated way. Therefore, a User (identity ) can have multiple endpoints (browser, phone, tablet).

For example, "alice@example.com" using "SquareChat" on her iPhone is a different endpoint (addressable message destination) than "alice@example.com" using "SquareChat" on her Kindle Fire tablet. The Endpoint Id you generate on the server should be unique per device, application, and identity. While it can be a string in any format you want, your Endpoint ID should include at least these three dimensions:

  • The identity of the user (for example, "alice@example.com")
  • An identifier that uniquely identifies your application (for example, "SquareChat")
  • A unique identifier for the device, either generated by your application or the OS (for example, a system-provided UUID)

As mentioned above, this identifier is generated for you in newer versions of the Chat SDK, so you only need to generate or provide this property if using an older version.

Optional: TTL (Time To Live) Access Tokens are only valid for a period of time, in seconds. The default is 3600 seconds (1 hour), but you can adjust this to your needs up to a maximum of 24 hours.

Once your client receives an Access Token from your server, you can initialize the Twilio Chat SDK and start sending and receiving messages, as covered in the previous guide.

Next: Access Token Lifecycle

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.