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

TwiML™ Voice: <Sip>


The <Dial> verb's <Sip> noun lets you set up VoIP sessions by using SIP — Session Initiation Protocol. With this feature, you can send a call to any SIP endpoint. Set up your TwiML to use the <Sip> noun within the <Dial> verb whenever any of your Twilio phone numbers are called. If you are unfamiliar with SIP, or want more information on how Twilio works with your SIP endpoint, please see the SIP overview.


The SIP session

the-sip-session page anchor

The SIP INVITE message includes the API version, the AccountSid, and CallSid for the call. Optionally, you can also provide a set of parameters to manage signaling transport and authentication, or configure Twilio to pass custom SIP headers in the INVITE message: this method includes headers such as UUI (User-to-user Information).

Once the SIP session completes, Twilio requests the <Dial> action URL, passing along the SIP CallID header, the response code of the invite attempt, any X-headers passed back on the final SIP response, as well as the standard Twilio <Dial> parameters.

If Enhanced Programmable SIP Features(link takes you to an external page) is not enabled on your account, only one <Sip> noun may be specified per <Dial>, and the INVITE message may be sent to only one SIP endpoint. Also, you cannot add any other nouns (eg <Number>, <Client>) in the same <Dial> as the SIP. If you want to use another noun, set up a callback on the <Dial> to use alternate methods.

The region parameter

sip-uri-region page anchor

To specify the geographic region from which Twilio will send SIP-out traffic towards your communication infrastructure, you must include the region parameter in your SIP URI. For example, if the region=ie1 parameter is included in your SIP URI, Twilio will send the SIP traffic from the Europe Ireland region:


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>sip:alice@example.com;region=ie1</Sip>
_10
</Dial>
_10
</Response>

RegionLocation
us1North America Virginia
us2North America Oregon
ie1Europe Ireland
de1Europe Frankfurt
sg1Asia Pacific Singapore
jp1Asia Pacific Tokyo
br1South America São Paulo
au1Asia Pacific Sydney

If the region parameter is not specified, Twilio will send SIP-out traffic from the North America Virginia region.

Use the Sip noun

sip-noun page anchor

All of the existing <Dial> parameters work with the <Sip> noun (record, timeout, hangupOnStar, etc). For SIP calls, the callerId attribute does not need to be a validated phone number. Enter any alphanumeric string. Optionally include the following chars: +-_., but no whitespace.

Within the <Sip> noun, you must specify a URI for Twilio to connect to. The URI should be a valid SIP URI under 255 characters. For example:

Use the <Sip> noun

use-the-sip-noun page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.sip('sip:jack@example.com');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>
_10
sip:jack@example.com
_10
</Sip>
_10
</Dial>
_10
</Response>

Send username and password attributes for authentication to your SIP infrastructure as attributes on the <Sip> noun.

Attribute NameValues
usernameUsername for SIP authentication
passwordPassword for SIP authentication

For example:

Authentication with your <Sip>

authentication-with-your-sip page anchor
Node.js
Python
C#
Java
PHP
Ruby

_11
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_11
_11
_11
const response = new VoiceResponse();
_11
const dial = response.dial();
_11
dial.sip({
_11
username: 'admin',
_11
password: '1234'
_11
}, 'sip:kate@example.com');
_11
_11
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip username="admin" password="1234">sip:kate@example.com</Sip>
_10
</Dial>
_10
</Response>

Send custom headers by appending them to the SIP URI — just as you'd pass headers in a URI over HTTP. For example:

Custom headers with <Sip>

custom-headers-with-sip page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.sip('sip:jack@example.com?x-mycustomheader=foo&x-myotherheader=bar');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>
_10
sip:jack@example.com?x-mycustomheader=foo&amp;x-myotherheader=bar
_10
</Sip>
_10
</Dial>
_10
</Response>

While the SIP URI itself must be under 255 chars, the headers must be under 1024 characters. Any headers starting with the x- prefix can be sent this way.

You can also send multiple parameters and values as part of the x- header


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>sip:jack@example.com?x-customname=Madhu%2CMathiyalagan%3BTitle%3DManager&amp;x-myotherheader=bar</Sip>
_10
</Dial>
_10
</Response>

UUI (User-to-User Information) header can be sent without prepending x-


_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>sip:jack@example.com?User-to-User=123456789%3Bencoding%3Dhex&amp;x-myotherheader=bar</Sip>
_10
</Dial>
_10
</Response>

Set a parameter on your SIP URI to specify what transport protocol you want to use. Currently, this is limited to UDP, TCP and TLS. By default, Twilio sends your SIP INVITE over UDP. Change this by using the transport parameter:

Transport with <Sip>

transport-with-sip page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.sip('sip:jack@example.com;transport=tcp');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>
_10
sip:jack@example.com;transport=tcp
_10
</Sip>
_10
</Dial>
_10
</Response>

Alternatively, you may customize it to use TLS for SIP signaling. When using TLS, the default port will be 5061 however, a different port may be specified.

TLS Transport with <Sip>

tls-transport-with-sip page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.sip('sip:jack@example.com;transport=tls');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>
_10
sip:jack@example.com;transport=tls
_10
</Sip>
_10
</Dial>
_10
</Response>

<Sip> Call parameters

sip-call-parameter page anchor

When a SIP call is answered, Twilio passes the following parameters with its request in addition to the standard TwiML [Voice request parameters][request parameters]:

ParameterValues
CalledTo header of the SIP Invite message. The SIP identifier of the called party.
CallerFrom header of the SIP Invite message. The SIP identifier of the party that initiated the call.
SipCallIdThe SIP call ID header of the request made to the remote SIP infrastructure.
SipDomainThe host part of the SIP request.
SipDomainSidYour SIP Domain ID. It is 34 characters long, and always starts with the letters SD.
SipHeader_The name/value of any X-headers returned in the 200 response to the SIP INVITE request. This is applicable only if you are using SIP [custom headers][custom-headers].
SipSourceIpSource IP address for SIP signaling.

Additional parameters

sip-action page anchor

When you invoke [dial action][dial-action] attribute and <Sip>, Twilio passes the following parameters with its request in addition to the standard [dial action][dial-action] parameters. Use the action callback parameters to modify your application based on the results of the SIP dial attempt:

ParameterValues
DialSipCallIdThe SIP call ID header of the request made to the remote SIP infrastructure.
DialSipResponseCodeThe SIP response code as a result of the INVITE attempt.
DialSipHeader_The name/value of any X-headers returned in the final response to the SIP INVITE request.

<Sip> Attributes

attributes page anchor

The <Sip> noun supports the following attributes that modify its behavior:

Attribute NameAllowed ValuesDefault Value
methodGET, POSTPOST
[password][authentication]Password for SIP authentication
statusCallbackEventinitiated, ringing, answered, completednone
statusCallbackany urlnone
statusCallbackMethodGET, POSTPOST
urlcall screening urlnone
[username][authentication]Username for SIP authentication
machineDetectionEnable, DetectMessageEndNone
machineDetectionTimeout3-6030
machineDetectionSpeechThreshold1000-60002400
machineDetectionSpeechEndThreshold500-50001200
machineDetectionSilenceTimeout2000-100005000
amdStatusCallbackAny URLNone
amdStatusCallbackMethodGET, POSTPOST

The url attribute allows you to specify a url for a TwiML document that runs on the called party's end, after they answer, but before the two parties are connected. You can use this TwiML to privately <Play> or <Say> information to the called party, or provide a chance to decline the phone call using <Gather> and <Hangup>. If [answerOnBridge][dial-answer-on-bridge] attribute is used on <Dial>, the current caller will continue to hear ringing while the TwiML document executes on the other end. TwiML documents executed in this manner are not allowed to contain the <Dial> verb.

The method attribute allows you to specify which HTTP method Twilio should use when requesting the URL specified in the url attribute. The default is POST.

When dialing out to a number using <Dial>, an outbound call is initiated. The call transitions from the initiated state to the ringing state when the phone starts ringing. It transitions to the answered state when the call is picked up, and finally to the completed state when the call is over. With statusCallbackEvent, you can subscribe to receive webhooks for the different call progress events: initiated, ringing, answered, or completed for a given call.

The statusCallbackEvent attribute allows you to specify which events Twilio should webhook on. To specify multiple events separate them with a space: initiated ringing answered completed. If a statusCallback is provided and no status callback events are specified the completed event will be sent by default.

As opposed to creating an outbound call via the API, outbound calls created using <Dial> are initiated right away and never queued. The following shows a timeline of possible call events that can be returned and the different call statuses that a <Dial> leg may experience:

Outbound Dial call events diagram.
EventDescription
initiatedThe initiated event is fired when Twilio starts dialing the call.
ringingThe ringing event is fired when the call starts ringing.
answeredThe answered event is fired when the call is answered.
completedThe completed event is fired when the call is completed, regardless of the termination status: busy, canceled, completed, failed, or no-answer. If no StatusCallbackEvent is specified, completed will be fired by default.

The statusCallback attribute allows you to specify a URL for Twilio to send webhook requests to on each event specified in the statusCallbackEvent attribute. Non-relative URLs must contain a valid hostname (underscores are not permitted).

The parameters Twilio passes to your application in its asynchronous request to the StatusCallback URL include all parameters passed in a synchronous request to retrieve TwiML when Twilio receives a call to one of your Twilio numbers. The full list of parameters and descriptions of each are in the [TwiML Voice Request][voice-request] documentation.

When the call progress events are fired, the Status Callback request also passes these additional parameters:

ParameterDescription
CallSidA unique identifier for this call, generated by Twilio. You can use the CallSid to modify the child call by [POSTing to Calls/{CallSid} with a new TwiML URL][update-call].
ParentCallSidA unique identifier for the parent call.
CallStatusA descriptive status for the call. The value is one of queued, initiated, ringing, in-progress, busy, failed, or no-answer. See the [CallStatus section][call-status] for more details.
CallDurationThe duration in seconds of the just-completed call. Only present in the completed event.
RecordingUrlThe URL of the phone call's recorded audio. This parameter is included only if record is set on the <Dial> and does not include recordings initiated in other ways. RecordingUrl is only present in the completed event.
RecordingSidThe unique ID of the [Recording][recordings] from this call. RecordingSid is only present in the completed event.
RecordingDurationThe duration of the recorded audio (in seconds). RecordingDuration is only present in the completed event. To get a final accurate recording duration after any trimming of silence, use [recordingStatusCallback][recording-status-callback].
TimestampThe timestamp when the event was fired, given as UTC in [RFC 2822][rfc-2822] format.
CallbackSourceA string that describes the source of the webhook. This is provided to help disambiguate why the webhook was made. On Status Callbacks, this value is always call-progress-events.
SequenceNumberThe order in which the events were fired, starting from 0. Although events are fired in order, they are made as separate HTTP requests and there is no guarantee they will arrive in the same order.

The statusCallbackMethod attribute allows you to specify which HTTP method Twilio should use when requesting the URL in the statusCallback attribute. The default is POST.

Whether to detect if a human, answering machine, or fax has picked up the call. Can be: Enable or DetectMessageEnd. Use Enable if you would like us to return AnsweredBy as soon as the called party is identified. Use DetectMessageEnd if you would like to leave a message on an answering machine.

(warning)

Warning

Answering Machine Detection on requires Enhanced Programmable SIP Features(link takes you to an external page) to be enabled on the account.

The number of seconds that we should attempt to detect an answering machine before timing out and sending a voice request with AnsweredBy of unknown.

The number of milliseconds that is used as the measuring stick for the length of the speech activity, where durations lower than this value will be interpreted as a human and longer than this value as a machine.

The number of milliseconds of silence after speech activity at which point the speech activity is considered complete.

