On this page

Deepgram

Stream transcripts with word timings and speaker labels, and speak with Aura voices, using deepgram(). No Deepgram package needed.

deepgram() uses Deepgram's live transcription WebSocket for listen, its pre-recorded endpoint for transcribe, and its text-to-speech API for speak. It calls the API directly, so you install no Deepgram package.

Install

npm install voiceai-sdk

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

Quickstart

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

await using session = await createSession(deepgram());
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)Yes
Streaming transcripts (listen)YesPartials and finals from the live WebSocket
Text-to-speech (speak)Yes
Streaming speechYes
Speech-to-speechNo
Word timingsYes
Speaker labelsYesSet diarize: true; speaker is a number as a string
InterruptionsYes
Voice list (listVoices)Yes
pause() and resume()YesSends KeepAlive messages while paused so the socket stays open

The capability matrix compares these with every other provider.

Stream transcripts

Iterate session.listen to get partial transcripts while someone speaks and a final one when they stop.

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

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

for await (const t of session.listen(microphone())) {
  if (t.type === "final") console.log(`[${t.startMs}ms] ${t.text}`);
}

microphone() stands for any AsyncIterable<Uint8Array> of 16-bit PCM, such as a transport or a file stream.

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 { deepgram } from "voiceai-sdk/deepgram";

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

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. The default is aura-2-thalia-en.

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

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

Hold a conversation

converse calls your handler for each final transcript and speaks what it returns. Return an async iterable to start speaking before the reply is complete.

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

await using session = await createSession(deepgram());
session.connect(transport);
await session.converse(async (text, { signal }) => model.stream(text, { signal }));

Configuration

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

OptionTypeDefaultEnv var
apiKeystringDEEPGRAM_API_KEY
modelstring"nova-3"
voicestring"aura-2-thalia-en"
diarizebooleanfalse
smartFormatbooleantrue
baseUrlstringhttps://api.deepgram.com

Call the API directly

session.native is the live transcription socket while listen runs.

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

await using session = await createSession(deepgram());
session.native.send(JSON.stringify({ type: "Finalize" }));

Limitations

  • Speech-to-speech isn't available. Use Deepgram in a pipeline, or with a converse handler that calls your model.
  • Diarization and some models depend on the language. An unsupported pair fails with unsupported when the session opens.

How it works

  • Session options map to query parameters on the live URL, such as encoding, sample_rate, language and diarize.
  • Turn ends come from Deepgram's UtteranceEnd messages, and barge-in from SpeechStarted.
  • speak streams the text-to-speech response body as chunks, so playback can start before the audio is complete.

Next steps

Was this page helpful?

Esc

Loading…

↑↓ navigate openCtrl J preview