Overview
FastAPIWebsocketTransport provides WebSocket support for FastAPI web applications, enabling real-time audio communication over WebSocket connections. It’s primarily designed for telephony integrations with providers like Twilio, Telnyx, and Plivo, supporting bidirectional audio streams with configurable serializers and voice activity detection.
FastAPI WebSocket API Reference
Pipecat’s API methods for FastAPI WebSocket integration
Example Implementation
Complete Twilio telephony integration example
FastAPI Documentation
Official FastAPI WebSocket documentation
Telephony Serializers
Learn about supported FrameSerializers for telephony providers
Installation
To use FastAPIWebsocketTransport, install the required dependencies:Prerequisites
FastAPI Application Setup
Before using FastAPIWebsocketTransport, you need:- FastAPI Application: Set up a FastAPI web application
- WebSocket Endpoint: Configure WebSocket routes for real-time communication
- Telephony Provider: Set up integration with Twilio, Telnyx, or Plivo
- Frame Serializers: Configure appropriate serializers for your telephony provider
Configuration Options
- Serializer Selection: Choose frame serializer based on telephony provider
- Audio Parameters: Configure sample rates and audio formats
- VAD Integration: Set up voice activity detection for optimal performance
- Connection Management: Handle WebSocket lifecycle and reconnections
Key Features
- Telephony Integration: Optimized for Twilio, Telnyx, and Plivo WebSocket streams
- Frame Serialization: Built-in support for telephony provider audio formats
- FastAPI Integration: Seamless WebSocket handling within FastAPI applications
- Bidirectional Audio: Real-time audio streaming in both directions
Configuration
FastAPIWebsocketTransport
The FastAPI WebSocket connection instance.
Transport configuration parameters.
Optional name for the input transport processor.
Optional name for the output transport processor.
FastAPIWebsocketParams
Inherits fromTransportParams with additional WebSocket-specific parameters.
Whether to add WAV headers to outgoing audio frames.
Frame serializer for encoding/decoding WebSocket messages. Use a telephony serializer (e.g.,
TwilioFrameSerializer, TelnyxFrameSerializer) for provider-specific audio formats.Session timeout in seconds. When set, triggers
on_session_timeout if the session exceeds this duration. None disables the timeout.Optional fixed-size packetization for raw PCM audio payloads. Useful when the remote WebSocket media endpoint requires strict audio framing (e.g., 640 bytes for 20ms at 16kHz PCM16 mono).
Usage
FastAPIWebsocketTransport integrates with your FastAPI application to handle telephony WebSocket connections. It works with telephony frame serializers to process audio streams from phone calls. See the complete example for a full implementation including:- FastAPI WebSocket endpoint configuration
- Telephony provider integration setup
- Frame serializer configuration
- Audio processing pipeline integration
Event Handlers
FastAPIWebsocketTransport provides event handlers for client connection lifecycle and session management. Register handlers using the@event_handler decorator on the transport instance.
Events Summary
| Event | Description |
|---|---|
on_client_connected | Client WebSocket connected |
on_client_disconnected | Client WebSocket disconnected |
on_session_timeout | Session timed out |
Connection Lifecycle
on_client_connected
Fired when a client successfully connects to the WebSocket.| Parameter | Type | Description |
|---|---|---|
transport | FastAPIWebsocketTransport | The transport instance |
websocket | WebSocket | The FastAPI WebSocket connection object |
on_client_disconnected
Fired when a client disconnects from the WebSocket.| Parameter | Type | Description |
|---|---|---|
transport | FastAPIWebsocketTransport | The transport instance |
websocket | WebSocket | The FastAPI WebSocket connection object |
on_session_timeout
Fired when a session exceeds the configuredsession_timeout duration. Only fires if session_timeout is set in the params.
| Parameter | Type | Description |
|---|---|---|
transport | FastAPIWebsocketTransport | The transport instance |
websocket | WebSocket | The FastAPI WebSocket connection object |
Additional Resources
- Events Overview - Overview of all events in Pipecat
- Serializers - Frame serializers for telephony providers