Realtime (WebSocket)
wss://…/v1/realtime — OpenAI's Realtime protocol, two-way audio and text, one hold per session.
WSS
wss://iranrouter.com/v1/realtime?model={model}OpenAI's Realtime protocol over WebSocket, relayed byte for byte as the SDK sends and reads it — session.update, input_audio_buffer.append, response.create, … and the server events. Only models the catalogue marks as realtime-capable.
from openai import OpenAI client = OpenAI(api_key="ir-...", websocket_base_url="wss://iranrouter.com/v1") with client.beta.realtime.connect(model="openai/gpt-4o-realtime-preview") as conn: conn.session.update(session={"modalities": ["text"]}) conn.conversation.item.create(item={"type": "message", "role": "user", "content": [{"type": "input_text", "text": "سلام"}]}) conn.response.create() for event in conn: if event.type == "response.text.delta": print(event.delta, end="") elif event.type == "response.done": break
| Topic | Behaviour |
|---|---|
| Auth | Authorization: Bearer ir-…, or from a browser in the subprotocol openai-insecure-api-key.ir-… (as OpenAI's SDK sends it). |
| Billing | A session is one request: $1 (in Rial at the day's rate) is held at connect, usage is summed from every response.done (text, audio and cached tokens at their own rates), and the session settles at close with the rest released. |
| Session budget | When usage reaches 80% of the hold, an error event with code session_budget_reached arrives and the connection closes; reconnect. Sessions last at most 30 minutes (session_max_duration). |
| Key | Credit (wallet) keys only; a token allowance or a free key is refused with model_not_in_plan. Up to 5 sessions at once per user (realtime_session_limit). |
| Input transcription | input_audio_transcription is not offered: it is removed from session.update and an error event with code input_audio_transcription_unsupported arrives; the rest of the update applies. |
| Leaving mid-response | A response still generating when you disconnect is cancelled, and the tokens generated up to that point are charged. |
| Errors | Before closing, an error event in OpenAI's shape arrives: invalid_api_key, model_not_found, model_not_realtime, insufficient_balance, no_capacity, upstream_error. |
Realtime is not in the OpenAPI document (it is a WebSocket). /openai/realtime serves the same session so websocket_base_url works with the SDK prefix.