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 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.
Let's get started!
In the Twilio console, 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.
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.
Configure Credential List
Endpoint
, Username (this can be E164 number, Extension 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.
Configure SIP Domain
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
Endpoint
that you created
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.
Provide login name - UserA@Trunk1.sip.us1.twilio.com
(do add us1 region parameter to your sip domain) and password
Trunk1.sip.us1.twilio.com
(do add
us1
region parameter to your sip domain while configuring zoiper)
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.
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 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
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Say>Welcome to xyz.com. Your call will be routed to an agent now</Say>4<Dial>5<Sip>sip:UserA@Trunk1.sip.us1.twilio.com</Sip>6</Dial>7</Response>8
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.
SIP Outbound call
from the dropdown menu and click "Save"
Take your cellphone and dial the Twilio Number you purchased. You should hear a notification that you are being transferred and then an outbound SIP call will be made. Congratulations!
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.
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.
There are a few key parameters to drill into when making the outbound call.
sip:UserA@Trunk1.sip.us1.twilio.com
(do add
us1
as a region parameter to your SIP Domain)
Visit the TwiML Bin page and click the +
icon to add a new bin.
Set the Friendly Name as Notification
and copy and paste the TwiML below
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Say>Well done you have successfully made an outbound SIP call from Twilo using an API</Say>4</Response>
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createCall() {11const call = await client.calls.create({12from: "+15017122661",13to: "sip:UserA@Trunk1.sip.us1.twilio.com",14twiml:15"<Response><Say>Hello and thanks for connecting to your SIP network!</Say></Response>",16});1718console.log(call.sid);19}2021createCall();
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"annotation": null,4"answered_by": null,5"api_version": "2010-04-01",6"caller_name": null,7"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",8"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",9"direction": "inbound",10"duration": "15",11"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",12"forwarded_from": "+141586753093",13"from": "+15017122661",14"from_formatted": "(415) 867-5308",15"group_sid": null,16"parent_call_sid": null,17"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"price": "-0.03000",19"price_unit": "USD",20"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",22"status": "completed",23"subresource_uris": {24"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",25"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",26"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",27"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",28"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",29"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",30"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",31"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",32"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"33},34"to": "sip:UserA@Trunk1.sip.us1.twilio.com",35"to_formatted": "(415) 867-5309",36"trunk_sid": null,37"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",38"queue_time": "1000"39}
Run the code. You should hear a notification that you have successfully made an outbound SIP call. Congratulations!
Great work!
Happy hacking!