Generate speech
Learn how to use and integrate the Soniox Text-to-Speech REST API.
Overview
The Soniox REST API provides request/response Text-to-Speech over HTTP. Send text and generation options in a single POST /tts call and receive the generated audio in the response body.
REST is the right fit for:
- Server-side generation → Render audio on a backend and store or serve the result.
- Batch jobs → Pre-render prompts, menus, notifications, or static narration.
- Non-interactive flows → Anywhere a single request-per-utterance model is simpler than managing a live WebSocket.
For low-latency, interactive, or LLM-driven use cases where audio must start playing before the full text is ready, use the real-time WebSocket API instead.
Endpoint
Required request headers:
Content-Type: application/jsonAuthorization: Bearer <SONIOX_API_KEY|SONIOX_TEMPORARY_API_KEY>
See the Generate speech API reference for the full request schema, parameters, and error codes.
Response
On success, the response body contains raw audio bytes for the generated speech. The Content-Type header reflects the requested audio_format:
audio_format | Content-Type |
|---|---|
pcm_f32le, pcm_s16le, pcm_mulaw, pcm_alaw | audio/pcm |
wav | audio/wav |
mp3 | audio/mpeg |
aac | audio/aac |
opus | audio/opus |
flac | audio/flac |
For supported sample rates and bitrates per format, see audio formats.
Error handling
If an error occurs before audio streaming begins, the server returns a standard JSON error body with error_code, error_type, error_message, more_info, and request_id.
Branch your client on error_type (stable across releases), not on error_message (human-readable, may change). Log the request_id so it can be passed to [email protected] when needed.
Once audio streaming has started, errors cannot be delivered to the client.
For the per-endpoint list of error codes and messages, see the Generate speech API reference. For the full catalog of error_type values across all Soniox APIs, see the Errors reference.
Code example
Prerequisite: Complete the steps in Get started.
See on GitHub: soniox_sdk_rest.py.