A Step-by-Step Guide to Set Up Twilio Elastic SIP Trunking

April 25, 2019
Written by
Alan Klein
Twilion

Copy of Generic Blog Header 2-3.png

In my last blog post, Registering a SIP Phone Directly to Twilio (and so much more...), we set up a Twilio Programmable Voice SIP Domain and registered our SIP softphones directly to Twilio. This let us place and receive calls to the Public Switched Telephone Network (PSTN) but more importantly, set up the foundation for building creative applications which intelligently route calls using dynamically returned Twilio Markup Language (TwiML).

In this post we’ll provide a step-by-step approach to connect your SIP element – IP-PBX, SIP-PRI IAD, SBC, etc. – to Twilio’s Elastic SIP Trunking product. By the end of this post, you’ll have your existing communications infrastructure connected through Twilio and ready to layer on a number of Twilio’s other features.

At its most basic level, Twilio Elastic SIP trunking provides dynamic trunking capacity to place and receive pay-as-you-go phone calls through Twilio. Twilio’s network spans a diverse super network of thousands of carrier partner connections and phone numbers from over 100 countries. With Elastic SIP Trunking we provide a conduit to and from the PSTN, while your existing SIP infrastructure provides the call features and functionality.

Twilio services and SIP features we’ll cover

  • Twilio Elastic SIP trunking requirements
  • Twilio Termination and Origination Configuration
  • SIP element Termination and Origination Configuration
  • Migration Plan (outbound calling, porting numbers, inbound calling)

Prerequisites to work with Elastic SIP Trunking and Twilio

To accomplish the tasks in this blog post you’ll need the following:

  • Twilio Account (Sign up for a free account.)
  • SIP Network Element

Before we get started

The features, capabilities, and configuration of each SIP element are quite different, and configuring features may require partnering with your vendor's professional services, even for minor changes. This cycle often repeats when you make changes, such as modifying your Interactive Voice Response (IVR) or integrating communications into your business applications. This process makes it hard to iterate and adapt to dynamic business conditions – and when you try, results are often costly and sub-optimal. That limitation is so un-Twilio, where we are the builders, the movers, the shakers!

Twilio Elastic SIP trunking starts with the native features and functionalities of your SIP element. However, it also opens up the possibility to use Twilio products such as Programmable Voice, Programmable SMS and Studio to embed communications seamlessly into your applications.

From there, you can optimize your business processes even further through conditional call routing and SMS notifications, without requiring additional SIP element configuration. When you remove your reliance on a third party, you can create a solution that best fits your requirements.

Let’s Begin

The configuration of SIP trunking across SIP elements is often similar, but screens and terminology differ.

I’ll first discuss the specific configuration steps required to successfully integrate with Twilio Elastic SIP trunking, then the broader generalized approach to integrate with a SIP network element. Twilio has a number of detailed configuration guides for popular platforms such as FreePBX(r) and Asterisk, but we have integrated with a large number of diverse platforms.

When specific configuration guides are not available for your equipment, forums such as Stack Overflow for additional insight are often helpful.

Twilio Elastic SIP trunking requirements

1. Twilio Elastic SIP Trunks do not support the SIP REGISTER method, so you will need to set-up Twilio SIP signaling IP addresses as trusted peers for your SIP element. (These are connections which you do not challenge for authentication credentials.)

Twilio signaling IP addresses are found under Networking Information. For North American locations, leverage NA Virginia and NA Oregon at a minimum, for redundancy.

2. Twilio Elastic SIP Trunks currently support the G.711U (PCMU) codec (the method of encoding voice across an IP network). You can disable all other SIP element codecs when communicating with Twilio.

3. Twilio Elastic SIP Trunks support the following SIP Methods:

  • INVITE
  • ACK
  • CANCEL
  • REFER
  • BYE
  • OPTIONS

4. All numbers must be sent to Twilio in E.164 format and will be sent in E.164 format to your SIP element.

5. You must direct calls toward your Twilio Elastic SIP Trunk termination URI; you cannot send calls to specific Twilio IP addresses as you will not get a response.

6. Twilio supports SIP/UDP, SIP/TCP, and SIP/TLS (for certain tested SIP elements).

7. SIP/TLS is required to utilize Secure Real-time Transport Protocol (SRTP / encrypted voice).

