Skip to content

Speech-to-text in Node.js: Whisper, a cloud API, or the browser

Compare three places to run speech-to-text for a Node.js app: Whisper on your own machine, a cloud API, and the browser's Web Speech API, with the trade-offs and code for each.

A Node.js app can run speech-to-text in three places: a local Whisper model on your own server, a cloud speech-to-text API, or the browser's Web Speech API on the user's device. Whisper keeps audio on your machine and needs no account, a cloud API needs a key and sends audio to the provider, and the browser API runs in front-end code with support that varies by browser. With VoiceAI SDK, the first two use the same transcribe and listen calls, so switching between them is a one-line change.

The Node.js samples are written against the planned 1.0 API of VoiceAI SDK, a developer preview, and were not run against live provider accounts. Nothing here was benchmarked. Speed and accuracy depend on the model, the provider, the language and your audio, so the comparison below describes trade-offs, not results.

The three options at a glance

Local WhisperCloud APIBrowser Web Speech API
Where you call itYour server, with local()Your server, for example with deepgram()Browser code only
Account and keyNoneAn API key on your serverNone
Where audio goesStays on your machine once the model is downloadedTo the providerDepends on the browser
Live partial transcriptsYes, from a sliding windowDepends on the providerDepends on the browser
SpeedDepends on your hardware and model sizeDepends on the provider and the networkDepends on the browser and device

The first two run in your Node process and share one API. The third never touches your server unless you send the text there yourself. That makes it a different kind of choice: it suits captions and form input in a web page, not a phone line or a server-side agent.

Transcribe a file with Whisper on your machine

local() from voiceai-sdk/local runs whisper.cpp as a child process and pipes audio to it. Install whisper-cli, put it on PATH, and check it with doctor. The CLI can transcribe a file before you write any code:

npm install voiceai-sdk
npx voiceai-sdk doctor
npx voiceai-sdk transcribe call.wav --provider local

The same thing from TypeScript:

import { readFile } from "node:fs/promises";
import { createSession } from "voiceai-sdk";
import { local } from "voiceai-sdk/local";

await using session = await createSession(local({ model: "base.en" }));

const result = await session.transcribe(await readFile("call.wav"));
console.log(result.ok ? result.text : `no transcript: ${result.reason}`);

The first run downloads the model to ~/.cache/voiceai-sdk. Larger Whisper models are more accurate and slower, and which one fits depends on your machine. Run Whisper and Piper locally from TypeScript covers models, voices and CI.

Use a cloud speech-to-text API

To move to a cloud provider, change the import and set the provider's key on the server, here DEEPGRAM_API_KEY. The result has the same shape: { ok, text, words, language, durationMs, reason? }.

import { readFile } from "node:fs/promises";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";

await using session = await createSession(deepgram(), { language: "en" });

const result = await session.transcribe(await readFile("call.wav"));
for (const word of result.words) {
  console.log(word.startMs, word.endMs, word.text, word.confidence);
}

words holds each word's start and end in milliseconds and a confidence score, when the provider reports word timings. Other speech-to-text providers in the SDK include AssemblyAI, Gladia, Speechmatics, Google Cloud, Azure and AWS. How to choose a speech-to-text API lists what to check before you pick one.

Stream live audio with listen

For a microphone or a call, use listen. It takes an AsyncIterable<Uint8Array> or a ReadableStream of raw audio and yields partial transcripts while the speaker talks and a final one when they stop. Tell the session the format with sampleRate and encoding.

import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";

await using session = await createSession(deepgram(), { sampleRate: 16000, encoding: "pcm16" });

// Raw 16 kHz pcm16 audio arrives on stdin.
for await (const t of session.listen(process.stdin)) {
  if (t.type === "partial") process.stdout.write(`\r${t.text}`);
  else console.log(`\r[${t.startMs}-${t.endMs} ms] ${t.text}`);
}

Swap deepgram() for local() and the loop stays the same. With local(), partials come from re-running the model on a sliding window, so they arrive less often than from cloud providers. Check session.capabilities.streamingStt before you build on partials.

Recognize speech in the browser

The Web Speech API is a browser API, not a Node.js one. Your front-end code starts recognition and gets text back, with no server of yours involved. Support varies by browser, and so do where the audio is processed and which languages are available, so plan a fallback for users whose browser doesn't support it.

If you need the same behavior for every user, or the transcript has to reach a server-side agent, stream the microphone to your server instead. webSocketAudio(ws) from voiceai-sdk/ws attaches a WebSocket of raw audio to a session, and the provider key never leaves the server. See Audio streaming.

Handle silence without try/catch

Audio with no speech is a result, not an error. transcribe resolves with ok: false and a reason:

  • no_speech: silence, hold music, or noise with no words in it.
  • timeout: recognition didn't finish in time. Retry, or split long audio.
  • unsupported_audio: the provider can't read the audio. Convert it first.

The SDK throws a VoiceError only when the provider or the session fails, with a code such as auth, rate_limited or network. That keeps the common case, a file with nothing in it, out of your error handling.

Common questions

Can I run Whisper from Node.js without a cloud account? Yes. local() drives the whisper-cli binary from whisper.cpp as a child process. Your Node code needs the binary on PATH, or its location passed as whisperPath, and a model, which is downloaded on first use.

Is the Web Speech API available in Node.js? No. It is part of the browser. On a server, use a local model or a cloud API.

Which option is the most accurate? It depends on the model, the provider, the language and the audio. VoiceAI SDK will publish benchmark results with 1.0. Until then, run a few of your own recordings through each option with npx voiceai-sdk transcribe and compare the text.

What audio format should I send? Match your source. The docs suggest 16 kHz pcm16 for microphones and browsers, and 8 kHz mulaw for phone calls. If a provider can't accept the format, createSession throws VoiceError with code unsupported instead of guessing.

Next steps

More guides on the VoiceAI SDK blog, or start with the quickstart.