Get Started

Using Twilio Subaccounts

You can use subaccounts to segment your Twilio usage into separate buckets, facilitating account management and reporting. If you intend to build an application that serves multiple user accounts, you can create subaccounts for each user to keep his or her activity separate. Or, if you're using a single Twilio account for multiple projects, you can use subaccounts to manage the usage of each project individually.

The Goal

When a new user signs up for your application, create a new subaccount for his or her Twilio activity. Link the credentials of the subaccount to your application's user information, provision a phone number for the subaccount, and then place a call from the subaccount.

Concepts

This HowTo demonstrates the creation of Twilio subaccounts using the Accounts list resource. The HowTo also shows how to purchase numbers for the subaccount using the IncomingPhoneNumbers list resource, as well as how to place a call using the Calls list resource.

Download

subaccounts.zip

Implementation

  • 1

    • After a new user registers with your application, create a new Twilio subaccount and link that subaccount's AccountSid and AuthToken to the local user account.

      • howtos/subaccounts/subaccounts.php
            // Include the PHP TwilioRest library 
        	require "Services/Twilio.php";
        	date_default_timezone_set("America/Los_Angeles");
            
            // Set our AccountSid and AuthToken 
            $ParentAccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            $ParentAuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
            
            $client = new Services_Twilio($ParentAccountSid, $ParentAuthToken);
            
        	// Mock data for our newly created local application user
        	$local_user = array("id"=>100, "first_name"=>"John", "last_name"=>"Smith", "email"=>"john@smith.com", "twilio_account_sid"=>"", "twilio_auth_token"=>"");
        
        	$client = new Services_Twilio('AC123', '123');
        	$subaccount = $client->accounts->create(array(
            	"FriendlyName" => $local_user['email']
        	));
        	
        	echo "<p>Created subaccount: {$subaccount->sid}</p>";
        	//update and save our local_user
        	$local_user['twilio_account_sid'] = $subaccount->sid;
        	$local_user['twilio_auth_token'] = $subaccount->auth_token;
        	//$local_user.save();
        	
            
  • 2

    • Purchase a number with your subaccount. You can purchase phone numbers under your subaccount, so you can easily keep track of which phone numbers belong to which subaccount. Call and SMS usage for that phone number are associated with that subaccount. Buying a phone number with a subaccount is easy, just use the Twilio subaccount's Sid in the URI of the request to purchase the number:

      • howtos/subaccounts/subaccounts.php
            // Purchase a number for the subaccount
        	
        	$SubAccountSid=$local_user['twilio_account_sid'];
        	$SubAccountAuthToken=$local_user['twilio_auth_token'];
        	// create a new Twilio client for subaccount
        	$subaccount_client = new Services_Twilio($SubAccountSid, $SubAccountAuthToken);
        
        	$number = $subaccount_client->account->incoming_phone_numbers->create(array(
        		"AreaCode" => "858",
        	));
        	
             $PhoneNumber = $number->phone_number;
        	 echo "<p>Purchased phone number $PhoneNumber</p>";
        
        	
            
  • 3

    • Make a phone call with your subaccount's new phone number. The only difference between a standard Twilio Call request is the subaccount credentials to process the REST API request.
    • Check out the Quickstart for more info on Making Outgoing Calls

      • howtos/subaccounts/subaccounts.php
        
        	// Place a call from the subaccount
        	/* Outgoing Caller ID you have previously validated with Twilio */
        	$from = 'NNNNNNNNNN';
         
        	/* Outgoing Number you wish to call */
        	$to = 'MMMMMMMMMM';
         
        	/* Callback Url responsing with TwiML when the call is answered */
        	$url = 'http://twimlets.com/message?Message%5B0%5D=Hello%20from%20your%20subaccount';
         
        	/* make Twilio REST request to initiate outgoing call */
        	$call = $client->account->calls->create($from, $to, $url);
        	 
            

And that's it! The call will be recorded in the Call records for the subaccount. This is exactly the same for sending outgoing SMS messages with subaccounts