Realtime WebSocket

A bidirectional session for live speech-to-speech conversation.

GEThttps://your-instance.com/v1/realtime

Upgrades to a WebSocket instead of returning a body. Audio flows both ways over the same connection, so the model can be interrupted mid-sentence — which is what makes a spoken conversation feel like one.

Connecting

Pass the model as a query parameter and your key as a bearer token. Browsers cannot set headers on a WebSocket handshake, so authenticate from your server and relay, rather than putting a key in front-end code.

import WebSocket from "ws";

const ws = new WebSocket(
  "wss://your-instance.com/v1/realtime?model=gpt-realtime",
  { headers: { Authorization: `Bearer ${process.env.CLAWROUTER_API_KEY}` } },
);

ws.on("open", () => {
  ws.send(JSON.stringify({
    type: "session.update",
    session: { modalities: ["text", "audio"], voice: "alloy" },
  }));
});

ws.on("message", (raw) => {
  const event = JSON.parse(raw.toString());
  if (event.type === "response.audio.delta") {
    // event.delta is base64 PCM — append it to your playback buffer
  }
});

Event flow

DirectionEventMeaning
→ sendsession.updateSet voice, modalities and turn detection
→ sendinput_audio_buffer.appendPush a chunk of microphone audio
→ sendresponse.createAsk for a reply now
← receiveresponse.audio.deltaA chunk of spoken reply
← receiveresponse.text.deltaA chunk of the transcript
← receiveresponse.doneThe turn is complete