Everything You Need to Know About Using Twilio Subaccounts

February 24, 2021
Written by
Reviewed by
Liz Moy
Twilion

This post will walk you through the benefits of using subaccounts and how to send messages via subaccounts. Subaccounts allow you to separate your usage, numbers, and settings while sharing a balance with your main account. They're a great way to segment your usage of Twilio.  

Below are the main items we will work through in this post:

  • Technical Requirements
  • What are Twilio subaccounts and why should you use them?
  • Using subaccounts to send a Message

 

Technical Requirements

  • A free Twilio Account - Sign up for one here through this link, you’ll get an extra $10 in credit when you upgrade.
  • A Twilio Phone number.
  • A verified mobile number (for receiving SMS messages).
  • Familiarity with sending REST API requests. Here are helpful tips on running a cURL from bash or zsh or you can use a tool like Postman.

What are Twilio subaccounts and why should you use them?

Subaccounts are containers that separate resources that belong to a Twilio project (Parent). For example; consider the fictional company Owlie Notifications. Owlie Notifications offers a SaaS platform that allows customers the ability to make calls. Owlie Notifications has about 50 customers.

Owlie Notifications has two options when creating their Twilio account structure:

  1. Create a Twilio Account, which is tied to an identifier called an Account SID, that is unique to their account, and is always prepended with “AC”. Whenever Owlie Notifications makes an API request they will use the Account SID for that account in their API request. For the sake of example, let’s say their Account SID is AC123.
  2. Create a Twilio Account (Account SID = AC123, aka the parent account) the same way suggested in option one, however they will also create a subaccount for each of their 50 customers (50 subaccounts). Each of the 50 subaccounts will get its own unique Account SID. So, when Owlie Notifications makes an API request for a customer they will use the appropriate subaccount Account SID instead of the parent account’s Account SID.

Here’s why going with option two is the optimal choice.

Key benefits of subaccounts

You can segment the billing process 

If Owlie Notifications chose not to use subaccounts they would need to keep track of all the usage for each customer on their end, which is additional work and leaves room for error. Because subaccounts act as containers, you can utilize the Twilio Usage API to get specific billing data for a subaccount, thus reducing the business logic needed to implement individual customer tracking.

Allows for resource provisioning

Subaccounts allow for resources (such as phone numbers) to be added to the specific subaccount. One consideration is that most resources can belong to only one subaccount at a time. This could be tricky if you are constantly moving numbers between accounts. For example, you will need to handle this logic if you close a subaccount but want to retain the phone number. Luckily, Twilio allows you to move phone numbers between accounts.

Gives you additional control over multiple accounts from the same project: With subaccounts you have the ability to suspend, close and reactivate subaccounts based on your customers’ needs.

Considerations before migrating to subaccounts

For customers using Twilio to send messages, Twilio will help to manage your customer opt-outs, like when an end-user replies STOP to message. Twilio maintains this opt-out list at the account level.

This means if an end-user has replied STOP to a phone number and that phone number is moved from a project to a subaccount, which has a unique Account SID, that opt-out mapping is broken. If you haven’t been tracking opt-outs; start now to ensure you have the most up to date list of subscribers.

Using subaccounts to send a Message

You might be thinking, “What if I am not using subaccounts already? How hard is it to migrate?”

Imagine that Owlie Notifications has been using a single account for all 50 customers. They now have customers that want to send SMS messages. Below are the steps that Owlie Notifications will likely need to follow:

You will use the subaccounts REST API to do this programmatically, however you can choose to use the server-side helper libraries if you prefer.

First, log into your Twilio Console. You will see your Account SID and Auth Token values assigned to your account in the dashboard:

Twilio Account SID and Auth Token in the console

For the following examples, you will use the Account SID and Auth Token for the Twilio account that you just made or copied. Don’t forget to replace them in all the placeholders below.

Create a subaccount

Let's continue with Owlie Notifications customers (Falcon Flights) and create a subaccount for them.

Request

From your terminal or command prompt, run the following command to make the request, taking care to replace the placeholder values with your parent account Account SID and Auth Token.

curl -X POST https://api.twilio.com/2010-04-01/Accounts.json \
--data-urlencode "FriendlyName=FalconFlights
" \
-u <INSERT YOUR PARENT ACCOUNT SID>:<INSERT YOUR PARENT AUTH TOKEN>

