Function
Create FastAPI app with WebSocket endpoint for AI Voice Agent communication.
create_app()
Create FastAPI application with WebSocket support.Parameters:
api_key_validator(Callable[[str], bool]): Function to validate API keysagent_session(AgentSession | None): Optional AgentSession configuration (recommended)config(ServerConfig | None): Optional server configuration (fallback)
FastAPI: FastAPI application with WebSocket endpoint at/ws
Parameters
api_key_validator
Function to validate API keys from client connections.Parameters:
api_key(str): API key provided by client
bool: True if API key is valid, False otherwise
agent_session
AgentSession configuration. If provided, takes precedence over
config parameter.Recommended: Use AgentSession for configuring your AI Voice Agent.Example:config
Server configuration. Used as fallback if
agent_session is not provided.Note: Prefer using agent_session for new code.WebSocket Endpoint
The server exposes a WebSocket endpoint at/ws that accepts:
-
Client Messages:
client_text- Text messagesclient_audio_start- Start audio streamingclient_audio_chunk- Audio data chunksclient_audio_end- End audio streaming
-
Server Messages:
server_connected- Connection confirmedserver_text- Text responsesserver_partial- Streaming text responsesserver_stt- Speech-to-text transcriptsserver_tool_call- Tool execution notificationsserver_tool_result- Tool execution resultsserver_error- Error messages
Usage Examples
Basic Server Setup
Server with Custom API Key Validation
Server with Tools
Running the Server
Using uvicorn
Command Line
Related
- AgentSession → - Agent configuration
- ServerConfig → - Server configuration
- Protocol → - WebSocket protocol reference
- Quickstart → - Connect your existing API to an AI Voice Agent

