REST speech generation with Node SDK
Generate speech from text with the Soniox Node SDK over HTTP
The Soniox Node SDK supports Text-to-Speech generation over HTTP with SonioxNodeClient. Use REST when you have the full text up front and don't need streaming from an LLM — the SDK can return audio bytes, stream them as an async iterable, or write the output directly to a file.
Use real-time speech generation when you want the lowest latency to first audio or when text arrives incrementally (for example, streamed from an LLM).
Quickstart
The shortest path is generateToFile — the SDK calls the REST API, streams the response, and writes the output for you.
Generate to bytes
Use client.tts.generate() when you want the full audio payload in memory — for custom storage, uploading to another service, or post-processing.
Stream audio chunks
Use client.tts.generateStream() to receive the response as an async iterable of Uint8Array chunks. This lets you start processing or playing audio before the full payload has arrived.
Write directly to a file
client.tts.generateToFile() accepts either a file path (string) or any WritableStream and returns the total number of bytes written. This is the simplest option for common server-side workflows.
List available models
client.tts.listModels() returns the set of available TTS models with their supported voices.
Generation options
All three generator methods accept the same GenerateSpeechOptions shape:
| Option | Type | Description |
|---|---|---|
text | string | Input text to synthesize. Required. |
voice | string | Voice identifier (e.g. "Adrian"). Required. |
model | string | TTS model. Default "tts-rt-v1-preview". |
language | string | Language code. Default "en". |
audio_format | TtsAudioFormat | Output audio format. Default "wav". |
sample_rate | number | Output sample rate in Hz. Required for raw PCM formats. |
bitrate | number | Codec bitrate in bps (for compressed formats). |
signal | AbortSignal | Optional signal to cancel the request. |
See Available models for the full list of TTS models, voices, and supported audio formats.
Cancel a request
Pass an AbortSignal to cancel a generation request — useful when the user changes their mind mid-request, or when you need a deadline.
Error handling
REST TTS requests can throw the following errors:
| Error | When it's thrown |
|---|---|
SonioxHttpError | Covers all HTTP failures: non-2xx responses (code: 'http_error'), network failures (code: 'network_error'), timeouts (code: 'timeout'), aborted requests (code: 'aborted'), and parse errors (code: 'parse_error'). Inspect code, statusCode, message, and bodyText. |
SonioxError | Base class for all SDK errors (SonioxHttpError extends it). Catch this if you want a single branch for every Soniox-originated failure. |
For raw HTTP integration details, see the TTS REST API reference.
Error handling limitations
Mid-stream errors reported via HTTP trailers (X-Tts-Error-Code, X-Tts-Error-Message) may not be surfaced by HTTP clients that ignore trailers, including browser fetch and the Soniox JS SDK. For guaranteed error delivery, use the realtime WebSocket TTS instead.
See also
- Real-time speech generation — WebSocket-based streaming TTS for LLM token streaming and low-latency playback.
GenerateSpeechOptionsreferenceTtsModelreference andTtsAudioFormat