8. Twilio uses RFC 4733/2833 for Dual Tone – Multi Frequency (DTMF) Events.

9. You must specify a Caller ID Number that either corresponds to a Twilio DID on your account or a Caller ID Number that has been verified on the Console or with the Outgoing Caller ID API.

10. Twilio Elastic SIP Trunking capacity expands and contracts to meet your businesses capacity needs. (Twilio SIP Trunking Scale and Limits provides the details.)

If your business requires a high outbound simultaneous calls per second rate (from your SIP Element to Twilio), we can work with you to increase the default of 1 trunking termination call per second (CPS).

Twilio Elastic SIP Trunking Configuration

 

Twilio SIP Infrastructure

 

We first provision the Twilio Elastic SIP Trunk through the Twilio Console, following the steps below. These steps assume there are no existing SIP Trunks provisioned in your account.

For an example, we’ll use the Twilio US1 (Virginia) data center as the primary data center for call Termination and Origination and the Twilio US2 (Oregon) data center as the secondary data center for call Termination and Origination.

For your own usage, modify the Termination and Origination URI’s based on the data centers that are physically closest to your location. A list of the regional termination and origination URI’s are on the SIP Trunking landing page:

Termination: https://www.twilio.com/docs/sip-trunking#termination

Origination: https://www.twilio.com/docs/sip-trunking#origination

1. Log into the Twilio Console - https://www.twilio.com/login.

2. From the main Dashboard on the left side of the screen, select (...) for All Products & Services.

3. Scroll down to Elastic SIP Trunking and click it.

4. From the Elastic SIP Trunking Dashboard, click the "Get Started" button.

5. From the Get Started with Elastic SIP Trunking page, Click the "Create a SIP Trunk".

6. Enter a Friendly Name. For the configuration guide, I used "TwilioBLOG". Click Create.

7. On the "TwilioBLOG" General Settings Screen, note the settings that impact this specific trunk (Call Recording, Secure Trunking, Call Transfer [SIP REFER], Media Settings).

Configuring Your Termination URI (From SIP Element toward Twilio)

8. On the left side of the screen under your "TwilioBLOG" Trunk Name, Choose Termination.

9.In order to use a Trunk for termination, it must have a Termination SIP URI and at least one authentication scheme (IP Access Control Lists and/or Credential Lists).

We will provision both authentication schemes.

10. In the Termination SIP URI edit box, enter a unique termination URI (we use "twilio-blog" aka twilio-blog.pstn.twilio.com), on the far-right, you will be notified if the name is unique and Available when you click outside the edit box.

11. Click the red plus (‘+’) next to IP Access Control Lists to create a new IP Access Control List that will restrict requests to your Termination SIP URI.

12. In the "New Access Control List" (ACL) provide a Friendly Name for the ACL, I used "EnterpriseSIPElement".

Under this list, you can allow multiple IP addresses to communicate with your SIP termination URI. Enter an IP address and a Friendly Name for that IP address (in my case "IP-PBX") and click Create ACL. This is the public IP address of your SIP element.

SIP Access Control List

13. You should receive confirmation the ACL was successfully created and see the Friendly Name (in my case "EnterpriseSIPElement") selected in the IP Access Control Lists.

14. Click the red plus (‘+’) next to Credential Lists to create a new Credential List. This will be used to challenge requests to your Termination SIP URI.

15. In the "New Credential List" provide a Friendly Name for the Credential List, I used "EnterpriseSIPElementProxyChallenge".

Under this list, you can have multiple credentials that allow communications with your SIP termination URI.

Enter a username, in my case "twilio-blog" and a password, in my case "sipElementpbxzx11%VzX", then click 'Create'. This username and password will be used by your SIP element to make calls through Twilio.

Pick a unique username and complex password. It must have a minimum length of 12 characters, at least one number, an uppercase character and a lowercase character.

Creating a new Credential List

16. You should receive confirmation you have successfully created a new Credential List and see the Credential List name (in my case, "EnterpriseSIPElementProxyChallenge") selected in the Credential Lists.

17. Scroll down to the bottom, and Click Save.

18. You should receive confirmation you have successfully updated your SIP Trunk.

Configuring Your Origination URI’s (From Twilio toward SIP Element)

