twilio plugin bridges PSTN phone calls into a Vision Agents + Stream call through Twilio voice webhooks, TwiML Media Streams, and bidirectional WebSocket audio.
Vision Agents requires a Stream account
for real-time transport.
What the plugin provides
- Twilio Media Streams over WebSocket with bidirectional audio
TwilioCallRegistryfor call/session/token trackingattach_phone_to_callto bridge Twilio mulaw audio ↔ Stream WebRTC- Built-in FastAPI helpers:
verify_twilio_signature,CallWebhookInput, TwiML response builders - Automatic mulaw/PCM conversion at 8 kHz
Prerequisites
| Requirement | Notes |
|---|---|
| Twilio account | TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN |
| Twilio phone number (E.164) | Used as caller ID and inbound number |
| Webhook public URL | ngrok for local dev → https://<NGROK_URL>/twilio/voice |
| Stream credentials | STREAM_API_KEY, STREAM_API_SECRET |
Installation
Environment Variables
| Variable | Required | Description |
|---|---|---|
TWILIO_ACCOUNT_SID | Yes | Twilio account SID |
TWILIO_AUTH_TOKEN | Yes | Auth token for REST API and webhook signature verification |
NGROK_URL | Local dev | Public hostname without https:// |
STREAM_API_KEY / STREAM_API_SECRET | Yes | Stream edge transport |
GOOGLE_API_KEY | Examples | Required by the phone agent examples (Gemini) |
Twilio account setup
- Buy or assign a Twilio phone number.
- Under Phone Numbers → Manage → Active numbers, open your number.
-
Set A call comes in to Webhook pointing at:
- Use HTTP POST.
Quick Start
The plugin gives you registry, media stream, and bridge primitives. Your FastAPI server wires them to Twilio webhooks and WebSockets:Inbound calls
Twilio sends a webhook when someone calls your number. Validate the signature, register the call, and return TwiML to start the media stream:Outbound calls
Pre-register the call in the registry, start your server, then dial via the Twilio REST API:Key Components
| Component | Description |
|---|---|
TwilioCallRegistry | Tracks active calls, tokens, optional async prepare tasks |
TwilioCall | Call session with from_number, to_number, await_prepare() |
TwilioMediaStream | WebSocket media handler; exposes audio_track, send_audio(), run() |
attach_phone_to_call | Bridges Twilio mulaw audio ↔ Stream call participant |
verify_twilio_signature | FastAPI dependency for webhook authentication |
CallWebhookInput | Typed model for Twilio voice webhook form data |
create_media_stream_response / create_media_stream_twiml | TwiML helpers for bidirectional streaming |
| Audio helpers | mulaw_to_pcm, pcm_to_mulaw, TWILIO_SAMPLE_RATE |
Audio
Twilio Media Streams use mulaw encoding at 8 kHz. The plugin converts between mulaw and PCM automatically inTwilioMediaStream and exposes conversion helpers if you need them directly.
| Constant | Default | Description |
|---|---|---|
TWILIO_SAMPLE_RATE | 8000 | Twilio media stream sample rate |
Common setup errors
| Error | Fix |
|---|---|
| Webhook URL mismatch | Update the Twilio number webhook to your current ngrok URL |
| Invalid Twilio signature | Ensure TWILIO_AUTH_TOKEN is set; add ProxyHeadersMiddleware behind ngrok |
| No audio on call | Confirm the media WebSocket URL uses wss:// and the server is running before dialing |
| Outbound call fails | Verify --from is a Twilio number on your account |
Next Steps
Twilio Phone Agent
Step-by-step inbound and outbound phone tutorial
Phone Support Agent (RAG)
Add knowledge retrieval to phone calls
Phone Calling
Provider overview and learning path
Telnyx
Alternative telephony provider
Stream Video RTC
Default edge transport for agent calls
Build a Voice Agent
Get started with voice