Varmirdocs
Docs/Guides/JavaScript

JavaScript

Use the official varmir package in Node 22+, or call the API with the built-in fetch and FormData.

SDK

The official SDK (Node.js 22+, no dependencies, TypeScript types, MIT, source) wraps every endpoint, throws typed errors, and handles live streaming. It is for servers only: never ship your API key to a browser.

shell
npm i varmir
export VARMIR_API_KEY=mstk_your_key
import { readFile } from "node:fs/promises";
import { Varmir, InsufficientCreditsError } from "varmir";

const client = new Varmir(); // reads VARMIR_API_KEY
try {
  const r = await client.transcribe(await readFile("call.wav"), {
    filename: "call.wav", language: "uk", translateTo: "en",
  });
  console.log(r.text, r.translation?.text);
} catch (e) {
  if (e instanceof InsufficientCreditsError) console.log("Top up credits");
  else throw e;
}

Without the SDK

You don't need any packages. Keep the key in an environment variable, not in code. Packages with names similar to varmir from other publishers aren't ours.

shell
export VARMIR_KEY=mstk_your_key

Transcribe a file (Node)

Save as transcribe.mjs and run node transcribe.mjs.

JavaScript
import { readFile } from "node:fs/promises";

const API = "https://api.varmir.com/api/v1";
const headers = { Authorization: `Bearer ${process.env.VARMIR_KEY}` };

const form = new FormData();
form.append("audio", new Blob([await readFile("meeting.mp3")]), "meeting.mp3");
form.append("translate_to", "en"); // optional

const res = await fetch(`${API}/transcribe`, { method: "POST", headers, body: form });
const body = await res.json();
if (!res.ok) throw new Error(`${body.error.code}: ${body.error.message}`);

console.log(body.language, body.text);
if (body.translation) console.log(body.translation.text);
console.log("charged:", body.charged_sec, "s");

Handle errors

Every error returns {"error": {"code", "message"}}. Retry 429 and 503 after the Retry-After header; fix everything else. See Rate limits & errors.

JavaScript
async function transcribe(blob, filename, fields = {}, retries = 3) {
  for (let attempt = 0; ; attempt++) {
    const form = new FormData();
    form.append("audio", blob, filename);
    for (const [k, v] of Object.entries(fields)) form.append(k, v);

    const res = await fetch(`${API}/transcribe`, { method: "POST", headers, body: form });
    const body = await res.json();
    if (res.ok) return body;

    // body.error = { code, message }
    const retryable = res.status === 429 || res.status === 503;
    if (retryable && attempt < retries) {
      const wait = Number(res.headers.get("Retry-After") ?? 5);
      await new Promise((r) => setTimeout(r, wait * 1000));
      continue;
    }
    // insufficient_credits, invalid_audio, invalid_api_key, ...
    throw new Error(`${body.error.code}: ${body.error.message}`);
  }
}

From the browser

Don't put your API key in front-end code. Anyone can read it there and spend your credits. Send the recording (for example a WebM blob from MediaRecorder) to your own backend, and call Varmir from there with the code above.