audio/pcmu encoding, the server forwards audio as-is with zero transcoding.
Before you begin
To complete this guide, you need:- An AssemblyAI API key with Voice Agent access.
- A Twilio account with a phone number. You can buy one if you don’t have one.
- Node.js 20+.
- ngrok for exposing your local server to the internet.
Quickstart
Clone the example repo and get a working Twilio voice agent in minutes.1
Clone the repo and install dependencies
2
Start an ngrok tunnel
Twilio needs a public URL to reach your local server. In a separate terminal, start ngrok:Copy the
https://...ngrok.app URL from the output.3
Configure environment variables
.env and fill in your keys:4
Run the server
Server running on http://localhost:3000.5
Point Twilio at your server
In the Twilio Console, open your phone number’s Voice configuration and set:
- A call comes in → Webhook →
POST→https://<your-ngrok-domain>/twiml - Call status changes (optional) → Webhook →
POST→https://<your-ngrok-domain>/call-status
6
Call your number
Dial your Twilio number from any phone. You should hear the agent’s greeting, then have a real-time conversation. Watch the server logs to see the event stream.
How it works
When a call comes in, the following sequence happens:- A caller dials your Twilio number.
- Twilio sends a webhook to
POST /twimlon your server. The server returns TwiML containing a<Stream>element pointed at your WebSocket endpoint. - Twilio opens a Media Streams WebSocket and starts sending the caller’s audio (G.711 μ-law, 8 kHz).
- Your server opens a parallel WebSocket to the Voice Agent API and binds to your stored agent by
agent_id, with the audio format set toaudio/pcmu. - Once
session.readyfires, the server forwards audio in both directions:- Caller → Agent: Each Twilio
mediaevent becomes aninput.audioevent. - Agent → Caller: Each
reply.audioevent becomes a Twiliomediaaction.
- Caller → Agent: Each Twilio
- When the caller barges in (
input.speech.started), the server sends a Twilioclearaction so the agent stops talking immediately.
Return TwiML with a stream
When Twilio receives a call, it hits your/twiml endpoint. The server responds with TwiML that opens a Media Streams WebSocket:
Connect to the Voice Agent API
When Twilio opens the Media Streams WebSocket, the server creates a parallel connection to the Voice Agent API and binds to your stored agent byagent_id:
Create the agent with
audio/pcmu encoding. Twilio streams G.711 μ-law at 8 kHz, so store that format on the agent — set both its input and output format.encoding to audio/pcmu when you create it. Binding by agent_id is mutually exclusive with inline input/output, so the format lives on the agent, not in this session.update. Matching Twilio’s native codec means zero transcoding or resampling.Bridge audio between Twilio and the Voice Agent API
Oncesession.ready fires, forward audio payloads in both directions:
Handle barge-in
When the caller starts speaking while the agent is talking, clear the Twilio audio buffer so the agent stops immediately:Make outbound calls
The example repo also supports outbound calling. Set the Twilio credentials in.env:
/outbound-twiml, which connects the call to /outbound-stream. The agent speaks first using the configured greeting.
Add custom tools
The example includes one tool,generate_random_number. To add your own tools:
- Define the tool in the
TOOLSarray insrc/bot.ts:
- Add the handler in
runTool:
- When the agent calls a tool, the Voice Agent API sends a
tool.callevent. The server runs the tool and sends back atool.resultevent with the samecall_id. The agent then continues the conversation naturally.
Troubleshooting
- Call connects but no audio: Check that
HOSTNAMEmatches your ngrok domain and that your server is reachable. Watch ngrok’s request log for the incoming Media Streams WebSocket. session.errorwithinvalid_valueon thevoicefield: Voice names are case-sensitive. Use lowercase (ivy,claire,dawn, etc.). See Choose a voice for available voices.- Greeting plays but later replies don’t: Make sure your tool handler always sends a
tool.resultback. The agent waits for it before continuing. - Audio is choppy or echoey: Twilio handles echo cancellation on the carrier side. If you hear echo during testing, it’s likely your speakerphone. Use a headset.
Next steps
- Configure your agent: Customize the system prompt, greeting, and turn detection.
- Choose a voice: Pick a voice for your agent.
- Add tools to your agent: Give your agent the ability to call functions.
- Audio format: Learn more about supported encodings.
- Twilio Media Streams: Twilio’s documentation on Media Streams.