> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gradium.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Text-to-Speech (REST)

> One-shot synthesis of a complete text block via HTTP POST

Use the REST endpoint when you have a finished text block and want a
single HTTP request, no WebSocket to manage. The response can either
be raw audio bytes (for direct file writes) or a JSON stream that
mirrors the WebSocket protocol.

> [card] [Streaming use case?](https://docs.gradium.ai/guides/text-to-speech.md)
>
> For low-latency synthesis of long or generated text, streaming TTS
> via the SDK gives you audio chunks as they're produced.

## Quickstart

`only_audio=true` returns the raw audio bytes directly, easiest for
"text in, file out".

```bash cURL theme={null}
curl -L -X POST https://api.gradium.ai/api/post/speech/tts \
  -H "x-api-key: your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello, world!", "voice_id": "YTpq7expH9539ERJ", "output_format": "wav", "only_audio": true}' \
  > output.wav
```

```python Python theme={null}
import requests

resp = requests.post(
    "https://api.gradium.ai/api/post/speech/tts",
    json={
        "text": "Hello, world!",
        "voice_id": "YTpq7expH9539ERJ",
        "output_format": "wav",
        "only_audio": True,
    },
    headers={"x-api-key": "your_api_key"},
)
resp.raise_for_status()
with open("output.wav", "wb") as f:
    f.write(resp.content)
```

To select a model, add `model_name` at the top level of the JSON body.
Omitting it uses `default`, which selects the latest production TTS model. See
[Model selection](https://docs.gradium.ai/guides/text-to-speech.md#model-selection).

## Response modes

The `only_audio` field in the request body picks one of two response
shapes:

* **`only_audio: true`**: the response body is the raw audio in the
  requested `output_format` (WAV, PCM, Opus, …). Save it directly to a
  file or pipe it to a player. The `Content-Type` reflects the format
  (`audio/wav`, `audio/ogg`, `audio/pcm`).
* **`only_audio: false`** (or omitted): the response is a JSON stream
  using the same message format as the WebSocket endpoint, including
  `audio` (base64), `text` (with timestamps), and `error`. Read the
  body line-by-line until it closes.

### Streaming the JSON response

Pass `only_audio: false` and read the body as it arrives. With cURL,
the `-N` (`--no-buffer`) flag prints each line as soon as the server
sends it.

```bash cURL theme={null}
curl -N -L -X POST https://api.gradium.ai/api/post/speech/tts \
  -H "x-api-key: your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello, world!", "voice_id": "YTpq7expH9539ERJ", "output_format": "wav", "only_audio": false}'
```

```python Python theme={null}
import base64
import json

import requests

audio_chunks = []
text_segments = []

with requests.post(
    "https://api.gradium.ai/api/post/speech/tts",
    json={
        "text": "Hello, world!",
        "voice_id": "YTpq7expH9539ERJ",
        "output_format": "wav",
        "only_audio": False,
    },
    headers={"x-api-key": "your_api_key"},
    stream=True,
) as resp:
    resp.raise_for_status()
    for line in resp.iter_lines(decode_unicode=True):
        if not line:
            continue
        msg = json.loads(line)
        if msg["type"] == "audio":
            audio_chunks.append(base64.b64decode(msg["audio"]))
        elif msg["type"] == "text":
            text_segments.append(msg)

with open("output.wav", "wb") as f:
    f.write(b"".join(audio_chunks))
```

For the full request schema, supported output formats, and error
shapes, see the [TTS POST Endpoint reference](https://docs.gradium.ai/api-reference/endpoint/tts-post.md).

## Next steps

> [card] [TTS POST API reference](https://docs.gradium.ai/api-reference/endpoint/tts-post.md)
>
> Full request body schema, output formats, error contracts.

> [card] [Streaming with the SDK](https://docs.gradium.ai/guides/text-to-speech.md)
>
> Chunked audio output, custom voices, flush, timestamps.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.