Skip to content
Skip to article
View as Markdown

Realtime audio over WebSocket

Use wss://api.dodorouter.com/r/{router_slug}/v1/realtime for a server-to-server speech conversation. Add an OpenAI routing step, select an OpenAI API key, and set the model to gpt-realtime-2.1. If the model is missing from the picker, enter it under Custom…. Send the router API key in the WebSocket handshake. The router selects the upstream model even if the SDK adds a model query parameter. Router authentication · OpenAI model reference.

For Google Gemini models, connect with the Google Gen AI SDK through the router’s native Gemini Live endpoint. Gemini Live audio.

Send a clip and save the spoken reply

This Python example uses websocket-client and the DODO_ROUTER_URL and DODO_ROUTER_API_KEY environment variables. Install the client with python -m pip install websocket-client and supply a mono, 24 kHz, 16-bit PCM WAV file named input.wav with at least 100 ms of speech. It writes a playable reply.wav.

import base64
import json
import os
import wave
from contextlib import closing

from websocket import create_connection

url = os.environ["DODO_ROUTER_URL"].rstrip("/").replace("http", "ws", 1) + "/v1/realtime"
headers = {"Authorization": "Bearer " + os.environ["DODO_ROUTER_API_KEY"]}
pcm = {"type": "audio/pcm", "rate": 24000}

with wave.open("input.wav", "rb") as clip, closing(create_connection(url, header=headers, timeout=60)) as ws:
    assert (clip.getnchannels(), clip.getsampwidth(), clip.getframerate()) == (1, 2, 24000)
    with wave.open("reply.wav", "wb") as reply:
        reply.setparams((1, 2, 24000, 0, "NONE", "PCM"))
        while True:
            event = json.loads(ws.recv())
            if event["type"] == "session.created":
                ws.send(json.dumps({"type": "session.update", "session": {
                    "type": "realtime", "output_modalities": ["audio"],
                    "audio": {"input": {"format": pcm, "turn_detection": None},
                              "output": {"format": pcm}},
                }}))
            elif event["type"] == "session.updated":
                while chunk := clip.readframes(2400):
                    ws.send(json.dumps({"type": "input_audio_buffer.append",
                                        "audio": base64.b64encode(chunk).decode("ascii")}))
                ws.send(json.dumps({"type": "input_audio_buffer.commit"}))
                ws.send(json.dumps({"type": "response.create"}))
            elif event["type"] == "response.output_audio.delta":
                reply.writeframesraw(base64.b64decode(event["delta"]))
            elif event["type"] == "response.done":
                if event["response"]["status"] != "completed":
                    raise RuntimeError(event["response"]["status"])
                break
            elif event["type"] == "error":
                raise RuntimeError(event["error"]["message"])

For a microphone, append chunks as you capture them and play each output delta as it arrives. The example disables voice activity detection so the file boundary ends the turn. With server-side voice activity detection, the provider can detect the end of speech and start its reply. OpenAI Realtime conversations.

Routing and logs

During setup, DodoRouter tries eligible OpenAI Realtime steps in routing order. Once the upstream WebSocket opens, it owns the conversation state. A disconnect ends the session. Open a fresh connection to start another one.

Each provider response.done produces a Realtime log with the terminal status and reported token usage. Set X-Session-Id to group turns under your own ID or use the ID DodoRouter generates for the connection. Set X-Dodo-User-Id to attribute usage to an application user. The log stores usage metadata and omits audio frames and transcripts. Audio pricing.