- Your server calls
GET /v1/tokenwith your API key to mint a short-lived temporary token. - Your browser opens the WebSocket with
?token=<token>, no API key exposed. - The browser sends one
session.updatewith youragent_id; the agent’s stored prompt, voice, and tools load automatically.
This page connects to a stored agent by
agent_id — the recommended path. If you’d rather configure the agent inline per session instead of creating one, send system_prompt / greeting / output in the session.update and omit agent_id. The two are mutually exclusive. See Inline configuration.1. Generate a token on your server
CallGET /v1/token with your API key in the Authorization header. Pick an expires_in_seconds short enough to limit replay risk (60–300s is a good default) and an optional max_session_duration_seconds to cap the session length.
These two parameters control different things and are easy to confuse:
expires_in_secondsis the token redemption window: how long the client has to use this token to open a WebSocket. If the window elapses before the WebSocket is opened, the server returns asession.errorwith codeunauthorizedon the first frame instead ofsession.ready. Once asession.readyhas been received, this value no longer applies.max_session_duration_secondsis the session duration cap: how long the resulting voice agent session is allowed to run after the WebSocket is open.
expires_in_seconds must be between 1 and 600. max_session_duration_seconds must be between 60 and 10800 (defaults to 10800, the 3-hour maximum session duration).session.resume. End sessions cleanly with session.end so you don’t pay for the 30-second resume grace window.
2. Connect from the browser with the token
Fetch the token from your server, open the WebSocket with?token=<token> (no Authorization header needed), and bind to your agent by agent_id:
3. Browser quickstart
A complete working example that captures microphone audio, streams it to the Voice Agent API, and plays back the agent’s response. This requires two files, an HTML page and an AudioWorklet processor.AudioWorklet processors load from a URL, so this needs two files. Serve them locally with
npx serve ..pcm-processor.js in the same directory as your HTML file:
4. Browser compatibility
The quickstart above works as-is on Chromium-based browsers (Chrome, Edge, Brave, Arc) and Firefox. Safari has a known quirk that produces silently garbled audio if you don’t account for it.Safari: resample inside the worklet
Safari ignores thesampleRate constructor option, so an AudioContext({ sampleRate: 24000 }) will silently run at 48 kHz on most Macs. Sending those samples to the Voice Agent API as if they were 24 kHz produces audio that sounds chipmunked or garbled.
Detect the actual context rate at runtime, send it into the worklet, and resample there:
createBuffer(1, length, 24000) works on all current browsers — the context resamples on output. Linear interpolation is good enough for speech.
Cross-browser checklist
- User gesture required. All major browsers gate
getUserMediaandAudioContextstartup behind a user gesture (Safari is strictest). Start audio inside aclickortouchstarthandler and callawait audioCtx.resume()before connecting nodes. - HTTPS or
localhost.getUserMediaonly works on secure origins. - Echo cancellation. Pass
echoCancellation: truetogetUserMediaso the agent’s TTS playing through the speakers doesn’t get re-captured by the mic. - Audio output sink. On iOS Safari, set the
<audio playsinline>attribute or route through anAudioContextdestination. Autoplay and full-screen behavior differ from desktop.
5. Ending the session cleanly
In the browser, wiresession.end to both your explicit end-call control and the pagehide event so tab close and navigation are covered. See Ending the session cleanly on the Voice Agent API overview for the full pattern and code sample.