On this page

Quickstart

Run your first voice session in memory with no account and no microphone, then on your machine with Whisper, then on a cloud provider.

By the end of this page you have run the same code three ways: in memory with nothing installed, with Whisper and Piper on your machine, and on a cloud provider. Only the provider line changes.

Haven't installed the package yet? See Install.

The package is ESM only, and these examples use top-level await, so your project has to run them as ES modules. Add "type": "module" to package.json, for example with npm pkg set type=module, or name the file hello.mts and run it with npx tsx.

The examples also use await using, which Node runs natively only from Node 24. If Node 22 runs the file itself, it stops with SyntaxError: Unexpected identifier 'session'. On Node 22, run the examples with npx tsx.

1. Run a session in memory

The mock() provider runs inside your own process. It has no network, no microphone, and needs no account, so it is the quickest way to see the API.

TypeScript
import { createSession } from "voiceai-sdk";
import { mock } from "voiceai-sdk/mock";

await using session = await createSession(mock());

const heard = await session.transcribe(mock.audio("hello from the session"));
const spoken = await session.speak("Hi there. How can I help?");

console.log(heard.ok, heard.text, spoken.durationMs);

Run it with npx tsx hello.ts or bun hello.ts. You see true, the text, and how long the spoken reply would play.

Three things to notice:

  • await using closes the session when the block ends, even if your code throws.
  • session.transcribe resolves with a result. Audio with no speech sets ok: false and a reason. It does not throw.
  • session.speak returns audio you can await all at once or iterate chunk by chunk.

Mock only understands audio made with mock.audio, which is why this example doesn't load a file. For real speech, use a real engine.

2. Run it on your machine

local() runs whisper.cpp for speech-to-text and Piper for text-to-speech: real models, still no account. Install both and check them with npx voiceai-sdk doctor.

TypeScript
import { createSession } from "voiceai-sdk";
import { local } from "voiceai-sdk/local";

await using session = await createSession(local({ model: "base.en", voice: "en_US-amy-medium" }));

const result = await session.transcribe(await Bun.file("hello.wav").bytes());
console.log(result.ok, result.text);

The only change is the provider import. The first run downloads the base.en model, so it takes a minute. After that you see true and what was said in hello.wav.

3. Hold a conversation

A session can also listen and answer in a loop. session.converse calls your handler for each finished sentence and speaks what it returns.

TypeScript
import { createSession } from "voiceai-sdk";
import { local } from "voiceai-sdk/local";
import { webSocketAudio } from "voiceai-sdk/ws";

Bun.serve({
  port: 3000,
  fetch: (req, server) => (server.upgrade(req) ? undefined : new Response("upgrade required", { status: 426 })),
  websocket: {
    async open(ws) {
      await using session = await createSession(local());
      session.connect(webSocketAudio(ws));

      await session.say`Hi, I'm listening.`;
      await session.converse(async (text) => `You said: ${text}`);
    },
  },
});

Point any client that streams 16 kHz PCM over a WebSocket at ws://localhost:3000. Each time you stop talking, the session repeats what it heard. When the socket closes, converse resolves and the session closes with it.

4. Move to a cloud provider

Swap the import and the factory. Everything after createSession stays the same.

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

await using session = await createSession(openai());

const result = await session.transcribe(await Bun.file("hello.wav").bytes());
console.log(result.ok, result.text);

No extra package. Set OPENAI_API_KEY on your server. See OpenAI Realtime.

Keys stay on the server. Never import a provider in code that ships to the browser.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview