Skip to main content
Synthesize text into audio. The router picks the best TTS provider for your (language, vertical, optimizeFor) and fails over automatically.
const result = await speko.synthesize('Hello world', {
  language: 'en',
  vertical: 'general',
});

Signature

speko.synthesize(
  text: string,
  options: SynthesizeOptions,
  abortSignal?: AbortSignal,
): Promise<SynthesizeResult>

Parameters

text: string

The text to synthesize. No length limit is enforced client-side — the upstream provider applies its own.

options: SynthesizeOptions

Extends RoutingIntent:
FieldTypeDescription
languagestring (BCP-47)e.g. "en", "es-MX".
verticalVerticalgeneral, healthcare, finance, legal.
optimizeForOptimizeFor?balanced, accuracy, latency, cost.
voicestring?Voice id override. The router interprets it per provider (e.g. a Cartesia voice UUID).
speednumber?Speech speed multiplier. Providers vary in what range they accept — 1.0 is always neutral.
constraintsPipelineConstraints?Allow-list constraints.

abortSignal?: AbortSignal

Cancel an in-flight request.

Returns

SynthesizeResult

FieldTypeDescription
audioUint8ArrayRaw audio bytes. Format depends on the chosen provider — always check contentType.
contentTypestringMIME type. ElevenLabs returns audio/mpeg. Cartesia returns audio/pcm;rate=24000.
providerstringUpstream provider that ran the request.
modelstringProvider-specific model identifier (e.g. voice model name).
failoverCountnumberProviders tried before this one succeeded.
scoresRunIdstring | nullScoring run id that selected this provider.

Wire format

The SDK sends POST /v1/synthesize with a JSON body:
{
  "text": "Hello world",
  "intent": { "language": "en", "vertical": "general", "optimizeFor": "latency" },
  "voice": "…",
  "speed": 1.0,
  "constraints": { "allowedProviders": { "tts": ["cartesia"] } }
}
The response is binary audio. provider, model, failoverCount, and scoresRunId are parsed from response headers (X-Speko-Provider, X-Speko-Model, X-Speko-Failover-Count, X-Speko-Scores-Run-Id).

Example: write to disk

import { writeFile } from 'node:fs/promises';

const result = await speko.synthesize('Welcome to the clinic.', {
  language: 'en',
  vertical: 'healthcare',
  voice: 'sonic-english',
});

const ext = result.contentType.includes('mpeg')
  ? 'mp3'
  : result.contentType.includes('pcm')
    ? 'pcm'
    : 'bin';

await writeFile(`greeting.${ext}`, result.audio);

Example: pin a provider for deterministic output

await speko.synthesize('…', {
  language: 'en',
  vertical: 'general',
  constraints: { allowedProviders: { tts: ['cartesia'] } },
});

Format gotchas

The return type depends on the provider Speko picks. If your downstream consumer only handles PCM (e.g. @spekoai/adapter-livekit v1), either pin a PCM provider via constraints or branch on contentType before you decode.