Overview
TwilioFrameSerializer enables integration with Twilio’s Media Streams WebSocket protocol, allowing your Pipecat application to handle phone calls via Twilio’s voice services with bidirectional audio conversion, DTMF event handling, and automatic call termination.
Twilio Serializer API Reference
Pipecat’s API methods for Twilio Media Streams integration
Example Implementation
Complete telephony examples with Twilio
Twilio Documentation
Official Twilio Media Streams documentation
Twilio Console
Manage phone numbers and Media Stream configuration
Installation
TheTwilioFrameSerializer does not require any additional dependencies beyond the core Pipecat library:
Prerequisites
Twilio Account Setup
Before using TwilioFrameSerializer, you need:- Twilio Account: Sign up at Twilio Console
- Phone Number: Purchase a Twilio phone number with voice capabilities
- Media Streams: Configure your phone number to use Media Streams
- Webhook Configuration: Set up webhook endpoints for call handling
Required Environment Variables
TWILIO_ACCOUNT_SID: Your Twilio Account SID for authenticationTWILIO_AUTH_TOKEN: Your Twilio Auth Token for API operations
Required Configuration
- Stream SID: Provided by Twilio during Media Stream connection
- Call SID: Required for automatic call termination (optional)
Key Features
- Bidirectional Audio: Convert between Pipecat and Twilio audio formats
- DTMF Handling: Process touch-tone events from callers
- Auto Hang-up: Terminate calls via Twilio’s REST API
- μ-law Encoding: Handle Twilio’s standard audio encoding format
Configuration
str
required
The Twilio Media Stream SID.
str
default:"None"
The associated Twilio Call SID. Required when
auto_hang_up is enabled.str
default:"None"
Twilio account SID. Required when
auto_hang_up is enabled.str
default:"None"
Twilio Auth Token. When
auto_hang_up is enabled, provide either this or
api_key_sid and api_key_secret, not both.str
default:"None"
Twilio API key SID (starts with
SK). Alternative to auth_token for auto
hang-up. Keyword-only; must be provided with api_key_secret.str
default:"None"
Twilio API key secret. Keyword-only; must be provided with
api_key_sid.str
default:"None"
Twilio region (e.g.,
"au1", "ie1"). Must be specified together with edge
when base_url is not provided.str
default:"None"
Twilio edge location (e.g.,
"sydney", "dublin"). Must be specified
together with region when base_url is not provided.str
default:"None"
REST API base URL (scheme + host, e.g.,
https://api.twilio.com) used for
auto hang-up. Defaults to the host derived from region/edge. Set this to
target a Twilio-API-compatible backend or a self-hosted server.InputParams
default:"None"
Configuration parameters for audio and hang-up behavior. See
InputParams below.
InputParams
Usage
Basic Setup
Without Auto Hang-up
Notes
- Auto hang-up requires credentials: When
auto_hang_upis enabled (the default), you must providecall_sid,account_sid, and eitherauth_tokenor bothapi_key_sidandapi_key_secret. AValueErroris raised at initialization if any are missing, ifauth_tokenis combined with any part of an API key, or if only one ofapi_key_sidandapi_key_secretis given. Credentials must belong to the configuredregion, since each Twilio region has its own Auth Token and API keys. - Region and edge pairing: When
base_urlis not provided, bothregionandedgemust be specified together if either is set. Twilio’s API uses the FQDN formatapi.{edge}.{region}.twilio.com. This requirement does not apply whenbase_urlis provided, sincebase_urlis used verbatim and ignoresregion/edge. - Audio format: Twilio uses 8kHz mu-law (PCMU) audio encoding. The serializer automatically converts between this format and Pipecat’s PCM audio.
- DTMF support: Touch-tone digit events from callers are converted to
InputDTMFFrameobjects.