# Subaccounts

## Twilio Subaccounts Management

Subaccounts can be created and accessed via Console, but can also be managed via the Twilio REST API. For more details, please see our article [View and Create New Twilio Subaccounts](https://help.twilio.com/articles/360011348693-View-and-Create-New-Twilio-Subaccounts).

For each subaccount you will need the following:

1. Subaccount Sid
2. API key & Secret
3. Voice Application
4. Caller number: make sure the number uses the main account calling function

{% hint style="info" %}
Following we will explain more how to manage your subaccounts using Twilio Console.
{% endhint %}

#### Create a subaccount:

To create a subaccount go to the console > subaccounts > Create

{% hint style="danger" %}
If you are using the console, make sure to switch to the subaccount before implementing the following steps
{% endhint %}

#### Create a subaccount API key:

Make sure you are using the subaccount console, then follow the same steps as [create an API key](https://docs.i2b.co/v1/twilio-in-browser-call-plugin/getting-started#create-an-api-key)

#### Create a subaccount voice application:

Make sure you are using the subaccount console, then follow the same steps as [Create a voice application](https://docs.i2b.co/v1/twilio-in-browser-call-plugin/getting-started#create-a-voice-application). Regarding the application voice function, you can use the main account voice function for easier maintenance. How ever you can create functions for each account, if you have a special use case.

## Bubble Editor

#### **Step1:** Access token generation

Create a custom event this event then Add an action that requests a subaccount voice access token and store the value in a state

<figure><img src="https://2270479705-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lby9-31FtGubTBCYpg4%2Fuploads%2FdFo5dLaVcA65epJKQlQB%2Fcreate-access-token-event.png?alt=media&#x26;token=87d6078d-2264-493e-931d-9d79a94f8618" alt=""><figcaption></figcaption></figure>

#### **Step 2:** Adding token to the client

Provide the token to the client element by referring to the token state

<figure><img src="https://2270479705-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lby9-31FtGubTBCYpg4%2Fuploads%2F5AvdtEyqwviT38jnyrqG%2Ftwilio-client-sub-account.png?alt=media&#x26;token=f71905bf-b9d4-4ba9-bf21-053c384537b7" alt=""><figcaption></figcaption></figure>

#### Step 3: Refreshing Client Tokens Before Expiration

To ensure uninterrupted service, it's crucial to refresh client tokens before they expire. Implement a token refresh mechanism by triggering a custom event, `get-token`, upon detecting that the current token is nearing expiration. Here's how to set up the event trigger:

<figure><img src="https://2270479705-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lby9-31FtGubTBCYpg4%2Fuploads%2Fq32QvsNOjxiW0818HbJ8%2Ftoken-expiring.png?alt=media&#x26;token=cc6cec09-17f7-4d43-8940-c68b4ce1fc08" alt=""><figcaption></figcaption></figure>

1. **Detect Token Expiration:** By adding the token expiring element event
2. **Trigger Custom Event:** Add the `get-token` action. This action should request a new token and update the token state.

By following these steps, you can ensure your application maintains secure and continuous access to resources by keeping client tokens up to date.

## Making a subaccount call

Similar to the [making calls](https://docs.inova.us/v1/twilio-in-browser-call-plugin/making-calls), just make sure to use a Caller number that's registered under the subaccount.

## Receiving a subaccount incoming call

That can be implemented in two ways:

1. Using one voice function and adding the voice function URL to all the numbers under this account
2. Using Twilio Studio flow; Similar to the [receiving calls](https://docs.inova.us/v1/twilio-in-browser-call-plugin/receiving-calls), just make sure to do it form the subaccount console.
