Skip to main content

Overview

The Realtime API enables low-latency, multi-turn conversations with AI models over WebSocket connections. This is ideal for voice assistants, interactive applications, and real-time chat experiences.
The Realtime API is currently supported on Cloudflare Workers runtime. For Node.js, use the dedicated realtime handler.

Connection

WebSocket Endpoint

Authentication

Pass authentication as query parameters:

Query Parameters

string
required
The provider to use (e.g., openai)
string
required
Your provider API key
string
The model to use (default: gpt-4o-realtime-preview)

Event Types

Client Events

Events sent from your application to the model:
object
Update session configuration
object
Add audio data to the input buffer
object
Commit the audio buffer for processing
object
Add a message to the conversation
object
Trigger a model response
object
Cancel an in-progress response

Server Events

Events sent from the model to your application:
object
Session was successfully created
object
Session configuration was updated
object
A new conversation item was created
object
Audio response chunk
object
Audio response completed
object
Text response chunk
object
Text response completed
object
Response generation completed
object
An error occurred

Example

Basic Text Conversation

Audio Streaming

Best Practices

  • Use PCM16 format at 24kHz sample rate for best compatibility
  • Keep audio chunks around 100ms (2400 samples) for optimal latency
  • Buffer audio on the client side to handle network jitter
  • Implement reconnection logic with exponential backoff
  • Monitor connection health with ping/pong frames
  • Close connections gracefully when done
  • Always handle error events from the server
  • Implement timeout logic for responses
  • Provide fallback behavior for connection failures
  • Use audio compression where appropriate
  • Implement voice activity detection to reduce unnecessary data
  • Cache session configuration to avoid repeated updates

Supported Providers

Realtime API support:
  • OpenAI: Full support with gpt-4o-realtime-preview
  • Azure OpenAI: Supported on compatible deployments
Check provider documentation for model availability and pricing.

Chat Completions

Standard chat API

Audio Speech

Text-to-speech API

Streaming

HTTP streaming guide