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
The Twilio Media Stream SID.
The associated Twilio Call SID. Required when
auto_hang_up is enabled.Twilio account SID. Required when
auto_hang_up is enabled.Twilio auth token. Required when
auto_hang_up is enabled.Twilio region (e.g.,
"au1", "ie1"). Must be specified together with
edge when base_url is not provided.Twilio edge location (e.g.,
"sydney", "dublin"). Must be specified
together with region when base_url is not provided.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.Configuration parameters for audio and hang-up behavior. See
InputParams below.
InputParams
| Parameter | Type | Default | Description |
|---|---|---|---|
twilio_sample_rate | int | 8000 | Sample rate used by Twilio (Hz). |
sample_rate | int | None | Optional override for pipeline input sample rate. When None, uses the pipeline’s configured rate. |
auto_hang_up | bool | True | Whether to automatically terminate the call on EndFrame or CancelFrame. |
ignore_rtvi_messages | bool | True | Whether to ignore RTVI protocol messages during serialization. |
resampler_clear_after_secs | float | None | 0.2 | Seconds of inactivity after which the stream resampler clears its internal history to avoid audio artefacts. Set to None to never clear (recommended for telephony providers with irregular gaps). |
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, andauth_token. AValueErroris raised at initialization if any are missing. - 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.