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

How to route calls to your SIP network with an outbound call


(information)

Info

If you are looking to explore SIP functionality, we recommend following the SIP Quickstart to get you up and running in a few clicks!

Are you interested in learning how to make calls using SIP with Twilio programmability? This guide will show you how to use Programmable Voice(link takes you to an external page) to make SIP outbound phone calls from Twilio to your Twilio Registered SIP Endpoints.

It's possible to connect an existing Twilio Voice Application directly to your SIP network. You can do this by requesting Twilio make a SIP call from either an existing incoming call, or from an outbound API request. Not only does this give you access to all the powerful Programmable Voice solutions, but it also allows you to lean on the savings by not routing your call through normal PSTN.


Steps to route calls to your SIP Network

steps-to-route-calls-to-your-sip-network page anchor
  1. Buy a Twilio Number
  2. Configure SIP Registration
  3. Configure your SIP Endpoint
  4. Make an outbound SIP call

Let's get started!


In the Twilio console(link takes you to an external page), search for and purchase an available phone number capable of making outbound calls. You'll use this phone number as the "From" phone number when you initiate an outbound call.

Search for voice capable numbers.

Configure SIP Registration

configure-sip-registration page anchor

Note: If you are not using Twilio Registered Endpoint then this step is not applicable to you.

In order to make and receive phone calls to a SIP phone, like Zoiper, SIP Registration is required. This enables routing the calls to SIP phone/Endpoint. In this tutorial, we are using a Twilio Registered Endpoint to receive the call. This guide will show how to enable registration and setup credentials.

  1. Configure Credential List

    1. Log in to your twilio account go to programmable voice. Select " credential lists(link takes you to an external page) "
    2. Click the "+" icon to create credential list.(link takes you to an external page)
    3. Add a Friendly Name as Endpoint , Username (this can be E164 number, Extention number or name) as UserA and Password as yourpassword and click Create. Note: The credential username and password created here will be used on your SIP Endpoints for authentication.
  2. Configure SIP Domain

    1. Select " Domains(link takes you to an external page) "
    2. Click the "+" icon to create SIP Domain(link takes you to an external page) .
    3. Add a Friendly Name as T1 and a unique SIP URI as Trunk1 . Note: SIP URI names needs to be unique, I have used Trunk1 in my example. You might have to find a name that is available
    4. Scroll down to "Voice Authentication" and associate the "Credential Lists" Endpoint that you created
    5. Scroll down to "SIP Registration" and click "Enabled"
    6. In "SIP Registration Authentication" associate the "Credential Lists" that you created. Then click "Save"

Note: If you are not using Twilio Registered Endpoint then this step is not applicable to you.

A SIP Endpoint can be desk phone or soft phone. In this guide, we will use the soft phone and will configure the phone to successfully register to SIP Registrar.

  1. Download and install SIP Endpoint. Zoiper(link takes you to an external page) is used for example
  2. Provide login name - UserA@Trunk1.sip.us1.twilio.com (do add us1 region parameter to your sip domain) and password

  3. Click "Next/continue" to confirm the Domain(link takes you to an external page)
  4. Optional settings can be skipped
  5. Done! You see in Zoiper that it is "Registered" and has "Tick" next to your login name.
  6. You can also verify the successfully registered endpoints under your SIP domain > Registered SIP Endpoints in the Twilio console
Reg.

Make an outbound SIP call

make-an-outbound-sip-call page anchor

There are a couple of ways that you can make an outbound SIP call from Twilio. Remember this outbound call that Twilio makes is what will connect with your SIP Network.

Make an outbound SIP call using an inbound call handler

make-an-outbound-sip-call-using-an-inbound-call-handler page anchor

It's possible to have Twilio run code when a call is received. For our example, we will have that code make an outbound SIP call to our network.

We will now write the TwiML for our application. Because this is a static application, we will use a TwiML Bin. Visit the TwiML Bin(link takes you to an external page) page and click the + icon to add a new bin.

Set the Friendly Name as SIP Outbound call and copy and paste the TwiML below


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Say>Welcome to xyz.com. Your call will be routed to an agent now</Say>
_10
<Dial>
_10
<Sip>sip:UserA@Trunk1.sip.us1.twilio.com</Sip>
_10
</Dial>
_10
</Response>

(information)

Info

Make sure to add us1 region parameter as part of your SIP domain

With our TwiML Bin created, now we need to wire it up to our number.

  1. Go to your Twilio Number(link takes you to an external page) page
  2. Click the Number you have purchased
  3. Scroll down to "Voice" under "A Call Comes in" select your configured TwiML Bin as SIP Outbound call from the dropdown menu and click "Save"
URL.

Take your cellphone and dial the Twilio Number(link takes you to an external page) you purchased. You should hear a notification that you are being transferred and then an outbound SIP call will be made. Congratulations!

Make an outbound SIP call using API

make-an-outbound-sip-call-using-api page anchor

Another way to make an outbound call is by using the REST API. Here we will create a Call resource and point it directly to our SIP client.

Retrieve your Twilio account credentials

retrieve-your-twilio-account-credentials page anchor

First, you'll need to get your Twilio account credentials. They consist of your AccountSid and your Auth Token. They can be found on the home page of the console(link takes you to an external page).

Retrieve Your Twilio Credentials.

There are a few key parameters to drill into when making the outbound call.

  • "From" - the username or number you are calling from
  • "To" - login name of your SIP Endpoint (Zoiper). For example, sip:UserA@Trunk1.sip.us1.twilio.com (do add us1 as a region parameter to your SIP Domain)
  • "Twiml" - The TwiML instructions on what should happen when the other party picks up the phone.

Visit the TwiML Bin(link takes you to an external page) page and click the + icon to add a new bin.

Set the Friendly Name as Notification and copy and paste the TwiML below


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Say>Well done you have successfully made an outbound SIP call from Twilo using an API</Say>
_10
</Response>

Make an outbound SIP call using API

make-an-outbound-sip-call-using-api-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.calls
_14
.create({
_14
twiml: '<Response><Say>Hello and thanks for connecting to your SIP network!</Say></Response>',
_14
to: 'sip:UserA@Trunk1.sip.us1.twilio.com',
_14
from: '+15017122661'
_14
})
_14
.then(call => console.log(call.sid));

Output

_37
{
_37
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"answered_by": null,
_37
"api_version": "2010-04-01",
_37
"caller_name": null,
_37
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_37
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_37
"direction": "inbound",
_37
"duration": "15",
_37
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_37
"forwarded_from": "+141586753093",
_37
"from": "+15017122661",
_37
"from_formatted": "(501) 712-2661",
_37
"group_sid": null,
_37
"parent_call_sid": null,
_37
"phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"price": "-0.03000",
_37
"price_unit": "USD",
_37
"sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_37
"status": "completed",
_37
"subresource_uris": {
_37
"notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json",
_37
"recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json",
_37
"payments": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Payments.json",
_37
"events": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Events.json",
_37
"siprec": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Siprec.json",
_37
"streams": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Streams.json",
_37
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessageSubscriptions.json",
_37
"user_defined_messages": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessages.json"
_37
},
_37
"to": "sip:UserA@Trunk1.sip.us1.twilio.com",
_37
"to_formatted": "sip:UserA@Trunk1.sip.us1.twilio.com",
_37
"trunk_sid": null,
_37
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
_37
"queue_time": "1000"
_37
}

Run the code. You should hear a notification that you have successfully made an outbound SIP call. Congratulations!


Great work!

Happy hacking!


Rate this page: