examples/realtime/twilio/README.md
This example demonstrates how to connect the OpenAI Realtime API to a phone call using Twilio's Media Streams. The server handles incoming phone calls and streams audio between Twilio and the OpenAI Realtime API, enabling real-time voice conversations with an AI agent over the phone.
Start the server:
uv run server.py
The server will start on port 8000 by default.
Expose the server publicly, e.g. via ngrok:
ngrok http 8000
Note the public URL (e.g., https://abc123.ngrok.io)
Configure your Twilio phone number:
https://your-ngrok-url.ngrok.io/incoming-call/incoming-call/media-streamTwilioRealtimeTransportLayer class owns the WebSocket message handling:
PORT environment variable (default: 8000)OPENAI_API_KEY environment variableRealtimeAgent configuration in server.pyserver.pyPhone Call → Twilio → WebSocket → TwilioRealtimeTransportLayer → OpenAI Realtime API
↓
RealtimeAgent with Tools
↓
Audio Response → Twilio → Phone Call
The TwilioRealtimeTransportLayer acts as a bridge between Twilio's Media Streams and OpenAI's Realtime API, handling the protocol differences and audio format conversions. It wraps the OpenAI realtime model to provide a clean interface for Twilio integration.