The number of milliseconds of initial silence after which an unknown AnsweredBy result will be returned.

The URL that we should call to notify your application whether the call was answered by human, machine or fax.

The HTTP method we should use when calling the amdStatusCallback URL.

Example 1: Dialing to a SIP endpoint

examples-1 page anchor

In this example, we want to connect to kate@example.com. To connect the call to Kate, use a <Dial> verb with a <Sip> noun nested inside.

Dialing to a SIP endpoint

dialing-to-a-sip-endpoint page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.sip('kate@example.com');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>kate@example.com</Sip>
_10
</Dial>
_10
</Response>

Example 2: Dial a SIP endpoint, protected by authentication

examples-2 page anchor

In this example, you are still dialing Kate, but you need to pass authentication credentials to reach her.

Dial a SIP endpoint, protected by authentication

dial-a-sip-endpoint-protected-by-authentication page anchor
Node.js
Python
C#
Java
PHP
Ruby

_11
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_11
_11
_11
const response = new VoiceResponse();
_11
const dial = response.dial();
_11
dial.sip({
_11
username: 'admin',
_11
password: '1234'
_11
}, 'kate@example.com');
_11
_11
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip username="admin" password="1234">kate@example.com</Sip>
_10
</Dial>
_10
</Response>

Example 3: Passing headers

examples-3 page anchor

In this example, pass custom headers along with the SIP address.

Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.sip('sip:kate@example.com?x-mycustomheader=foo&x-myotherheader=bar');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Sip>sip:kate@example.com?x-mycustomheader=foo&amp;x-myotherheader=bar</Sip>
_10
</Dial>
_10
</Response>

Example 4: Dial with several attributes

examples-4 page anchor

A more complex Dial, specifying custom settings as attributes on <Dial>, including call screening.

Dial with several attributes

dial-with-several-attributes page anchor
Node.js
Python
C#
Java
PHP
Ruby

_18
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_18
_18
_18
const response = new VoiceResponse();
_18
const dial = response.dial({
_18
record: 'record-from-answer',
_18
timeout: 10,
_18
hangupOnStar: true,
_18
callerId: 'bob',
_18
method: 'POST',
_18
action: '/handle_post_dial'
_18
});
_18
dial.sip({
_18
method: 'POST',
_18
url: '/handle_screening_on_answer'
_18
}, 'sip:kate@example.com?x-customheader=foo');
_18
_18
console.log(response.toString());

Output

_17
<?xml version="1.0" encoding="UTF-8"?>
_17
<Response>
_17
<Dial
_17
record="record-from-answer"
_17
timeout="10"
_17
hangupOnStar="true"
_17
callerId="bob"
_17
method="POST"
_17
action="/handle_post_dial">
_17
_17
<Sip
_17
method="POST"
_17
url="/handle_screening_on_answer">
_17
sip:kate@example.com?x-customheader=foo
_17
</Sip>
_17
</Dial>
_17
</Response>

Example 5: Call Progress Events

examples-5 page anchor

In this example, we want to receive a webhook for each call progress event when dialing a SIP endpoint using <Dial>.

Node.js
Python
C#
Java
PHP
Ruby

_12
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_12
_12
_12
const response = new VoiceResponse();
_12
const dial = response.dial();
_12
dial.sip({
_12
statusCallbackEvent: 'initiated ringing answered completed',
_12
statusCallback: 'https://myapp.com/calls/events',
_12
statusCallbackMethod: 'POST'
_12
}, 'sip:kate@example.com');
_12
_12
console.log(response.toString());

Output

_11
<?xml version="1.0" encoding="UTF-8"?>
_11
<Response>
_11
<Dial>
_11
<Sip
_11
statusCallbackEvent='initiated ringing answered completed'
_11
statusCallback='https://myapp.com/calls/events'
_11
statusCallbackMethod='POST'>
_11
sip:kate@example.com
_11
</Sip>
_11
</Dial>
_11
</Response>

Example 6: Parallel Calling or Simultaneous Dialing