19. On the left side of the screen under your "TwilioBlog" Trunk Name, Choose Origination.

20. Click the "Add new Origination URI" button, where we define how calls are sent from Twilio to your SIP Element. We’ll configure multiple origination URI regions for redundancy. It is possible to configure up to ten Origination SIP URIs with different priorities & weights.

The priority field, 0 to 65535, determines a SIP URI’s usage precedence.

Twilio will always use the SIP URI with the lowest-numbered priority value first, then fallback to other SIP URIs of equal or higher value if the session to that SIP URI fails. (This is the approach we use in this blog.)

If a service has multiple origination SIP URIs with the same priority value, Twilio will use the weight field to determine which SIP URI to use. The weight value is relevant only in relation to other SIP URIs with the same priority value. Weight is used to balance the load when more than one URI has the same priority.

NOTE: Should origination calls fail from a higher priority region and the following SIP status codes are returned by your SIP element, ("2xx", "400", "404", "405", "410", "416", "482", "484", "486", "6xx"), Twilio will not fail over to the next origination SIP URI.

If there is no SIP response from your SIP element, Twilio will fail over after 4 seconds.

21. For the Origination SIP URI edit box, enter the format (without the quotes and with your  SIP elements unique public IP address or Fully Qualified Domain Name): sip:203.0.113.254;region=us1 with a priority of 10 and a weight of 10.

This will originate all SIP Traffic from the Twilio US1 (Virginia) data center to your SIP element and limit the IP addresses to that region. Click Add.

Add a SIP Origination URI on Twilio

22. Click the plus button (‘+’) next to Origination URI, to add a secondary Origination URI, should the primary encounter issues reaching your SIP element.

23. For the Origination SIP URI edit box, enter the format (without the quotes and with your unique public IP address or Fully Qualified Domain Name): sip:203.0.113.254;region=us2 with a priority of 20 and a weight of 10.

This will originate SIP Traffic from the Twilio US2 (Oregon) data center to your SIP element, only if the US1 Virginia data center is unable to deliver the call. Click Add.

Add an Origination URI on Twilio SIP example

 

Before we leave the origination screen, note two things:

  • Caller Name (CNAM) can be toggled. This feature attempts to look up a Caller Name for US and Canadian based callers, and provide those names in the SIP INVITE toward your SIP Element.
  • The Disaster Recovery URL can fallback to a Twilio Markup Language (TwiML) source. The Disaster Recovery URL is requested when delivery toward all configured origination URIs fail.

Assigning Telephone Numbers To Your Elastic SIP Trunk

24. On the left side of the screen under your "TwilioBLOG" Trunk Name, Choose Numbers.

25. Click the "Buy a Number" button.

26. Search for a number that meets your needs.

27. Click Buy next to the number.

28. Click Buy This Number to confirm the purchase.

29. You will receive a confirmation dialog the number was purchased, close the dialog.

30. Your purchased number is now associated with your SIP Trunk.

And with that, you’ve configured Twilio Elastic SIP Trunk!

SIP element Termination and Origination Configuration Frequently Asked Questions

Below are some general steps and recommendations to follow when configuring your SIP element for integration with Twilio Elastic SIP Trunking.

Origination (from Twilio toward your SIP Element)

