Skip to main content

1. Fetch Session Token

Call the token endpoint with your API key to get a session token and WebSocket URL.

2. Connect WebSocket

Open the WebSocket connection and send the token as the first message. Handle both text (control) and binary (audio) messages.

3. Microphone Capture

Use the Web Audio API to capture microphone input, downsample to 16kHz if needed, convert to Int16, and send 640-byte frames over the WebSocket.

4. Audio Playback

Convert incoming PCM frames to Float32 and schedule them for gapless playback using AudioBufferSourceNode.
To visualize agent audio, insert an AnalyserNode between the source and destination, then read frequency data with getByteFrequencyData().

5. Complete Example

A minimal standalone HTML page that connects to the Voice API and enables bidirectional voice conversation.