Quickstart — Browser
Connect a browser to the Voice Socket: mint a token on your server, open the socket, stream the mic, and play the agent back. Plan for ~30 lines of glue plus audio handling.
A browser can't put your
ek_key on a WebSocket — and you shouldn't anyway. Mint a short-lived token server-side and hand only that to the browser.
Step 1 — Mint a token (your server)
Call this from your backend with a key that has the voice:connect scope. Never ship the ek_ key to the browser.
curl -X POST https://platform.eurekatechlabs.com/v1/voice/tokens \
-H "Authorization: Bearer ek_live_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{ "agentId": "agt_xxxxxxxxxxxx" }'// e.g. an endpoint your front-end calls: GET /api/voice-token
app.get('/api/voice-token', async (req, res) => {
const r = await fetch('https://platform.eurekatechlabs.com/v1/voice/tokens', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.EUREKA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ agentId: 'agt_xxxxxxxxxxxx' }),
});
res.json(await r.json()); // { token, expiresIn, agentId }
});Response:
{ "token": "evt_xxxxx.yyyyy", "expiresIn": 60, "agentId": "agt_xxxxxxxxxxxx" }The token lasts about 60 seconds — mint it right before connecting.
Step 2 — Connect and stream (browser)
Audio is raw PCM: signed 16-bit, 24 kHz, mono, sent as binary frames. Browsers capture at the AudioContext rate (often 48 kHz), so you downsample to 24 kHz before sending and play the agent audio back through an AudioContext.
const AGENT_ID = 'agt_xxxxxxxxxxxx';
const RATE = 24000;
// 1. Get a fresh token from your server
const { token } = await fetch('/api/voice-token').then(r => r.json());
// 2. Open the socket
const ws = new WebSocket(
`wss://core.eurekatechlabs.com/v1/voice/${AGENT_ID}?token=${token}`
);
ws.binaryType = 'arraybuffer';
// 3. Play agent audio (binary frames) + handle events (JSON frames)
const playCtx = new AudioContext({ sampleRate: RATE });
let playHead = playCtx.currentTime;
ws.onmessage = (e) => {
if (typeof e.data === 'string') {
const frame = JSON.parse(e.data);
handleEvent(frame); // see Step 3
return;
}
// Binary = agent voice. Int16 PCM → Float32 → schedule playback.
const pcm = new Int16Array(e.data);
const buf = playCtx.createBuffer(1, pcm.length, RATE);
const ch = buf.getChannelData(0);
for (let i = 0; i < pcm.length; i++) ch[i] = pcm[i] / 32768;
const src = playCtx.createBufferSource();
src.buffer = buf;
src.connect(playCtx.destination);
playHead = Math.max(playHead, playCtx.currentTime);
src.start(playHead);
playHead += buf.duration;
};
// 4. Capture the mic, downsample to 24 kHz Int16, send as binary
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const micCtx = new AudioContext();
const source = micCtx.createMediaStreamSource(stream);
const node = micCtx.createScriptProcessor(4096, 1, 1);
node.onaudioprocess = (ev) => {
if (ws.readyState !== WebSocket.OPEN) return;
const input = ev.inputBuffer.getChannelData(0); // Float32 @ micCtx.sampleRate
const ratio = micCtx.sampleRate / RATE;
const outLen = Math.floor(input.length / ratio);
const pcm = new Int16Array(outLen);
for (let i = 0; i < outLen; i++) {
const s = Math.max(-1, Math.min(1, input[Math.floor(i * ratio)]));
pcm[i] = s < 0 ? s * 32768 : s * 32767;
}
ws.send(pcm.buffer); // binary frame
};
source.connect(node);
node.connect(micCtx.destination);
ScriptProcessorNodeis shown for brevity. In production use anAudioWorklet— it runs the resampling off the main thread and sounds noticeably better.
Step 3 — Handle events
The agent's behaviour is driven by the JSON frames you receive:
function handleEvent(frame) {
switch (frame.type) {
case 'session.created':
console.log('connected', frame.sessionId, frame.audio);
break;
case 'transcript':
console.log(frame.role, frame.text); // user / assistant text
break;
case 'interrupted':
// Caller barged in — STOP playback and flush anything queued.
playHead = playCtx.currentTime;
break;
case 'tool.called':
console.log('tool', frame.name, frame.args);
break;
case 'session.ended':
console.log('ended:', frame.reason);
break;
case 'error':
console.error(frame.code, frame.message);
break;
}
}Handling interrupted is what makes barge-in feel natural: the moment the user speaks over the agent, drop the audio you've queued.
Step 4 — End the session
Send a graceful close, or just close the socket:
ws.send(JSON.stringify({ v: 1, type: 'end' }));
// or: ws.close();Next
- Protocol Reference — every frame, field, and close code
- Connection Auth — tokens vs. server keys
- Limits & Errors — timeouts, caps, close codes