Does your firewall allow inbound signaling and media from the configured Twilio regions (i.e. ;region=(region)?

The list of ports and protocols (SIP Signaling IPs and Ports and Media IPs and Ports) should match the Twilio regions listed under Networking Information you are originating calls from.

  • Normally, if there is a firewall issue, you will see Twilio originate the SIP INVITE multiple times with no response from your SIP Element. (Viewing Twilio SIP Pcap captures, under Call Logs, with a tool such as Wireshark, will allow you to easily see this behavior.)

Does your SIP element expect to receive inbound calls from Twilio? Is Twilio Configured as a trusted peer?

  • If the SIP element is not configured correctly, it responds with one of the following three response codes:
            401 Unauthorized
            403 Forbidden
            407 Proxy Authentication Required[g][h]
  • You can spot this behavior by viewing Twilio SIP Pcap captures (under Call Logs) with a tool such as Wireshark.

Is your SIP element dial plan configured to route the incoming request (Request-URI), from Twilio, which is in E.164 format?

  • If the SIP element dial plan is not configured correctly, it will respond to Twilio with a 404 Not Found.
  • You can spot this behavior by viewing Twilio SIP Pcap captures (under Call Logs) with a tool such as Wireshark.

Termination (from your SIP Element to Twilio)

Does your firewall allow outbound signaling IP's and ports and media IP's and ports to the configured Twilio regions, as defined under Networking Info? You will configure your SIP element to use a primary and secondary Twilio Elastic SIP Trunk termination URI for redundancy purposes.

  • If your firewall is not configured properly, Twilio may never receive the call attempt or the signaling may alert the dialed PSTN party but there will be no voice (media).
  • Local packet captures from your network edge may provide insights into the filtering.

Are phone numbers in E.164 format for the Request-URI, From (CallerId) and To?

Based on authentication method configured on your Twilio trunk (Twilio recommends both IP ACL and credential list), is your SIP element configured to match against these authentication methods?

For E911, reference the official configuration guide and details to check for proper construction of the outbound SIP INVITE.

  • If E911 is configured for a Twilio number and test calling fails, check the Twilio console debugger, for errors.
  • Verify your configuration matches the provisioning requirements here.
  • View local packet captures from your SIP Element, to verify the number formatting.

Are voice geographic permissions configured for all destination countries?

If using a Twilio Trial account, did you verify the numbers you are trying to call? Please reference trial account limitations, for more details.

Make sure you are using either a Verified Caller ID in E.164 format or a Twilio phone number in E.164 format as the outbound CallerID.

Use SIP Option pings and timers for trunk failover.

  • Set-up your Communications Infrastructure to issue SIP OPTIONS messages as a liveliness mechanism to your Elastic SIP Trunk. The Twilio platform will respond appropriately. Do not no send more than one SIP OPTIONS message every 10-15 seconds to avoid your requests from being banned by the Platform.
  • If supported by your SIP element, SIP Options liveliness checks will allow your SIP element to intelligently add or remove trunks from service, so there is no perceived timer delays when placing outbound calls.

Migrating SIP Elements to Twilio Elastic SIP Trunking

You can migrate your SIP element to Twilio Elastic SIP Trunking in a comfortable timeframe or an incremental basis.

  1. Validate your SIP element configuration by making sure that calls can be placed and received successfully to your SIP element using your Twilio number (as we covered above.)
  2. Configure your SIP element to use Twilio Elastic SIP Trunking for terminating non-emergency calls, to further validate the solution.
  3. Set-up and validate an Elastic SIP Trunking E911 number, and validate its configuration using the testing criteria outlined here.
  4. Schedule porting of your existing telephone numbers to Twilio.

Detailed steps for porting your numbers over to Twilio are available in the document, Porting a Phone Number to Twilio.

Pre-provision the numbers in the Twilio Console, so they are bound to your Elastic SIP Trunk before the port date is completed. Additional helpful porting information can be found on the the porting landing page.

  1. Validate E911 calling, using the testing criteria outlined here.

Your SIP Element(s) on Twilio with Elastic SIP Trunking

Now you have your SIP Element - or possibly Elements – communicating through Twilio’s Elastic SIP Trunking. Your legacy infrastructure is now backed by our global Super Network managed by over 150 network professionals, and you can instantly provision numbers in over 100 countries via API or your Twilio console. From here, you can monitor call quality and metadata through our Voice Insights product and layer on other channels – everything from Facebook Messenger, WhatsApp to Web Chat, and beyond.

With Twilio Elastic SIP Trunking as your on-ramp to the PSTN, you’ll be able to move much more quickly with your communications. No longer do you need to engage professional services every time your business needs to adapt, on the contrary, you will be driving ahead with  innovation.

We can’t wait to see what you build! Once you get your Elastic SIP Trunk up, try taking a look at Twilio Studio, and setting up a SIP Dial to your SIP element!

Additional SIP Domain Resources

Alan Klein is a Senior Sales Engineer based in the rapidly growing Atlanta, GA Office. Alan is a Serverless solution champion and Programmable Voice and Elastic SIP Trunking SIP Expert at Twilio. He's currently focused on furthering his knowledge in Node.js and front-end development and sharing his knowledge with others. You can reach him at aklein [at] twilio.com or on Twitter at @SystemsEng.