Browser microphone capture
Pipe a browser microphone into the live stream with a short-lived stream token. Your API key stays on your server; no extra libraries.
Stream token
Never put the API key in browser code. Your server calls POST /api/v1/stream/token with the key and passes the returned token to the page. The token is valid for 60 seconds and only opens WS /api/v1/stream; the stream is billed to the key that issued it. Issuing a token is free (30 per minute per key).
// Your server (Node + Express): the API key never leaves it.
app.post("/stream-token", requireLogin, async (req, res) => {
const r = await fetch("https://api.varmir.com/api/v1/stream/token", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.VARMIR_API_KEY}` },
});
res.status(r.status).json(await r.json()); // { token, expires_in: 60, expires_at }
});// Browser
const { token } = await (await fetch("/stream-token", { method: "POST" })).json();
const ws = new WebSocket("wss://api.varmir.com/api/v1/stream");
ws.binaryType = "arraybuffer";
ws.onopen = () => ws.send(JSON.stringify({
type: "config", token, sample_rate: 16000, language: "uk", translate_to: "en",
}));
ws.onmessage = (e) => {
const m = JSON.parse(e.data);
if (m.type === "config_ok") startMic(); // capture below
if (m.type === "partial") showDraft(m.text);
if (m.type === "final") appendLine(m.text);
if (m.type === "translation") appendTranslation(m.segment, m.text);
};
// Stop: ws.send(JSON.stringify({ type: "end" })), then wait for "done".Permissions
getUserMedia needs a secure context (HTTPS or localhost). The first call shows the browser's mic permission prompt; the user's choice is sticky per origin.
Capture & downsample
Create an AudioContext at 16 kHz (the model's native rate) so the browser does the resampling for you.
const media = await navigator.mediaDevices.getUserMedia({ audio: true });
const ctx = new AudioContext({ sampleRate: 16000 });
const source = ctx.createMediaStreamSource(media);
const proc = ctx.createScriptProcessor(4096, 1, 1);
source.connect(proc);
proc.connect(ctx.destination);
proc.onaudioprocess = (e) => sendChunk(e.inputBuffer.getChannelData(0));Send to /stream
The wire format is pcm_s16le. Convert the Float32 frame from Web Audio and ship it over the open WebSocket.
function sendChunk(float32) {
// Convert Float32 [-1, 1] → Int16 little-endian
const i16 = new Int16Array(float32.length);
for (let i = 0; i < float32.length; i++) {
i16[i] = Math.max(-1, Math.min(1, float32[i])) * 0x7fff;
}
ws.send(i16.buffer);
}