examples-6 page anchor
(information)

Info

In order to use the SIP Parallel Calling Twilio feature, you must enable the "Enhanced Programmable SIP Features" in your Voice settings in the Console(link takes you to an external page).

Parallel calling, also known as simultaneous dialing, is useful when you have several phones (or several people) that you want to ring when you receive an incoming call. Say you have multiple different endpoints where you can take a call, such as a mobile phone, home phone, office phone, and/or PC soft phone. This feature allows you to call up to ten endpoints at the same time by specifying a <Dial> verb with multiple destinations. Additionally, for each endpoint, you can specify which Status Callback events for which you want to receive Webhook requests.

In addition to phone numbers and/or Twilio Client identifiers, you can now specify SIP URIs for parallel calling using the <Dial> verb's <Sip> noun.

You can mix up to ten different <Sip>, <Number>, or <Client> nouns within a Dial verb to call simultaneously.

Keep in mind that the first call that connects will cancel all the other attempts. If you dial an office phone system or a mobile phone in airplane mode, etc., that endpoint may pick up after a single ring, preventing the other endpoints from ringing long enough for a human to ever answer. Hence you should take care to use simultaneous dialing in situations where you know the behavior of the called parties.

For the example, Alice has three different ways to contact her using SIP. She wants you to ring her on her SIP soft phone, SIP desk phone and SIP mobile client at the same time, and she will pick up one depending on where she is at the time of the call.

Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
const response = new VoiceResponse();
_10
const dial = response.dial();
_10
dial.number('+12143211432');
_10
dial.sip('sip:alice-soft-phone@example.com');
_10
dial.sip('sip:alice-desk-phone@example.com');
_10
dial.sip('sip:alice-mobile-client@example.com');
_10
_10
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial>
_10
<Number>+12143211432</Number>
_10
<Sip>sip:alice-soft-phone@example.com</Sip>
_10
<Sip>sip:alice-desk-phone@example.com</Sip>
_10
<Sip>sip:alice-mobile-client@example.com</Sip>
_10
</Dial>
_10
</Response>

Example 7: Serial Calling or Sequential Dialing

examples-7 page anchor
(information)

Info

In order to use the SIP Serial Calling Twilio feature, you must enable the "Enhanced Programmable SIP Features" in your Voice settings in the Console(link takes you to an external page).

Serial calling, also known as sequential dialing, is useful when you have several phones (or several people) that you want to ring in a specific order when you receive an incoming call. Say you have a group of agents in a call center, and you want to try each of them in order until one of them picks up. This feature allows you to call up to ten agents sequentially by specifying a <Dial> verb with multiple destinations, and setting the sequential attribute to true. The priority of the calls is based on the order within the <Dial> verb. Additionally, for each endpoint, you can specify which Status Callback events you want to receive via Webhooks.

You can mix up to ten different <Sip>, <Number>, or <Client> nouns within a <Dial> verb to call sequentially.

Keep in mind that the first call that connects will cancel all the remaining attempts in the sequence. If you dial an office phone system or a mobile phone in airplane mode, etc., that endpoint may pick up after a single ring, preventing the remaining endpoints from being called. Hence you should take care to use sequential dialing in situations where you know the behavior of the called parties.

For the example below, if Alice rejects or does not answer then the call will go to Bob. Similarly if Bob rejects or does not answer then the call will go to Charlie. If Charlie does not answer, the call will fail.

Node.js
Java
PHP

_11
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_11
_11
const response = new VoiceResponse();
_11
const dial = response.dial({
_11
sequential: true
_11
});
_11
dial.sip('sip:alice@example.com');
_11
dial.sip('sip:bob@example.com');
_11
dial.sip('sip:charlie@example.com');
_11
_11
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Dial sequential="true">
_10
<Sip>sip:alice@example.com</Sip>
_10
<Sip>sip:bob@example.com</Sip>
_10
<Sip>sip:charlie@example.com</Sip>
_10
</Dial>
_10
</Response>


Rate this page: