Custom Telephony
A step by step guide to integrate custom telephony providers
Check out the code at Node Demo / Python Demo for a practical example. All code in this guide is from this repository.
In this guide, we will show how to integrate Retell AI with phone call using Twilio in your server. This way, you can use your own numbers and have full control over telecommunications functionalities.
You can use your own Twilio with both Retell LLM or Custom LLM.
How Data Flow During Phone Call
Get Phone Number / Bind Phone Number With Agent
This will set up the voice webhook of the number, and the number is good to accept inbound
calls.
Setup Voice Webhook
This webhook will be called whenever a phone rings for inbound
and outbound
calls. You will also register call
with Retell at this stage.
(Optional) Make Outbound Call
Supply the callee number to make an outbound call with your configured number in step 1.
Audio Websocket
Retell’s server with handle audio bytes with Twilio.
LLM Websocket
Retell’s server communicate with your server to get responses for agents.
Setup Twilio Account And Your Server
If you haven’t, sign up account with Twilio to get your account id and auto token. Please note that Twilio free account can only create one number, so if you need more than one number in your development, you’d have to upgrade to a Twilio paid account.
Same as the previous guide for setting up your LLM WebSocket
server, you need your server exposed to public network so Twilio can call.
Buy A Phone Number From Twilio
Get a new number under the area code, associate an agent with it.
Now every time inbound / outbound call happens for this number, it would call
${your-ip-address}/twilio-voice-webhook/agent-id
which you will set up later.
You can grab the agent id from you Retell LLM agent or Custom LLM agent
The Retell agent id in the path parameter would let your voice webhook be able to know which agent to use.
Bind Your Existing Twilio Numbers With Retell Agent
Change the voice url of your existing Twilio number. Can also use to update the number you created from last step.
Now every time inbound / outbound call happens for this number, it would call
${your-ip-address}/twilio-voice-webhook/agent-id
which you will set up later.
The Retell agent id in the path parameter would let your voice webhook be able to know which agent to use.
Setup Twilio Voice Webhook (Inbound Setup Complete)
Twilio will call this endpoint when there’s an incoming/out-going call for your Twilio number to get an address to stream the call to.
Here you will pass Retell’s Audio Websocket
server address to it.
wss://api.retellai.com/audio-websocket/{call_id}
The following code does 2 things:
Register Call
: sends the detail of the call (agent, encoding, etc) to Retell server and gets acall_id
from our server. Thiscall_id
is the path parameter of theaudio websocket
endpoint. By registering the call, and pass that id to Twilio, which in turn use the id to connect to Retell server, we would know how to handle this call correctly.- Start Twilio Stream and asks it to connect to Retell: here you would pass the
audio websocket
URL to Twilio, and Twilio would connect to Retell server by calling that, and we will take care of all the audio bytes exchanges from there.
Now your webhook is listening at ${your-ip-address}/twilio-voice-webhook
and expects a path parameter for agent Id
.
Make An Outbound Call with Retell Agent
Specify the fromNumber
as a twilio number you just set up. toNumber
as callee number.
Was this page helpful?