Things you’ll need
- An active Twilio account with API credentials
- One or more Twilio provisioned phone numbers
- A public-facing server or tunneling service like ngrok
- API keys for speech-to-text, text-to-speech, and LLM services
Twilio Dial-in Example
Complete dial-in implementation using Twilio Media Streams over WebSocket
Twilio Dial-out Example
Outbound calling using Twilio Media Streams with WebSocket transport
Phone Number Setup
You’ll need Twilio phone numbers for both dial-in and dial-out functionality:- Visit console.twilio.com and purchase phone numbers
- Ensure your numbers support Voice capabilities
- Configure webhook URLs for dial-in numbers (covered below)
Environment Setup
Configure your environment variables for Twilio and AI services:.env
Dial-in
Dial-in allows users to call your Twilio number and connect to your Pipecat bot via WebSocket Media Streams.How It Works
Here’s the sequence of events when someone calls your Twilio number:- Twilio receives an incoming call to your phone number
- Twilio calls your webhook server with call details
- Your server responds with TwiML that establishes a WebSocket connection
- Twilio opens a WebSocket to your server with real-time audio
- Your bot processes the audio using the Pipecat pipeline
- The bot responds with audio sent back to Twilio over WebSocket
- Twilio plays the audio to the caller in real-time
Set up your webhook server
Your server handles Twilio webhooks and WebSocket connections for real-time audio streaming. When Twilio receives a call, your server:- Receives the webhook with call details (CallSid, caller information)
- Returns TwiML that establishes a WebSocket connection to your server
- Accepts the WebSocket connection from Twilio
- Parses call data including custom parameters from the WebSocket
- Runs the Pipecat bot with the parsed call information
Custom Parameters with TwiML
Twilio’s<Parameter>
noun allows you to pass custom information to your bot through the WebSocket connection. This is essential for passing caller details, user IDs, or other contextual information:
TwiML with custom parameters
customParameters
field in the WebSocket data.
Complete Server Implementation
See the full FastAPI server code with TwiML generation and WebSocket handling
Configure your Pipecat bot for dial-in
Your bot receives the WebSocket connection and parses the call data to extract custom parameters and call information. The key components are: WebSocket Parsing: Pipecat’s built-in parser extracts call data from Twilio’s WebSocket messages:bot.py
bot.py
bot.py
Complete Bot Implementation
See the full bot.py with WebSocket parsing, transport setup, and pipeline
configuration
Set up Twilio webhook
Configure your Twilio phone number to use your server’s webhook URL:- Go to the Twilio Console
- Navigate to Phone Numbers → Manage → Active Numbers
- Click on your phone number
- Under “Configure”, set “A Call Comes In” to:
- Webhook:
https://your-server.com/
(your server’s URL) - HTTP Method: POST
- Webhook:
Run the Example
For local development, use ngrok to expose your local server:Complete Setup Instructions
See the full README with step-by-step setup, webhook configuration, and
testing
Dial-out
Dial-out allows your bot to initiate calls to phone numbers using Twilio’s outbound calling capabilities with WebSocket Media Streams.How It Works
Here’s the sequence of events for dial-out calls:- Your application triggers a dial-out (via API call or user action)
- Server initiates a Twilio call to the target phone number
- Twilio establishes the call and opens a WebSocket connection
- Your bot joins the WebSocket and sets up the pipeline
- The recipient answers and is connected to your bot
- The bot handles the conversation with real-time audio streaming
Set up your server for dial-out
The dial-out server creates outbound calls and manages WebSocket connections. When you trigger a dial-out call, your server:- Receives the dial-out request with target phone number and parameters
- Creates a Twilio call using the REST API with TwiML that establishes WebSocket
- Accepts the WebSocket connection from Twilio
- Parses call data including any custom parameters
- Runs the Pipecat bot with the call information
Complete Server Implementation
See the full FastAPI server code with outbound call creation and WebSocket
handling
Configure your Pipecat bot for dial-out
The dial-out bot configuration is similar to dial-in, but you can pass custom parameters when creating the outbound call: Custom Parameters for Dial-out: Pass information to your bot through TwiML parameters:server.py
bot.py
Complete Bot Implementation
See the full bot.py with outbound call handling and parameter usage
Run the Example
To test dial-out functionality:- Start your server: Run your FastAPI server
- Trigger a call: Make an API request to start an outbound call
- Answer your phone: The bot will call the specified number
- Talk to your bot: Have a conversation with your AI agent
Complete Setup Instructions
See the full README with step-by-step setup, API usage, and outbound call
configuration
Key Features
Audio Format and Sample Rate
Twilio Media Streams uses 8kHz mono audio with 16-bit PCM encoding. To avoid resampling, set the audio input and outputsample rate to 8000 Hz:Automatic Call Termination
When you provide Twilio credentials to theTwilioFrameSerializer
, it automatically ends calls when your pipeline ends:
Deployment
Local Development
Use ngrok to expose your local server for testing:Pipecat Cloud Deployment
For production deployment without managing your own infrastructure, use Pipecat Cloud:Twilio WebSocket on Pipecat Cloud
Deploy Twilio WebSocket bots with automatic scaling and managed infrastructure
Self-Hosted Production Deployment
For fully self-hosted production deployment, ensure your servers are:- Publicly accessible with HTTPS
- Able to handle concurrent WebSocket connections
- Properly configured with Twilio API credentials
- Implementing proper error handling and logging
Next Steps
- Explore the complete examples for full implementations
- Learn about Daily + Twilio SIP integration for advanced telephony scenarios
- Check out Daily PSTN integration for direct phone number provisioning