Skip to main content

Realtime API

The realtime endpoints give applications a low latency, bidirectional session with a model, for voice assistants and live transcription. The gateway serves the OpenAI realtime protocol, a WebSocket at /v1/realtime, and HTTP endpoints for WebRTC calls, ephemeral client secrets and transcription sessions. Sessions are authenticated with a virtual key, and their usage is costed from the audio and text token counts the provider reports, so voice traffic shows up in the same budgets as text.

Scenario​

A company builds a voice assistant that runs in the browser. The browser must never hold the long lived virtual key, so the application server asks the gateway for a short lived client secret and hands it to the browser, which opens a WebRTC call through the gateway. Each session is logged as one request with its audio usage, and the team's key budget caps what the assistant can spend.

Steps​

  1. Register a realtime model. In the Admin UI open Models + Endpoints (/ui/models-and-endpoints), click Add Model, pick the Provider, choose the model, set the Public Model Name to gpt-realtime and set Mode to Realtime - /realtime, then click Add Model.

    model_list:
    - model_name: gpt-realtime
    spanway_params:
    model: openai/gpt-realtime
    api_key: os.environ/OPENAI_API_KEY
    model_info:
    mode: realtime
  2. Create a key in Virtual Keys (/ui/api-keys) and pick the model under Models.

  3. From your server, mint a client secret for the browser.

    curl -X POST http://localhost:4000/v1/realtime/client_secrets \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"session": {"type": "realtime", "model": "gpt-realtime"}, "expires_after": {"anchor": "created_at", "seconds": 600}}'
  4. In the browser, open the WebRTC call with the secret as the bearer token and the SDP offer as the body, POST /v1/realtime/calls with Content-Type: application/sdp. The gateway answers with the provider's SDP answer. For a WebSocket client instead, connect to ws://localhost:4000/v1/realtime?model=gpt-realtime with the virtual key in the Authorization header and exchange realtime events.

  5. Try a session in the browser. Open Playground (/ui/playground), set Endpoint Type to /v1/realtime, pick the model and a Voice, click Connect and talk or type. Then open Logs (/ui/logs) to see the session with its usage.

Expected result​

The client secret call answers with {"value": "ek_9e0f1a2b", "expires_at": 1760441834, "session": {...}}. The call exchange answers 200 with an SDP body, and the session appears in Logs as one request once it ends, with the aggregated audio and text usage and cost.

WebSocket sessions​

Connect to ws://localhost:4000/v1/realtime?model=<model_name> with the virtual key in the Authorization header, then exchange realtime events as defined by the OpenAI protocol. The gateway relays events to the provider and back, records usage when the session ends, and applies the key's budget. Clients that send the key in the api-key header, as older realtime clients do, are accepted too. The model query parameter is required. Without it the gateway closes the WebSocket with code 1008 and the reason model query parameter is required. The one exception is intent=transcription, which uses gpt-realtime-whisper when no model is given. Two more query parameters are accepted: intent names the kind of session, such as transcription, and guardrails takes a comma-separated list of guardrail names to run on the session, for example ws://localhost:4000/v1/realtime?model=gpt-realtime&guardrails=pii-mask.

WebRTC and client secrets​

MethodPathSummary
POST/v1/realtime/callsStart a WebRTC realtime call
POST/v1/realtime/client_secretsCreate an ephemeral realtime client secret
POST/v1/realtime/transcription_sessionsCreate a realtime transcription session
GET/v1/realtimeOpen a realtime voice or transcription WebSocket session

POST /v1/realtime/client_secrets returns a short lived secret for the browser, so the long lived virtual key stays on your server. The body must name the model, there is no default. The secret must be minted with a virtual key, because it carries that key. POST /v1/realtime/calls exchanges the browser's SDP offer for the provider's answer through the gateway and is authenticated with the client secret, not with a virtual key. Each call runs as the virtual key that minted the secret: its budget, rate limits, model list and blocked state are checked again on the call, so blocking or deleting the key also stops the secrets it minted. POST /v1/realtime/transcription_sessions starts a transcription only session.

Who changed what​

Minting a client secret, through client_secrets or transcription_sessions, writes an audit row on the key with the model, the session type and the expiry, never the secret. Read it on the Logs page, Audit Logs tab, see audit logs.

Newer OpenAI session routes​

OpenAI session routes the gateway has no native handler for, such as /v1/live/sessions, can be reached through the OpenAI pass-through at /openai/v1/live/sessions, over HTTP or WebSocket. The pass-through checks the virtual key and logs the call, but model routing and fallbacks do not apply there, so prefer the native realtime routes above when they cover what you need.

Gateway features​

Realtime sessions are logged as one request with the aggregated usage, so they appear on the Logs page and count against budgets. Guardrails with the realtime_input_transcription mode screen the transcribed user audio. Realtime models are available from OpenAI, Azure OpenAI, Gemini, Vertex AI and xAI.

Errors​

StatusWhat it meansFix
400 The request names no realtime model. Send session.model or model.The client_secrets body names no model.Send session.model or model.
400 A realtime client secret can only be created with a virtual key.The secret was requested without a virtual key, for example with the master key.Mint it with a virtual key.
400 on client_secrets from the providerexpires_after is out of range.Use a shorter expiry.
401 {"error": "Missing or invalid Authorization header"} on /v1/realtime/callsNo Authorization: Bearer header.Send the client secret as the bearer token.
401 {"error": "Invalid or expired token"} on /v1/realtime/callsThe bearer token is not a client secret from this gateway, or was minted before secrets were bound to keys.Mint a new secret.
401 {"error": "Token has expired"} on /v1/realtime/callsThe secret's expires_at has passed.Mint a new secret.
401, 403 or 429 in the OpenAI error shape on /v1/realtime/callsThe key that minted the secret is blocked, expired, deleted, over budget, over its rate limit or no longer allowed the model.Fix the key, then mint a new secret.
401 AuthenticationError on the WebSocketNo key in Authorization or api-key, or the key is expired. The socket is closed right after the handshake.Send a live virtual key.
403 PermissionDeniedErrorThe key may not use the model, or a guardrail blocked the transcribed audio.Add the model to the key, or read the guardrail result in Logs.
429 RateLimitErrorBudget or rate limit reached.Raise the limit on the key.
The Playground shows Connection failedThe browser could not open the WebSocket, usually because the gateway sits behind a proxy that does not forward WebSockets.Allow WebSocket upgrades on the load balancer.

Providers​

The gateway's endpoint support matrix marks 4 provider integrations as supporting this endpoint:

Google AI Studio - Gemini, OpenAI, Google - Vertex AI, xAI.