Response

After running the command, you will receive a response like this:

{
   "status": "active",
   "date_updated": "Wed, 17 Feb 2021 15:12:01 +0000",
   "auth_token": "ACxxxxx",
   "friendly_name": "FalconFlights",
   "owner_account_sid": "ACxxxx",
   "uri": "/2010-04-01/Accounts/ACxxx.json",
   "sid": "ACxxx
",
   "date_created": "Wed, 17 Feb 2021 15:12:01 +0000",
   "type": "Full",
   "subresource_uris": {
...
   }
}

This response includes a few key pieces of information.

There is a new account named FalconFlights which has its own unique Account SID and Auth Token.

The owner_account_sid should match your project (parent) account SID (Owlie Notifications).

Please keep track of the subaccount Account SID and Auth Token as you will use them later on.

Transfer a phone number

Earlier, I mentioned the case where you may want to transfer a phone number. Here’s how to do that. If you don’t have a Twilio phone number, you can purchase one either via the Twilio Console or REST API.

To begin transferring your Twilio phone number between your parent account and a subaccount, navigate to the Active Numbers section of  the Console. Click on a number to see a view with configuration options.

There is where you can find your Phone Number SID (PNxxx).

Finding a Phone Number identifier in Twilio

Request

It's important to ensure that you use the parent account SID in your headers, as the parent account owns the phone number.

Run the following command in your terminal:

curl -X POST https://api.twilio.com/2010-04-01/Accounts/<INSERT PARENT ACCOUNT SID>/IncomingPhoneNumbers/<INSERT PHONE NUMBER SID>.json \
--data-urlencode "AccountSid=<INSERT SUBACCOUNT SID>" \
-u <INSERT YOUR PARENT ACCOUNT SID>:<INSERT YOUR PARENT AUTH TOKEN>

Note, when moving phone numbers to another account, the Voice and SMS URL will be preserved.

Response

{
    "sid": "PNxxx",
    "account_sid": "ACxxx",
    "friendly_name": "(415) 874-0103",
    "phone_number": "+14158740103",
    "voice_url": "https://demo.twilio.com/welcome/voice/",
    "voice_method": "POST",
    "voice_fallback_url": null,
    "voice_fallback_method": "POST",
    "voice_caller_id_lookup": false,
    "date_created": "Wed, 17 Feb 2021 15:17:28 +0000",
    "date_updated": "Wed, 17 Feb 2021 15:23:39 +0000",
    "sms_url": "https://demo.twilio.com/welcome/sms/reply",
    "sms_method": "POST",
    "sms_fallback_url": "",
    "sms_fallback_method": "POST",
    "address_requirements": "none",
    "beta": false,
    "capabilities": {
        "voice": true,
        "sms": true,
        "mms": true,
        "fax": true
    },
    "voice_receive_mode": "voice",
    "status_callback": "",
    "status_callback_method": "POST",
    "api_version": "2010-04-01",
    "voice_application_sid": null,
    "sms_application_sid": "",
    "origin": "twilio",
    "trunk_sid": null,
    "emergency_status": "Inactive",
    "emergency_address_sid": null,
    "address_sid": null,
    "identity_sid": null,
    "bundle_sid": null,
    "uri": "/2010-04-01/Accounts/AC1bcc67e32e161e04aaacc159b2e86601/IncomingPhoneNumbers/PNd7c52583c2ffabfe2a05a165e48e3e68.json",
    "status": "in-use"
}

Create a Messaging Service

Create a Messaging Service in your newly created subaccount. A Messaging Service is a resource that can act as a container of your phone numbers and provides software that makes your life easier when sending messages at scale.

For this request, you will use the Account SID and Auth token associated with the subaccount.

Request

curl -X POST https://messaging.twilio.com/v1/Services \
--data-urlencode "FriendlyName=falconFlightNotifications" \
-u <INSERT YOUR SUBACCOUNT SID>:<INSERT YOUR SUBACCOUNT AUTH TOKEN>

Response

The response will provide you with a Messaging Service SID (MGxxxx). You will use this in the next steps, so keep it handy.

You’ll notice quite a few different attributes in your response object. If you want to learn more about all of the features on a Messaging Service, check out the Messaging Service documentation.

