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-sdkpnpm add voiceai-sdkyarn add voiceai-sdkbun add voiceai-sdkSet ELEVENLABS_API_KEY on your server, and keep it out of browser bundles.
Quickstart
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
| Feature | Support | Notes |
|---|---|---|
Speech-to-text (transcribe) | Yes | Batch only, with transcribe |
Streaming transcripts (listen) | No | Pair with a streaming provider in a pipeline |
Text-to-speech (speak) | Yes | |
| Streaming speech | Yes | |
| Speech-to-speech | No | |
| Word timings | Yes | From the alignment data sent with each audio chunk |
| Speaker labels | Yes | |
| Interruptions | Yes | |
Voice list (listVoices) | Yes | Your 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.
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.
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.
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.
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.
| Option | Type | Default | Env var |
|---|---|---|---|
apiKey | string | ELEVENLABS_API_KEY | |
voice | string | Required for speak | |
model | string | "eleven_flash_v2_5" | |
sttModel | string | "scribe_v1" | |
stability | number | Voice default |
Call the API directly
session.native is the ElevenLabs text-to-speech socket while speech is streaming.
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
listenfails withunsupported. Use{ stt: deepgram(), tts: elevenlabs() }for live conversations.pause()andresume()fail withunsupported.speakneeds a voice id, fromlistVoicesor 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 nextspeakopens a new one.listVoicescalls the voices endpoint and maps each voice to{ id, name, language }.
Next steps
- Lifecycle and cleanup
- Give your agent a voice
- Compare providers: the same features on every other provider