Quickstart — Browser
Put a browser on a real phone call: mint a token on your server, open the socket, dial, then stream the mic and play the far end back.
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.
Before you start: your product needs the Direct Call capability enabled (product page → Call Settings), with a number assigned to it. Leaving AI Agent enabled at the same time is fine — the two run side by side on the same numbers.
Step 1 — Mint a token (your server)
Call this from your backend with a key that has direct_call:connect. Add calls:write to the same key if the browser should be able to place calls.
curl -X POST https://platform.eurekatechlabs.com/v1/direct-call/tokens \
-H "Authorization: Bearer ek_live_YOUR_KEY"// e.g. an endpoint your front-end calls: GET /api/direct-call-token
app.get('/api/direct-call-token', async (req, res) => {
// Your own authorization goes here — decide whether THIS user is allowed
// to make calls before you hand out a token.
const r = await fetch('https://platform.eurekatechlabs.com/v1/direct-call/tokens', {
method: 'POST',
headers: { 'Authorization': `Bearer ${process.env.EUREKA_API_KEY}` },
});
res.json(await r.json()); // { token, expiresIn, productId }
});Response:
{ "token": "evt_xxxxx.yyyyy", "expiresIn": 60, "productId": "9663e67e-9181-…" }The token lasts about 60 seconds — mint it right before connecting. It is scoped to the product, so anyone holding it can dial from your numbers: do your own permission check before issuing one.
Step 2 — Connect and dial
const RATE = 24000;
const { token, productId } = await fetch('/api/direct-call-token').then(r => r.json());
const ws = new WebSocket(`wss://core.eurekatechlabs.com/v1/direct-call/${productId}?token=${token}`);
ws.binaryType = 'arraybuffer';
const playCtx = new AudioContext({ sampleRate: RATE });
let playHead = playCtx.currentTime;
let callActive = false;
ws.onmessage = (e) => {
// Binary = the other person's voice.
if (typeof e.data !== 'string') return playPcm(e.data);
const frame = JSON.parse(e.data);
switch (frame.type) {
case 'call.ready':
if (!frame.permissions.includes('calls:write')) {
return console.warn('This token cannot place calls.');
}
ws.send(JSON.stringify({
type: 'dial',
from: '+60123456789', // must belong to this product
to: '+60198765432',
}));
break;
case 'ringing':
startRingbackTone(); // we don't send ringback — play your own
break;
case 'answered':
stopRingbackTone();
callActive = true;
startMic();
break;
case 'call.failed': // busy / no_answer / rejected / carrier_error
stopRingbackTone();
console.log('Call failed:', frame.reason);
break;
case 'call.ended':
callActive = false;
console.log('Call ended:', frame.reason);
break;
case 'error':
console.error(frame.code, frame.message);
break;
}
};Step 3 — Send the microphone
Browsers capture at the AudioContext rate (often 48 kHz), so downsample to 24 kHz and send binary frames. Send steadily — about one 20 ms chunk at a time. Sending faster than real time gets audio dropped.
async function startMic() {
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true, // essential — both ends are live mics
noiseSuppression: true,
autoGainControl: true,
},
});
const ctx = new AudioContext({ sampleRate: RATE });
const src = ctx.createMediaStreamSource(stream);
const node = ctx.createScriptProcessor(2048, 1, 1);
node.onaudioprocess = (e) => {
if (!callActive || ws.readyState !== WebSocket.OPEN) return;
const input = e.inputBuffer.getChannelData(0);
const pcm = new Int16Array(input.length);
for (let i = 0; i < input.length; i++) {
const s = Math.max(-1, Math.min(1, input[i]));
pcm[i] = s < 0 ? s * 0x8000 : s * 0x7fff;
}
ws.send(pcm.buffer);
};
src.connect(node);
node.connect(ctx.destination);
}Insist on headsets. Echo cancellation helps, but with speakers the far end still hears themselves. This is the single most common complaint about browser calling, and a headset policy fixes it outright.
Step 4 — Play the far end
function playPcm(buffer) {
const pcm = new Int16Array(buffer);
const audio = playCtx.createBuffer(1, pcm.length, RATE);
const channel = audio.getChannelData(0);
for (let i = 0; i < pcm.length; i++) channel[i] = pcm[i] / 0x8000;
const src = playCtx.createBufferSource();
src.buffer = audio;
src.connect(playCtx.destination);
// Queue back-to-back so chunks don't overlap or gap.
playHead = Math.max(playHead, playCtx.currentTime);
src.start(playHead);
playHead += audio.duration;
}Step 5 — Hang up
ws.send(JSON.stringify({ type: 'hangup' }));Closing the socket also ends the call — a live call with nobody listening is hung up rather than left running.
What happens after
The call bills like any other, appears in call history, and fires the
call.ended webhook with the transcript field
empty (there is no AI transcribing it) and a recording if you enabled
Record calls on the product.
Next
- Protocol reference — every frame and error code.
