On this page

ElevenLabs

Speak in ElevenLabs voices and transcribe recordings with elevenlabs(): streaming text-to-speech with word timings, and batch speech-to-text. No ElevenLabs package needed.

elevenlabs() streams text-to-speech over ElevenLabs' WebSocket input API, so you can send text as your model writes it. transcribe uses ElevenLabs' speech-to-text endpoint.

Install

npm install voiceai-sdk

Set ELEVENLABS_API_KEY on your server, and keep it out of browser bundles.

Quickstart

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

await using session = await createSession(elevenlabs({ voice: process.env.VOICE_ID }));
const result = await session.transcribe(await Bun.file("meeting.wav").bytes());
console.log(result.ok, result.text);

This prints true and the transcript. If the file has no speech, ok is false and reason is "no_speech". await using closes the session when the block ends.

What it supports

FeatureSupportNotes
Speech-to-text (transcribe)YesBatch only, with transcribe
Streaming transcripts (listen)NoPair with a streaming provider in a pipeline
Text-to-speech (speak)Yes
Streaming speechYes
Speech-to-speechNo
Word timingsYesFrom the alignment data sent with each audio chunk
Speaker labelsYes
InterruptionsYes
Voice list (listVoices)YesYour voice library, including cloned voices
pause() and resume()No

The capability matrix compares these with every other provider.

Transcribe recordings

transcribe accepts bytes, a Blob, or a stream, and resolves when the whole file is processed.

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

await using session = await createSession(elevenlabs({ voice: process.env.VOICE_ID }));
const result = await session.transcribe(await Bun.file("voicemail.mp3").bytes());

for (const word of result.words) console.log(word.startMs, word.text);

Speak

Iterate session.speak to send audio as it is produced. Pass an async iterable of text, such as a model's output, and speech starts before the text is finished.

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

await using session = await createSession(elevenlabs({ voice: process.env.VOICE_ID }));

for await (const chunk of session.speak("Let me check that for you.")) {
  player.write(chunk.data);
}

Voices

listVoices returns the voices you can pass as voice.

TypeScript
import { listVoices } from "voiceai-sdk";
import { elevenlabs } from "voiceai-sdk/elevenlabs";

const voices = await listVoices(elevenlabs());
console.log(voices.filter((v) => v.language.startsWith("en")).map((v) => v.id));

Use it in a pipeline

ElevenLabs doesn't stream transcripts. For a conversation, combine it with a provider that streams transcripts.

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

await using session = await createSession({ stt: deepgram(), tts: elevenlabs({ voice: process.env.VOICE_ID }) });
await session.converse(async (text) => answer(text));

Configuration

Pass options to elevenlabs(). Options that have an environment variable fall back to it.

OptionTypeDefaultEnv var
apiKeystringELEVENLABS_API_KEY
voicestringRequired for speak
modelstring"eleven_flash_v2_5"
sttModelstring"scribe_v1"
stabilitynumberVoice default

Call the API directly

session.native is the ElevenLabs text-to-speech socket while speech is streaming.

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

await using session = await createSession(elevenlabs({ voice: process.env.VOICE_ID }));
const speech = session.speak("Hello there.");
session.native.send(JSON.stringify({ text: " ", flush: true }));
await speech;

Limitations

  • listen fails with unsupported. Use { stt: deepgram(), tts: elevenlabs() } for live conversations.
  • pause() and resume() fail with unsupported.
  • speak needs a voice id, from listVoices or your ElevenLabs dashboard.

How it works

  • Streamed text is buffered until a sentence boundary before it is sent, which keeps the voice's intonation natural.
  • interrupt() closes the text-to-speech socket. The next speak opens a new one.
  • listVoices calls the voices endpoint and maps each voice to { id, name, language }.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview