Skip to content

Commit bcca797

Browse files
github-actions[bot]examples-botclaude
authored
[Example] 021 — Twilio Voice Agent Telephony Integration (Node.js) (#104)
## New example: Twilio Voice + Deepgram Voice Agent — AI Phone Agent <!-- metadata type: example number: 021 slug: twilio-voice-agent-node language: javascript products: agent,stt,tts integrations: twilio --> **Integration:** Twilio Voice | **Language:** Node.js | **Products:** Voice Agent (STT + LLM + TTS) ### What this shows - Express server bridging Twilio Media Streams to Deepgram's Voice Agent WebSocket API (`wss://agent.deepgram.com/v1/agent/converse`) - Bidirectional audio: caller speech → Deepgram STT → LLM → Deepgram TTS → caller - Function calling (tool use) within the voice agent — demo "pizza shop" order status lookup - Inbound call handling via TwiML webhook + outbound call initiation via Twilio REST API - μ-law 8 kHz audio throughout — no server-side format conversion needed - Barge-in support: clears Twilio audio buffer when the user interrupts the agent ### Required secrets - `DEEPGRAM_API_KEY` - `TWILIO_ACCOUNT_SID` - `TWILIO_AUTH_TOKEN` - `TWILIO_PHONE_NUMBER` Closes #26 --- *Built by Engineer on 2026-04-01* Co-authored-by: examples-bot <noreply@deepgram.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5392afc commit bcca797

5 files changed

Lines changed: 737 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Deepgram — https://console.deepgram.com/
2+
DEEPGRAM_API_KEY=
3+
4+
# Twilio — https://console.twilio.com/
5+
TWILIO_ACCOUNT_SID=
6+
TWILIO_AUTH_TOKEN=
7+
TWILIO_PHONE_NUMBER=
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Twilio Voice + Deepgram Voice Agent — AI Phone Agent
2+
3+
Build an AI-powered phone agent by connecting Twilio Voice calls to Deepgram's Voice Agent API. Callers speak naturally, the agent listens (STT), thinks (LLM), and responds (TTS) — all in real-time over the phone, with function calling support for dynamic actions like order lookups.
4+
5+
## What you'll build
6+
7+
An Express server that bridges Twilio Media Streams to Deepgram's Voice Agent WebSocket API, enabling bidirectional conversational AI over the phone. The example includes a "pizza shop" agent that greets callers and can look up order statuses using a function call.
8+
9+
## Prerequisites
10+
11+
- Node.js 18+
12+
- Deepgram account — [get a free API key](https://console.deepgram.com/)
13+
- Twilio account — [sign up](https://www.twilio.com/try-twilio)
14+
- A Twilio phone number with Voice capability
15+
- A public URL for your server (use [ngrok](https://ngrok.com/) for local development)
16+
17+
## Environment variables
18+
19+
| Variable | Where to find it |
20+
|----------|-----------------|
21+
| `DEEPGRAM_API_KEY` | [Deepgram console](https://console.deepgram.com/) |
22+
| `TWILIO_ACCOUNT_SID` | [Twilio console](https://console.twilio.com/) → Account Info |
23+
| `TWILIO_AUTH_TOKEN` | [Twilio console](https://console.twilio.com/) → Account Info |
24+
| `TWILIO_PHONE_NUMBER` | [Twilio console](https://console.twilio.com/) → Phone Numbers |
25+
26+
Copy `.env.example` to `.env` and fill in your values.
27+
28+
## Install and run
29+
30+
```bash
31+
npm install
32+
npm start
33+
```
34+
35+
Then expose the server publicly (for local dev):
36+
37+
```bash
38+
ngrok http 3000
39+
```
40+
41+
Configure your Twilio phone number's Voice webhook to `https://<your-ngrok-url>/voice` (HTTP POST).
42+
43+
Call your Twilio number — the agent will greet you and respond conversationally.
44+
45+
### Outbound calls
46+
47+
To initiate an outbound call:
48+
49+
```bash
50+
curl -X POST http://localhost:3000/outbound \
51+
-H "Content-Type: application/json" \
52+
-d '{"to": "+1234567890"}'
53+
```
54+
55+
## Key parameters
56+
57+
| Parameter | Value | Description |
58+
|-----------|-------|-------------|
59+
| `model` (listen) | `nova-3` | Deepgram STT model for transcribing caller speech |
60+
| `model` (think) | `gpt-4o-mini` | LLM that generates agent responses |
61+
| `model` (speak) | `aura-2-thalia-en` | Deepgram TTS voice for agent speech |
62+
| `encoding` | `mulaw` | Audio format — matches Twilio's native μ-law 8 kHz |
63+
| `sample_rate` | `8000` | Telephony standard — no server-side conversion needed |
64+
65+
## How it works
66+
67+
1. An incoming call hits `POST /voice`, which returns TwiML with `<Connect><Stream>` pointing to the `/media` WebSocket
68+
2. Twilio opens a bidirectional WebSocket to `/media` and streams caller audio as base64-encoded μ-law at 8 kHz
69+
3. The server opens a WebSocket to Deepgram's Voice Agent API (`wss://agent.deepgram.com/v1/agent/converse`) and sends a Settings message configuring STT, LLM, TTS, a system prompt, and function definitions
70+
4. Caller audio is decoded from base64 and forwarded as raw binary to the Deepgram agent
71+
5. The agent transcribes speech, generates a response via the LLM, and streams TTS audio back as binary frames
72+
6. TTS audio is base64-encoded and sent back to Twilio as media events, so the caller hears the agent's response
73+
7. When the LLM decides to call a function (e.g., `check_order_status`), the server receives a `FunctionCallRequest`, executes the function locally, and sends the result back via `FunctionCallResponse`
74+
8. If the caller interrupts (barge-in), the server sends a `clear` event to Twilio to stop playback immediately
75+
76+
## Starter templates
77+
78+
If you want a ready-to-run base for your own project, check the [deepgram-starters](https://github.com/orgs/deepgram-starters/repositories) org — there are starter repos for every language and every Deepgram product.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "deepgram-twilio-voice-agent",
3+
"version": "1.0.0",
4+
"description": "Connect Twilio Voice calls to Deepgram's Voice Agent API for bidirectional conversational AI over the phone",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"start": "node src/index.js",
8+
"test": "node tests/test.js"
9+
},
10+
"dependencies": {
11+
"dotenv": "^16.4.0",
12+
"express": "^4.21.0",
13+
"express-ws": "^5.0.2",
14+
"twilio": "^5.4.0",
15+
"ws": "^8.18.0"
16+
},
17+
"engines": {
18+
"node": ">=18"
19+
}
20+
}

0 commit comments

Comments
 (0)