{
   "fallback_method": "POST",
   "fallback_to_long_code": true,
   "date_updated": "2021-02-17T15:28:03Z",
   "synchronous_validation": false,
   "sticky_sender": true,
   "inbound_method": "POST",
   "friendly_name": "falconFlightNotifications",
   "mms_converter": true,
   "validity_period": 14400,
   "account_sid": "ACxxx",
   "fallback_url": null,
   "inbound_request_url": null,
   "url": "https://messaging.twilio.com/v1/Services/MGa91155a0d582afdde110a606465d1daa",
   "sid": "MGxxxx",
   "date_created": "2021-02-17T15:28:03Z",
   "smart_encoding": true,
   "scan_message_content": "inherit",
   "area_code_geomatch": true,
   "status_callback": null,
   "links": {
      ...
   }
}

Add Phone number to Messaging Service

Now that you have a Messaging Service you can use the Services API to add your phone number to your Messaging Service.

Request

curl -X POST https://messaging.twilio.com/v1/Services/<INSERT YOUR MESSAGING SERVICE SID/PhoneNumbers \
--data-urlencode "PhoneNumberSid=<INSERT PHONE NUMBER SID>" \
-u <INSERT YOUR SUBACCOUNT SID>:<INSERT YOUR SUBACCOUNT AUTH TOKEN>

Response

You have now successfully added the phone number to your Messaging Service.

{
   "phone_number": "+14158740103",
   "date_updated": "2021-02-17T15:30:47Z",
   "capabilities": [
       "MMS",
       "SMS",
       "Voice"
   ],
   "account_sid": "AC1bcc67e32e161e04aaacc159b2e86601",
   "url": "https://messaging.twilio.com/v1/Services/MGxxx/PhoneNumbers/PNxxx",
   "country_code": "US",
   "sid": "PNxxxx",
   "date_created": "2021-02-17T15:30:47Z",
   "service_sid": "MGxxxx"
}

Sending a Message

Now that you have a new subaccount, a Messaging Service with a phone number lets send a message!

To send a message through the Messaging Service, use the Messaging Service SID (MGxxx) in your request.

Make sure the To phone number is verified. If your number isn’t verified you can add a verified phone number via the Twilio Console.

Request

curl -X POST https://api.twilio.com/2010-04-01/Accounts/<INSERT YOUR SUBACCOUNT SID>/Messages.json \
--data-urlencode "Body=Hi there" \
--data-urlencode "From=<INSERT YOUR MESSAGING SERVICE SID>
" \
--data-urlencode "To=<INSERT YOUR PHONE NUMBER E.164 FORMAT>" \
-u <INSERT YOUR SUBACCOUNT SID>:<INSERT YOUR SUBACCOUNT AUTH TOKEN>
{
   "sid": "SMb319786dcd44433b905d0e8acd2b33f3",
   "date_created": "Wed, 17 Feb 2021 15:39:56 +0000",
   "date_updated": "Wed, 17 Feb 2021 15:39:56 +0000",
   "date_sent": null,
   "account_sid": "ACxxx",
   "to": "+12083186206",
   "from": null,
   "messaging_service_sid": "MGxxx",
   "body": "Hi there",
   "status": "accepted",
   "num_segments": "0",
   "num_media": "0",
   "direction": "outbound-api",
   "api_version": "2010-04-01",
   "price": null,
   "price_unit": null,
   "error_code": null,
   "error_message": null,
   "uri": "/2010-04-01/Accounts/ACxxx/Messages/SMb319786dcd44433b905d0e8acd2b33f3.json",
   "subresource_uris": {
       "media": "/2010-04-01/Accounts/ACxxx/Messages/SMb319786dcd44433b905d0e8acd2b33f3/Media.json"
   }
}

Upon success, you should receive a text like the one below:

Text demonstration from a subaccount

Awesome it worked!

Conclusion: subaccounts are powerful

Lets recap everything you did:

  1. Created a subaccount
  2. Transferred a phone number from your parent account to your subaccount
  3. Created a Messaging Service in your subaccount
  4. Added your phone number to the Messaging Service
  5. Sent a message via your Messaging Service in your subaccount

Hopefully you see the power of subaccounts! This article only scratched the surface. If you liked this blog you might be interested in the following:

Josh is a Senior Solutions Engineer at Twilio, focusing on helping our Independent Software Vendor (ISV) Partners build scalable architectures and business on Twilio. You can reach him at jsiverson [at] twilio.com.