Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Flex SDK authentication (public beta)


(new)

Public Beta

The Flex SDK is currently available as a Public Beta product and the information contained in this document is subject to change. This means that some features are not yet implemented and others may be changed before the product is declared as Generally Available. Public Beta products are not covered by a SLA.

(warning)

Not a HIPAA Eligible Service or PCI Compliant

The Flex SDK is not a HIPAA Eligible Service or PCI compliant and should not be used in workflows that are subject to HIPAA or PCI.

There are three options to authenticate the Flex SDK:

Customization

customization page anchor

Depending on your specific requirements, you might need to adjust the optional userOptions provided to createClient for better logging, telemetry, and session management.

It's important to build robust error handling to catch and respond to any issues that might arise during the initialization and operation process.

Integrate the provided methods into your Flex SDK application lifecycle to manage user authentication, token validation, and system setup effectively.


Option 2: Use a JWE token to authenticate

option-2-use-a-jwe-token-to-authenticate page anchor

You can get the Flex authentication token from your authenticated Hosted Flex application(link takes you to an external page).

The token is stored in the browser's local storage, and you can use it to initialize the FlexSDK client.

The JWE token in the browser's local storage.

The following would initialize Flex SDK and enable you to access the surface area of Flex.

1
import { createClient } from '@twilio/flex-sdk'
2
3
const client = await createClient("FlexToken");

Option 3: Build your own authentication

option-3-build-your-own-authentication page anchor

If you don't want to use an SSO solution, you can build your own authentication method.

To build your own authentication, you'll need to:

  1. Provision a user.
  2. Fetch the user you're authenticating.
  3. Mint an authentication token.

For authorization, use Basic Authentication, which is an authentication method that provides a username and password when a request is made. Use your AccountSid for the username, and your AuthToken for the password.

(warning)

Warning

Only use the following endpoints internally on the backend. For security reasons, do not expose your AuthToken in front-end applications.

To provision a user, replace {flex_instance_sid} in the command below with your Flex Instance SID (GOxxxxxxxxxxxxxxxxxxxxx). You'll find your Flex Instance SID in Console on the Flex Overview(link takes you to an external page) page.

1
curl --location 'https://flex-api.twilio.com/v4/Instances/{flex_instance_sid}/Users/Provision' \
2
--header 'Content-Type: application/json' \
3
--header 'Authorization: ••••••' \
4
--data-raw '{
5
"username": "user1",
6
"email": "test@example.com",
7
"full_name": "Foo Bar",
8
"roles": ["agent"],
9
"worker" : {
10
"attributes" : {
11
"channel.voice.capacity" : 10,
12
"language": "english, spanish",
13
"more.stringarray" : "more,more2"
14
}
15
}
16
}'

Sample response:

1
{
2
"account_sid": "ACCOUNT_SID",
3
"created_date": "2025-03-25T15:09:41Z",
4
"deactivated": false,
5
"deactivated_date": null,
6
"email": "user@example.com",
7
"flex_team_sid": "FLEX_TEAM_SID",
8
"flex_user_sid": "FLEX_USER_SID",
9
"full_name": "Foo Bar",
10
"instance_sid": "FLEX_INSTANCE_SID",
11
"locale": null,
12
"roles": [
13
"agent"
14
],
15
"teams": {
16
"team_member": null,
17
"team_owner": null
18
},
19
"updated_date": "2025-03-25T15:09:41Z",
20
"username": "user1",
21
"version": 0,
22
"worker": {
23
"worker_sid": "WORKER_SID",
24
"workspace_sid": "WORKSPACE_SID"
25
}
26
}
27

Note the Flex User ID (FUxxxxxxxxxxxxxxxxxxxxx) as you'll use this later for token minting.

To fetch the user you're authenticating, run the following command:

1
curl --location 'https://flex-api.twilio.com/v4/Instances/{flex_instance_sid}/Users?Username=user1' \
2
--header 'Authorization: ••••••' \

You can query users with the Username= query parameter and value, which is user1 in the previous example.

Sample response:

1
{
2
"account_sid": "ACCOUNT_SID",
3
"instance_sid": "INSTANCE_SID",
4
"meta": {
5
"direct_token": true,
6
"list_key": "users",
7
"next_token": null,
8
"page_size": 1,
9
"previous_token": null
10
},
11
"users": [
12
{
13
"account_sid": "ACCOUNT_SID",
14
"created_date": "2025-03-25T15:09:41Z",
15
"email": "user@example.com",
16
"flex_team_sid": "FLEX_TEAM_SID",
17
"flex_user_sid": "FLEX_USER_SID",
18
"full_name": "Foo Bar",
19
"instance_sid": "INSTANCE_SID",
20
"locale": null,
21
"roles": [
22
"agent"
23
],
24
"updated_date": "2025-03-25T15:09:41Z",
25
"user_sid": null,
26
"username": "user1",
27
"version": 1,
28
"workspace_sid": "WORKSPACE_SID"
29
}
30
]
31
}

Mint an authentication token

mint-an-authentication-token page anchor

To mint an authentication token, update the {flex_instance_sid} and {flex_user_sid} values, then run the following command:

1
curl --location 'https://flex-api.twilio.com/v4/Instances/{flex_instance_sid}/Users/{flex_user_sid}/Tokens' \
2
--header 'Content-Type: application/json' \
3
--header 'Authorization: ••••••' \
4
--data '{
5
"ttl":3600
6
}'

Sample response:

1
{
2
"access_token": "ACCESS_TOKEN",
3
"token_info": {
4
"account_sid": "ACCOUNT_SID",
5
"expiration": "2025-03-25T16:16:16Z",
6
"flex_instance_sid": "FLEX_INSTANCE_SID",
7
"flex_user_sid": "FLEX_USER_SID",
8
"identity": "user1",
9
"permissions": [
10
"FPN9999",
11
"FPN0000"
12
],
13
"roles": [
14
"agent"
15
],
16
"username": "user1"
17
}
18
}