Cartesia
Stream text-to-speech with Cartesia's Sonic models using cartesia(): word timings, many voices, and continuation across chunks. No Cartesia package needed.
cartesia() is a text-to-speech provider. It streams speech over Cartesia's WebSocket API and keeps one context per utterance, so text sent in pieces is spoken as one sentence.
Install
npm install voiceai-sdkpnpm add voiceai-sdkyarn add voiceai-sdkbun add voiceai-sdkSet CARTESIA_API_KEY on your server, and keep it out of browser bundles.
Quickstart
import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";
await using session = await createSession(cartesia({ voice: process.env.VOICE_ID }));
const speech = await session.speak("Your order has shipped.", { format: "wav" });
await Bun.write("shipped.wav", speech.audio);This writes the spoken sentence to shipped.wav. await using closes the session when the block ends.
What it supports
| Feature | Support | Notes |
|---|---|---|
Speech-to-text (transcribe) | No | Use a pipeline with a speech-to-text provider |
Streaming transcripts (listen) | No | |
Text-to-speech (speak) | Yes | |
| Streaming speech | Yes | |
| Speech-to-speech | No | |
| Word timings | Yes | Timestamps are requested for every utterance |
| Speaker labels | No | |
| Interruptions | Yes | |
Voice list (listVoices) | Yes | |
pause() and resume() | No |
The capability matrix compares these with every other provider.
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 { cartesia } from "voiceai-sdk/cartesia";
await using session = await createSession(cartesia({ 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 { cartesia } from "voiceai-sdk/cartesia";
const voices = await listVoices(cartesia());
console.log(voices.filter((v) => v.language.startsWith("en")).map((v) => v.id));Use it in a pipeline
Cartesia only speaks. For a conversation, combine it with a provider that streams transcripts.
import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession({ stt: deepgram(), tts: cartesia({ voice: process.env.VOICE_ID }) });
await session.converse(async (text) => answer(text));Configuration
Pass options to cartesia(). Options that have an environment variable fall back to it.
| Option | Type | Default | Env var |
|---|---|---|---|
apiKey | string | CARTESIA_API_KEY | |
voice | string | Required | |
model | string | "sonic-2" | |
language | string | "en" |
Call the API directly
session.native is the Cartesia WebSocket.
import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";
await using session = await createSession(cartesia({ voice: process.env.VOICE_ID }));
console.log(session.native.readyState);Limitations
transcribeandlistenfail withunsupported. Pair Cartesia with a speech-to-text provider.pause()andresume()fail withunsupported.
How it works
- Each
speakcall gets its own context id, and streamed text is sent withcontinue: trueuntil the iterable ends. interrupt()cancels the current context. Audio already sent to the transport still plays.
Next steps
- Lifecycle and cleanup
- Give your agent a voice
- Compare providers: the same features on every other provider