Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Verifying Caller IDs at Scale


What if you need to make calls in a subaccount using a Twilio phone number provisioned in your Master account? You'll need to verify those outgoing caller IDs with the Twilio account or subaccount you are using.

This guide will be helpful if you have a large number of phone numbers that you need to verify programmatically. If you need to verify a mobile phone or landline number, it may be easier to do so through the Twilio console.


What this guide covers

what-this-guide-covers page anchor

This guide covers the process you would use to programmatically verify many phone numbers with Twilio. The specifics of the implementation are going to be different, based on the API provider you are using for the phone numbers.

In general, you will start with a list of phone numbers to verify. Make a call to the Twilio API with each phone number to start the verification process. Twilio will respond with a unique six digit verification code for each number.

Twilio will then make a phone call to the phone number from another provider, and that phone number should respond with the six digit verification code. After Twilio either receives or doesn't receive the correct code, a separate status callback request is made to a web URL of your choosing that contains the verification result for that phone number - either success or failure.

This guide uses Twilio Sync to share state between different systems - but you may use another data store, such as Redis or a database. It also uses a Twilio Function to store verification status in Twilio Sync.


Verifying outgoing caller IDs with Twilio

verifying-outgoing-caller-ids-with-twilio page anchor

Using the Outgoing Caller IDs Resource, you can create a validation request for a phone number. Twilio will return a six digit verification code to you in the response to the create request (synchronously). Twilio will then call that phone number on the PSTN, with a caller ID of +14157234000.

We also set the status callback, so that we can get the verification status for each phone number.

Verify an Outgoing Caller ID

verify-an-outgoing-caller-id page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_14
// Download the helper library from https://www.twilio.com/docs/node/install
_14
// Find your Account SID and Auth Token at twilio.com/console
_14
// and set the environment variables. See http://twil.io/secure
_14
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14
const authToken = process.env.TWILIO_AUTH_TOKEN;
_14
const client = require('twilio')(accountSid, authToken);
_14
_14
client.validationRequests
_14
.create({
_14
friendlyName: 'Third Party VOIP Number',
_14
statusCallback: 'https://somefunction.twil.io/caller-id-validation-callback',
_14
phoneNumber: '+14158675310'
_14
})
_14
.then(validation_request => console.log(validation_request.friendlyName));

Output

_10
{
_10
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"call_sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"friendly_name": "Third Party VOIP Number",
_10
"phone_number": "+14158675310",
_10
"validation_code": "111111"
_10
}

From your non-Twilio phone number (or from a Twilio number not associated with the current account), you will need to answer the incoming call from Twilio. This typically involves configuring a webhook with the third party, however you may be using an incoming voice call webhook on that phone number already. If that's the case, you can either add some logic to the webhook to respond to the inbound Twilio phone number (+14157234000), or temporarily replace the webhook with one that replies back to the Twilio validation request phone call.

When you send the validation request, you will get a six digit verification code in the response. You will have to get that verification code to the webhook that responds to the Twilio incoming phone call. One way to do this would be to use a key/value store like Redis. Another way would be to use Twilio Sync to store the validation code.


Using Twilio Sync to store validation information

using-twilio-sync-to-store-validation-information page anchor

Twilio Sync provides shared state in the cloud, so you can store the verification codes in Sync after calling the Verify Outgoing Callers API, and then access those access codes from your phone number's webhook (which probably isn't running on the same system as a script that verifies caller ids). There are two steps - the first is to set up a sync map to store information by phone number. For now, we are only going to store the verification code. Later, we will store verification information, once we retrieve it from the status callback.

When you work with Twilio Sync, you can either use a client SDK (for the web browser with Javascript, iOS, or Android), or the Twilio REST API. The Twilio REST API has helper libraries for Java, C#, Python, Ruby, PHP, and Node.js/Javascript. You will want to use the REST API or the related helper libraries for most of this project, but if you were to build a web application dashboard to show results, you may want to look into the client-side Javascript library for Sync.

We can use the default Sync service - if you are using Sync already, you can also create a new Sync Service from the console or API, and use that. When we create the Sync Map, we give it a unique name, "OutgoingCallerIds" - this can also be whatever you would like to use.

Create a sync map for verifying caller ids

create-a-sync-map-for-verifying-caller-ids page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_11
// Download the helper library from https://www.twilio.com/docs/node/install
_11
// Find your Account SID and Auth Token at twilio.com/console
_11
// and set the environment variables. See http://twil.io/secure
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client.sync.v1.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11
.syncMaps
_11
.create({uniqueName: 'OutgoingCallerIds'})
_11
.then(sync_map => console.log(sync_map.sid));

Output

_16
{
_16
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_16
"created_by": "created_by",
_16
"date_expires": "2015-07-30T21:00:00Z",
_16
"date_created": "2015-07-30T20:00:00Z",
_16
"date_updated": "2015-07-30T20:00:00Z",
_16
"links": {
_16
"items": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items",
_16
"permissions": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Permissions"
_16
},
_16
"revision": "revision",
_16
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_16
"sid": "MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_16
"unique_name": "OutgoingCallerIds",
_16
"url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_16
}

After creating a Sync Map with the REST API (you will only need to do this once), the next step will be to store the verification code from Twilio into that Sync Map. Sync Maps store Map Items, which consist of a data object accessed by a key. The key is a string, and should be unique - the phone number works perfectly as a key. The data can be up to 16 kilobytes in size, and should be structured as key value pairs. We can store the verification code with the key verification_code. and store the six digit verificiation code as the value.

Store a verification code with Twilio Sync

store-a-verification-code-with-twilio-sync page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_14
// Download the helper library from https://www.twilio.com/docs/node/install
_14
// Find your Account SID and Auth Token at twilio.com/console
_14
// and set the environment variables. See http://twil.io/secure
_14
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14
const authToken = process.env.TWILIO_AUTH_TOKEN;
_14
const client = require('twilio')(accountSid, authToken);
_14
_14
client.sync.v1.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14
.syncMaps('MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14
.syncMapItems
_14
.create({key: '+14158675310', data: {
_14
verification_code: '123456'
_14
}})
_14
.then(sync_map_item => console.log(sync_map_item.key));

Output

_15
{
_15
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_15
"created_by": "created_by",
_15
"data": {
_15
"verification_code": "123456"
_15
},
_15
"date_expires": "2015-07-30T21:00:00Z",
_15
"date_created": "2015-07-30T20:00:00Z",
_15
"date_updated": "2015-07-30T20:00:00Z",
_15
"key": "+14158675310",
_15
"map_sid": "MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_15
"revision": "revision",
_15
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_15
"url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items/key"
_15
}


Responding to the Twilio Verification Phone Call

responding-to-the-twilio-verification-phone-call page anchor

Depending on your third-party provider, how you respond to an incoming phone call will differ. For instance, your provider may offer a webhook solution that calls a web URL of your choosing with an HTTP POST or GET request.

Your response should be based on the incoming phone number - +14157234000 is the phone number that Twilio will be using to verify your phone number, so you can play back the proper six digit validation code with DTMF.

When your phone number receives the validation request, your program will need to look up that verification code. For instance, if you were using Redis as the data storage, the phone number would likely be the key. If you use Twilio Sync, you can fetch a Map Item from a Sync Map, based on the phone number as the key.

Retrieving the Verification Code from Twilio Sync.

retrieving-the-verification-code-from-twilio-sync page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_12
// Download the helper library from https://www.twilio.com/docs/node/install
_12
// Find your Account SID and Auth Token at twilio.com/console
_12
// and set the environment variables. See http://twil.io/secure
_12
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_12
const authToken = process.env.TWILIO_AUTH_TOKEN;
_12
const client = require('twilio')(accountSid, authToken);
_12
_12
client.sync.v1.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_12
.syncMaps('MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_12
.syncMapItems('+14158675310')
_12
.fetch()
_12
.then(sync_map_item => console.log(sync_map_item.key));

Output

_13
{
_13
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_13
"created_by": "created_by",
_13
"data": {},
_13
"date_expires": "2015-07-30T21:00:00Z",
_13
"date_created": "2015-07-30T20:00:00Z",
_13
"date_updated": "2015-07-30T20:00:00Z",
_13
"key": "+14158675310",
_13
"map_sid": "MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_13
"revision": "revision",
_13
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_13
"url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items/key"
_13
}

The map item will contain the verification code in the data object. Read that verification code, and then play it back to Twilio, and your phone number will be verified!


Receiving verification status in a callback

receiving-verification-status-in-a-callback page anchor

After your phone number gets a verification call from Twilio, you can receive a status callback from Twilio to record the verification status for that phone number. This webhook will contain two parameters we are interested in - VerificationStatus and To (the number being verified), as well as the other parameters included in a TwiML Voice Request.

Because you may be verifying many numbers, there may be some cases where the verification status fails, and you have to retry those numbers. For those cases, it's helpful to have a record of which verifications were successful, and which were not.

If possible, record the verification status in logs, but also into a key/value store or database, so it is easy to get a list of failed verifications back. Again, we can use Twilio Sync for this.


Recording verification status with Twilio Sync

recording-verification-status-with-twilio-sync page anchor

We will update the Map Item for our phone number in our OutgoingCallerIds Map. You will need the original key used for the map item - this would be the phone number, in the same format as you stored it. Then you can pass in a new data object - in this case, it would look like {"verification_status":true}.

If you like, you could also use another Map, with a different unique name, and then create a Map Item in that Map for the verification status.

Recording verification status with Twilio Sync

recording-verification-status-with-twilio-sync-1 page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_14
// Download the helper library from https://www.twilio.com/docs/node/install
_14
// Find your Account SID and Auth Token at twilio.com/console
_14
// and set the environment variables. See http://twil.io/secure
_14
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_14
const authToken = process.env.TWILIO_AUTH_TOKEN;
_14
const client = require('twilio')(accountSid, authToken);
_14
_14
client.sync.v1.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14
.syncMaps('MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_14
.syncMapItems('+14158675310')
_14
.update({data: {
_14
verification_status: 'true'
_14
}})
_14
.then(sync_map_item => console.log(sync_map_item.key));

Output

_15
{
_15
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_15
"created_by": "created_by",
_15
"data": {
_15
"verification_status": "true"
_15
},
_15
"date_expires": "2015-07-30T21:00:00Z",
_15
"date_created": "2015-07-30T20:00:00Z",
_15
"date_updated": "2015-07-30T20:00:00Z",
_15
"key": "+14158675310",
_15
"map_sid": "MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_15
"revision": "revision",
_15
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_15
"url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items/key"
_15
}

If you would like, the status callback for the caller id verification could be a Twilio Function. Functions is Twilio's serverless environment, where you can run Javascript code in a Node.js environment. You would need to set the SYNC_SERVICE_SID and VERIFY_CALLER_ID_SYNC_MAP_SID variables on the Function Credentials page(link takes you to an external page) in the Twilio Console. Then create a new Twilio Function in the Runtime Console(link takes you to an external page) and add the Javascript source code from the Twilio Function for storing verification status in Twilio Sync code sample.

Save the Twilio Function, and then it will automatically deploy. Now just supply that URL as the status callback when you verify the Twilio phone number.

Twilio Function for storing verification status in Twilio Sync

twilio-function-for-storing-verification-status-in-twilio-sync page anchor

_22
exports.handler = function(context, event, callback) {
_22
console.log('Incoming webhook', event);
_22
_22
let client = context.getTwilioClient();
_22
_22
let verificationStatus = event['VerificationStatus'];
_22
let phoneNumber = event['To'];
_22
_22
client.sync.services(context.SYNC_SERVICE_SID)
_22
.syncMaps(context.VERIFY_CALLER_ID_SYNC_MAP_SID)
_22
.syncMapItems(phoneNumber)
_22
.update({data: {
_22
verification_status: verificationStatus
_22
}})
_22
.then(sync_map_item => {
_22
console.log(sync_map_item.data);
_22
callback(null, 'OK');
_22
}).catch( error => {
_22
console.log(error);
_22
callback(null, error);
_22
});
_22
};


Retrieving verification status from Twilio Sync

retrieving-verification-status-from-twilio-sync page anchor

With the verification status stored in Sync, we can make another request to retrieve all of the map items from the Sync Map. With that request, you could then print out the results from a command line, or you could create a web-based dashboard. If you do create a web-based dashboard, you can update the verification status in real time as your script runs with the client side Javascript SDK for Sync.

The REST API for fetching all Map Items for a Map is paginated - if you have more than 50 numbers to verify, you will need to paginate through the list. For more on working with map items, see the Map Item Resource documentation.

Retrieving verification status from Twilio Sync

retrieving-verification-status-from-twilio-sync-1 page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_12
// Download the helper library from https://www.twilio.com/docs/node/install
_12
// Find your Account SID and Auth Token at twilio.com/console
_12
// and set the environment variables. See http://twil.io/secure
_12
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_12
const authToken = process.env.TWILIO_AUTH_TOKEN;
_12
const client = require('twilio')(accountSid, authToken);
_12
_12
client.sync.v1.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_12
.syncMaps('MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_12
.syncMapItems
_12
.list({limit: 20})
_12
.then(syncMapItems => syncMapItems.forEach(s => console.log(s.key)));

Output

_26
{
_26
"items": [
_26
{
_26
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_26
"created_by": "created_by",
_26
"data": {},
_26
"date_expires": "2015-07-30T21:00:00Z",
_26
"date_created": "2015-07-30T20:00:00Z",
_26
"date_updated": "2015-07-30T20:00:00Z",
_26
"key": "key",
_26
"map_sid": "MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_26
"revision": "revision",
_26
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_26
"url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items/key"
_26
}
_26
],
_26
"meta": {
_26
"first_page_url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0",
_26
"key": "items",
_26
"next_page_url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=1",
_26
"page": 0,
_26
"page_size": 50,
_26
"previous_page_url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0",
_26
"url": "https://sync.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Maps/MPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Items?From=from&Bounds=inclusive&Order=asc&PageSize=50&Page=0"
_26
}
_26
}


After verifying your phone numbers with this account, you should be able to make outgoing calls with Twilio using these numbers for the caller ID.

Here are some additional documentation resources that can help you with this project:


Rate this page: