Realtime API (WebSocket)
Enable real-time speech-to-speech and text conversations with AI models via WebSocket.
Overview
The FastRouter Realtime API enables low-latency, multimodal conversations over a persistent WebSocket connection. Unlike the standard chat completions API, where each request is a discrete HTTP round trip, the Realtime API maintains a stateful session — letting you stream audio and text to the model as it happens and receive responses incrementally, token by token and audio chunk by audio chunk.
This makes it the right choice for building voice agents, live transcription-and-response experiences, interactive customer support bots, and any application where conversational latency matters. The API supports speech-to-speech interactions natively, so you can send raw audio and get spoken responses back without stitching together separate STT, LLM, and TTS pipelines.
Because FastRouter exposes the Realtime API through a single gateway endpoint, you get unified billing, observability, and key management across realtime models — using the same API key as the rest of your FastRouter workloads. Sessions are event-driven: you send client events (like session.update or input_audio_buffer.append) and listen for server events (like response.audio.delta) over one connection.
Key capabilities:
Speech-to-speech — stream microphone audio in, receive natural spoken audio out
Text and audio modalities — mix and match input and output formats per response
Function calling — let the model invoke your tools mid-conversation
Voice activity detection — automatic turn detection, or disable it for push-to-talk
Streaming everything — text deltas, audio chunks, and transcripts arrive in real time
Endpoint
wss://go.fastrouter.ai/v1/realtimeConnection
Connect to the WebSocket endpoint with your API key and model as query parameters:
const url = new URL("wss://go.fastrouter.ai/v1/realtime");
url.searchParams.set("model", "openai/gpt-realtime-2.1");
url.searchParams.set("api_key", "sk-v1-...");
const ws = new WebSocket(url.toString());Available Models
openai/gpt-realtime-2.1
Latest GPT Realtime model (recommended)
openai/gpt-realtime-1.5
Previous-generation GPT Realtime model
openai/gpt-realtime-mini
Smaller, lower-cost realtime model
openai/gpt-realtime
Alias for the current GPT Realtime model
Session Configuration
After the connection opens, configure the session by sending a session.update event:
Session Options
modalities
string[]
Output modalities: ["text"], ["audio"], or ["text", "audio"]
voice
string
Voice for audio: alloy, echo, fable, onyx, nova, shimmer
input_audio_format
string
Input audio format: pcm16
output_audio_format
string
Output audio format: pcm16
instructions
string
System instructions for the model
temperature
number
Sampling temperature (0.6–1.2 recommended)
Sending Text Messages
Send text messages using conversation.item.create followed by response.create:
Sending Audio (Streaming)
Stream audio input using input_audio_buffer.append, then commit and request a response:
Audio Format Requirements
Format: 16-bit PCM (little-endian)
Sample Rate: 24,000 Hz recommended
Channels: Mono
Encoding: Base64
Converting Audio to Base64 PCM16
Receiving Responses
Handle incoming events with an onmessage handler:
Playing Audio Output
Event Reference
Client Events (Send)
session.update
Configure session settings
conversation.item.create
Add a message to the conversation
input_audio_buffer.append
Stream audio input chunks
input_audio_buffer.commit
Commit buffered audio as user input
input_audio_buffer.clear
Clear the audio input buffer
response.create
Request a model response
response.cancel
Cancel an in-progress response
Server Events (Receive)
session.created
Session initialized
session.updated
Session configuration applied
response.created
Response generation started
response.text.delta
Streaming text chunk
response.text.done
Text response complete
response.audio.delta
Streaming audio chunk (Base64 PCM16)
response.audio.done
Audio response complete
response.audio_transcript.delta
Streaming transcript of audio
response.done
Full response complete (includes usage)
error
Error occurred
Voice Activity Detection (VAD)
By default, VAD is enabled and the API automatically detects when users start and stop speaking. For push-to-talk interfaces, disable VAD:
Note: With VAD disabled, you must manually call
input_audio_buffer.committo finalize audio input,response.createto trigger a response, andinput_audio_buffer.clearbefore starting new input.
Function Calling
Define functions the model can call:
When the model calls a function, handle it and provide results:
Complete Example
Last updated
