← Blog

Register SIP account and receive calls with Twilio

I live across three countries and I need local numbers in each for 2FA, signatures, and the usual stuff. I don’t use all SIM cards at the same time, but receiving calls on those numbers still matters.

I used to run my own Asterisk PBX on a VPS. I got tired of maintaining it and moved my DID numbers (originally from various providers) to Twilio.

Twilio uses TwiML (Twilio Markup Language) for call handling. This post shows how to use TwiML to register a SIP softphone and receive inbound calls.

One gotcha: as of July 2023 Twilio has POPs in ie1 (EU), us1 (US), and au1 (Australia). I had errors trying to dial a SIP endpoint registered in ie1 from a number also in EU (error 32002: Your TwiML tried to Dial a Twilio SIP Domain that can't be found). Stick with us1 for SIP domains and TwiML Bins until you know what you’re doing. If you got an EU POP working end-to-end, let me know.

Steps to set up Twilio for SIP registration and inbound calls:

  1. Create a SIP Credentials List in SIP Credentials Lists. Add a username and password. Your SIP softphone will use this to authenticate.

  2. Create a SIP Domain in SIP Domains with a unique name (e.g., yourusername.sip.twilio.com). Enable SIP registration and attach the credentials list from step 1. Configure encryption and other settings as needed. Pick us1 for broader compatibility, or ie1 if you target EU and accept the limitations above.

  3. Create a TwiML Bin in TwiML Bins with the content below. Replace USERNAME with the username from your credentials list and YOURDOMAIN.sip.twilio.com with your actual SIP domain. Adjust the region parameter in <Sip> if needed (e.g., us1 or au1).

    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
      <!-- Say>Hello from Twilio!</Say -->
    
      <Dial answerOnBridge="true" sequential="true" ringTone="lt" record="record-from-answer-dual" trim="trim-silence">
        <Sip>sip:USERNAME@YOURDOMAIN.sip.twilio.com;region=us1</Sip> <!-- Example using us1 region -->
      </Dial>
    
      <!-- Example of Simultaneous Dialing (Optional) -->
      <!--
      <Dial answerOnBridge="true" ringTone="es" record="record-from-answer-dual" trim="trim-silence">
        <Sip>sip:USERNAME@YOURDOMAIN.sip.twilio.com;region=us1</Sip>
        <Number>+YOUR_OTHER_PHONE_NUMBER</Number>
      </Dial>
      -->
    
      <Say>Hello, the person you are trying to reach is unavailable. Please leave a message at the beep.</Say>
    
      <Record timeout="30" transcribe="true" playBeep="true"/>
      <Hangup/>
    </Response>

    The example above shows sequential dialing to one SIP endpoint. You can modify it for simultaneous dialing or other call flows. Make sure the SIP URI is formatted as sip:username@domain.

  4. Buy a DID number from Twilio Phone Numbers. In the number’s configuration, under “A CALL COMES IN,” pick “TwiML Bin” and choose the Bin you created.

  5. Set up a SIP client (Groundwire by Acrobits, Zoiper, Linphone). Use the username and password from your credentials list and your SIP domain (e.g., YOURDOMAIN.sip.twilio.com) to register.

  6. Make a test call to your Twilio DID number. If something is off, check the Twilio Debugger logs.

You can then redirect your existing mobile numbers to your new Twilio DID using your carrier’s call forwarding (e.g., dial *21*+YOUR_TWILIO_NUMBER# on the phone; the exact code varies by carrier).