The integration of Zadarma and Twilio allows incoming calls from Zadarma virtual numbers to be directed to your Twilio account and processed according to your current Twilio settings. You can also make outgoing calls from Twilio through your Zadarma account at affordable Zadarma rates.
1. Incoming call setup
Adding a SIP domain in Twilio.
In your Twilio account, go to the Twilio Console → Voice → SIP Domains and click the + button to add a new domain.
Example of SIP domain settings:
- FRIENDLY NAME - domain name, for example, zadarma;
- SIP URI - domain, any name, for convenience you can use a virtual number.
In the Voice Authentication section, add the three IP ACCESS CONTROL LISTS one by one, which will contain the trusted IP addresses from Zadarma that Twilio will accept incoming calls from. You can find these IP addresses in the section Settings → Virtual phone numbers or simply copy them from this guide.
- FRIENDLY NAME - sipurifr.zadarma.com
- CIDR NETWORK ADDRESS - 185.45.152.216 /32
- FRIENDLY NAME - sipuriny.zadarma.com
- CIDR NETWORK ADDRESS - 185.45.155.33 /32
- FRIENDLY NAME - sipde.zadarma.com
- CIDR NETWORK ADDRESS - 185.45.152.174 /32
Make sure that all three added IP ACCESS CONTROL LISTS are selected in the SIP domain settings.
Below, in the Call Control Configuration section, in the A CALL COMES IN parameter, select what will manage incoming calls. If you have already set up incoming call routing in Twilio, choose your option, for example, Studio and your Flow (scenario) for incoming calls. Click Save to save the settings of your SIP domain.
Example of a Flow (scenario) for incoming calls▾
If you have selected your option in A CALL COMES IN - skip this step. Below is an example of creating a simple Flow for incoming calls in the Twilio Studio visual editor.
1. Go to Twilio Console → Studio and click the Create new Flow button. Enter FLOW NAME for example, "Incoming calls".
2. Select the Start from scratch template and click Next.
3. Drag the Gather Input on Call block (for IVR menu) and connect it to the Trigger (Incoming Call).
4. Specify the voice message (Text to Speech or Audio URL).
5. Set up the handling of choices (1 – Sales department, 2 – Support, etc.).
Next, you need to create two Set Variablesto modify the caller's number from the From header.
To create the first Set Variables with a Variable Name: step1_caller_id и Value:
{{contact.channel.address | replace: "sip:", "" }}
Create the second Set Variables (after the first one) Variable Name: clean_caller_id и Value:
{{flow.variables.step1_caller_id | replace: "@sip.zadarma.com", "" }}
Add the routing of the incoming call to an extension using Connect Call To.
CONNECT CALL TO – select SIP Endpoint.
В SIP ENDPOINT enter its URL, for example sip:11111@15551111111.sip.twilio.com
В CALLER ID
{{flow.variables.clean_caller_id}}
Save the widget settings and publish the Flow by clicking Publish.
Next, in the SIP Domain settings, in the SIP Registration section, you need to enable the registration option by toggling the switch to Enabled.
Below, in CREDENTIAL LISTS, select the existing SIP Endpoint that will receive incoming calls using the softphone. If you don’t have a SIP Endpoint yet, create one by clicking the "+" button.
Adding SIP Endpoint▾
If you already have a SIP Endpoint, skip this step.
- FRIENDLY NAME - my twilio extension.
- USERNAME - an arbitrary login, for example, 11111.
- PASSWORD - a password for authentication, create a strong password with at least 12 characters.
The softphone will connect to your domain, for example, 15551111111.sip.twilio.com and use the USERNAME and PASSWORD for authentication.
The SIP domain setup is complete. Click the Save button.
2. Call Routing in Twilio
n the previous step, we added a SIP domain in Twilio, which contains the required SIP URI for routing:
- In Zadarma, open the section Settings → Virtual phone numbers.
- Next to your number, click on the ⚙ (gear) icon.
- Open the "External Server" tab.
- Enable the "External Server" (SIP URI)" option.
In the field that appears, enter SIP URI: sip:15551111111@15551111111.sip.twilio.com
- where 15551111111.sip.twilio.com – is your SIP URI from the SIP Domain settings in Twilio.
- Click "Save".
The setup for incoming calls is complete.
2. Outgoing Call Setup from Twilio
Outgoing Calls from Twilio can be made in several ways. Let's consider an example of initiating a call using the Twilio REST API (Calls API).
In the next example, a conference will be created between a SIP Endpoint with a configured softphone and an external number (a call via Zadarma).
The example will use a Python script, so before starting, make sure that Python and the Twilio library (www.twilio.com/docs/libraries/reference/twilio-python) are installed on your system.
To authenticate the request, you need to use the Account SID and Auth Token. You can find them in Twilio in the Account Dashboard in the top left corner of the page.
For outgoing call authorization, a SIP login and password will be used from your Zadarma personal account.
Create a script call.py with the following content:
from twilio.rest import Client
# Twilio credentials
ACCOUNT_SID = "Account_SID_value"
AUTH_TOKEN = "Auth_Token_value"
TWILIO_NUMBER = "+15559999999" # A verified number in Twilio for Caller ID
CONFERENCE_NAME = "MyConferenceRoom"
# Zadarma SIP credentials
ZADARMA_SIP_URI = "sip:+525500000777@sip.zadarma.com" # Where the call is being made through Zadarma
ZADARMA_SIP_USER = "12345" # instead of 12345 should be your SIP login from Zadarma
ZADARMA_SIP_PASSWORD = "my_sip_password" # Password for the SIP login from Zadarma
# SIP Endpoint (local)
SIP_ENDPOINT = "sip:11111@15551111111.sip.twilio.com" # Your SIP Endpoint on which the softphone is set up to receive calls
# Initializing Twilio client
client = Client(ACCOUNT_SID, AUTH_TOKEN)
# Function for adding a participant to the conference
def add_to_conference(to_number, from_number=None, sip_auth_user=None, sip_auth_password=None):
call_params = {
"to": to_number,
"from_": from_number if from_number else TWILIO_NUMBER,
"twiml": f"""<Response>
<Dial>
<Conference>{CONFERENCE_NAME}</Conference>
</Dial>
</Response>"""
}
# Adding authentication
if sip_auth_user and sip_auth_password:
call_params["sip_auth_username"] = sip_auth_user
call_params["sip_auth_password"] = sip_auth_password
call = client.calls.create(**call_params)
print(f"Call to {to_number} initiated, SID: {call.sid}")
# Starting calls
add_to_conference(ZADARMA_SIP_URI, from_number=TWILIO_NUMBER, sip_auth_user=ZADARMA_SIP_USER, sip_auth_password=ZADARMA_SIP_PASSWORD) # Call to an external number through Zadarma
add_to_conference(SIP_ENDPOINT) # Connecting the SIP Endpoint
print(f"Conference '{CONFERENCE_NAME}' created!")
Save and run your script call.py. If everything is set up correctly, calls will be received in the softphone and on the external number from your SIP login on Zadarma.