Things you’ll need
- An active Twilio developer account with API credentials
- One or more Twilio provisioned phone numbers
- Daily API key for WebRTC transport
- The Twilio Python client library (
pip install twilio
)
SIP Dial-in Example
Complete dial-in implementation using Twilio SIP with Daily WebRTC transport
SIP Dial-out Example
Outbound calling using Daily WebRTC transport with Twilio SIP routing
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 both Twilio and Daily:.env
Dial-in
Dial-in allows users to call your Twilio number and connect to your Pipecat bot via Daily’s WebRTC transport.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 (
/call
endpoint) - Your server creates a Daily room with SIP capabilities
- Your server starts the bot process with the room details
- Your server responds to Twilio with TwiML that puts the caller on hold with music
- Upon receiving the
on_dialin_ready
event, the bot forwards the call to the Daily SIP endpoint - The caller and bot are connected, and the bot handles the conversation
Set up your webhook server
Your server acts as the orchestrator between Twilio’s telephony infrastructure and Daily’s WebRTC transport. When Twilio receives a call, it sends a webhook to your server. Your server then:- Extracts call information from Twilio’s webhook (CallSid, caller details)
- Creates a Daily room with SIP capabilities enabled
- Spawns a bot process with the room details and call information
- Responds to Twilio with TwiML that puts the caller on hold with music
- Coordinates the connection between Twilio SIP and Daily WebRTC
Complete Server Implementation
See the full FastAPI server code with Twilio webhook handling and Daily room
creation
Configure your Pipecat bot for dial-in
The bot receives arguments from the server process and uses them to coordinate between Daily’s WebRTC transport and Twilio’s SIP infrastructure. The key components are: Bot Entry Point: The bot receives arguments containing:room_url
: Daily room URL for the WebRTC connectiontoken
: Daily room token for authenticationcall_sid
: Twilio call identifier for SIP forwardingsip_endpoint
: Daily’s SIP endpoint URL for call routing
DailyTransport
for WebRTC:
bot.py
on_dialin_ready
event is crucial. It signals when Daily’s SIP infrastructure is ready to receive the call. At this point, the bot uses Twilio’s API to update the call with new TwiML that forwards the audio from Twilio’s SIP to Daily’s SIP endpoint, completing the connection.
Complete Bot Implementation
See the full bot.py with Daily transport setup and Twilio call forwarding
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/call
(your server’s URL) - HTTP Method: POST
- Webhook:
Run the Example
For local development, you can use ngrok to expose your local server:Complete Setup Instructions
See the full README with step-by-step setup, webhook configuration, and
troubleshooting
Dial-out
Dial-out allows your bot to initiate calls through Twilio’s SIP infrastructure using Daily’s WebRTC transport.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 creates a Daily room with SIP capabilities
- Bot joins the Daily room and sets up the WebRTC transport
- Bot initiates SIP call through Twilio to the target number
- Twilio routes the call to the destination phone number
- The recipient answers and is connected to your bot via Daily WebRTC
- The bot handles the conversation with high-quality WebRTC audio
Set up your server for dial-out
The dial-out server orchestrates outbound calls by coordinating between your application, Daily’s WebRTC transport, and Twilio’s SIP infrastructure. When you trigger a dial-out call, your server:- Receives the dial-out request with target phone number and call parameters
- Creates a Daily room with SIP capabilities and dial-out enabled
- Spawns a bot process with the room details and target phone number
- Initiates the SIP call through Twilio to the destination number
- Manages the connection between Daily WebRTC and Twilio SIP routing
- Handles call events like answered, busy, or failed connections
Complete Server Implementation
See the full FastAPI server code with Daily room creation and SIP dial-out
management
Configure your Pipecat bot for dial-out
The dial-out bot receives arguments from the server process and uses them to initiate outbound calls through Twilio’s SIP infrastructure via Daily’s WebRTC transport. The key components are: Bot Entry Point: The bot receives arguments containing:room_url
: Daily room URL for the WebRTC connectiontoken
: Daily room token for authenticationtarget_number
: Phone number to call via Twilio SIPsip_uri
: Twilio SIP URI for routing the outbound call
DailyTransport
for dial-out:
bot.py
start_dialout()
method initiates the call by connecting Daily’s WebRTC transport to Twilio’s SIP infrastructure. The sip_uri
parameter contains the Twilio SIP endpoint configured with the target phone number, allowing Daily to route the call through Twilio’s telephony network to reach the destination.
Complete Bot Implementation
See the full bot.py with WebRTC transport setup and SIP dial-out configuration
Run the Example
To test dial-out functionality:- Start your server: Run your FastAPI server
- Trigger a call: Make an API request to start a dial-out call
- Answer your phone: The bot will call the specified number via Twilio
- Talk to your bot: Have a conversation with high-quality WebRTC audio
Complete Setup Instructions
See the full README with step-by-step setup, API usage, and SIP configuration
Best Practices
Hold Music for Initialization
Always respond to Twilio’s initial webhook with hold music to give your bot time to initialize:<Pause>
- Twilio’s pause duration is limited and may not provide enough time for Daily SIP setup.
Handle Multiple Events
Use a flag to ensure you only forward calls once when handling multipleon_dialin_ready
events:
Deployment
Local Development
Use ngrok to expose your local server for testing:Pipecat Cloud Deployment
For production deployment on Pipecat Cloud, your webhook server calls the/{agent}/start
endpoint instead of spawning local bot processes:
body
field, which your Pipecat Cloud bot receives as arguments.
Self-Hosted Production Deployment
For self-hosted production deployment, ensure your servers are:- Publicly accessible with HTTPS
- Able to handle concurrent webhook requests
- Properly configured with both Twilio and Daily API credentials
Next Steps
- Explore the complete examples for full implementations
- Learn about Daily PSTN integration for direct phone number provisioning
- Check out Twilio WebSocket integration for simpler telephony workflows