# Text-to-Speech

> OpenAI-compatible AI gateway routing across free-tier providers with health-aware selection.

Canonical: https://ai-gateway.sassmaker.com/text-to-speech/

## Endpoint

```
POST /v1/audio/speech
```

OpenAI-compatible text-to-speech. Returns MP3 audio bytes. Uses the request shape of the OpenAI `client.audio.speech.create()` API.

## Request Body

| Field | Type | Default | Description |
|---|---|---|---|
| `model` | string | `"auto"` | TTS model (see [Providers](https://ai-gateway.sassmaker.com/text-to-speech/#providers)). Use `"auto"` for health-aware routing. |
| `input` | string | required | Text to speak. 1–3,000 chars; the shared daily budget may reject shorter requests. |
| `voice` | string | `"luna"` | `luna`; legacy `alloy`, `echo`, `fable`, `onyx`, `nova`, `shimmer` are aliases for the same Luna voice. |
| `response_format` | string | `"mp3"` | Workers AI supports `"mp3"` only; other formats are rejected before inference. |
| `speed` | number | `1.0` | Workers AI supports `1.0` only; other values are rejected before inference. |
| `project_id` | string | — | Body-level project tag. Alternative to `x-gateway-project-id` header. |

### Required Header

| Header | Required | Description |
|---|---|---|
| `Authorization: Bearer <GATEWAY_API_KEY>` | yes | Gateway API key. |
| `x-gateway-project-id` | yes (or body `project_id`) | 1–64 chars `[a-zA-Z0-9._:-]`. |
| `x-gateway-force-provider` | no | Pin to `workers_ai` or `groq`. |

## Providers

| Provider | Model | Voices | Status |
|---|---|---|---|
| **Cloudflare Workers AI** | `@cf/deepgram/aura-2-en` | `luna` (plus the legacy aliases above) | **Enabled** — no external key, uses Workers AI binding. |
| **Groq PlayAI** | `playai-tts` | — | :warning: **Disabled** — Groq deprecated `playai-tts` on their free tier. Kept in the registry for future re-enablement but currently returns `no_tts_provider`. |

Aura 2 uses a character-based price. The gateway conservatively reserves against
UTF-8 byte length plus a 20% buffer before inference, within the shared 9,500
Neurons/day cap. At the published rate, 1,000 ASCII characters reserve 3,273
Neurons. A 3,000-character request can therefore exceed the whole daily cap.
See [Cloudflare pricing](https://developers.cloudflare.com/workers-ai/platform/pricing/).

## Response

Binary audio bytes. The `Content-Type` matches the served format:

| Format | `Content-Type` |
|---|---|
| mp3 | `audio/mpeg` |

**Response Headers:**

| Header | Description |
|---|---|
| `x-gateway-provider` | Provider that served the request (e.g. `workers_ai`). |
| `x-gateway-model` | Exact model used. |
| `x-gateway-project-id` | Echoes the resolved project id. |

## Examples



### curl

```bash
curl https://ai-gateway.sassmaker.com/v1/audio/speech \
  -H "Authorization: Bearer <GATEWAY_API_KEY>" \
  -H "x-gateway-project-id: my_project" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "auto",
    "input": "Hello world. This is the free AI gateway speaking.",
    "voice": "nova",
    "response_format": "mp3"
  }' \
  --output speech.mp3
```


### JavaScript

```js
const response = await fetch('https://ai-gateway.sassmaker.com/v1/audio/speech', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer <GATEWAY_API_KEY>',
    'x-gateway-project-id': 'my_project',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'auto',
    input: 'Hello world. This is the free AI gateway speaking.',
    voice: 'nova',
    response_format: 'mp3',
  }),
});

const audioBlob = await response.blob();
const url = URL.createObjectURL(audioBlob);
new Audio(url).play();

console.log('Served by:', response.headers.get('x-gateway-provider'));
```


### OpenAI SDK

```js


const client = new OpenAI({
  baseURL: 'https://ai-gateway.sassmaker.com/v1',
  apiKey: '<GATEWAY_API_KEY>',
  defaultHeaders: { 'x-gateway-project-id': 'my_project' },
});

const mp3 = await client.audio.speech.create({
  model: 'auto',
  voice: 'shimmer',
  input: 'Hello from Claude and the free AI gateway.',
});

const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.promises.writeFile('speech.mp3', buffer);
```



## Error Codes

| Status | `code` | Meaning |
|---|---|---|
| 400 | `invalid_project_id` | Missing/invalid `project_id`. |
| 502 | `provider_error` | All TTS candidates failed. Last upstream error is included in the message. |
| 503 | `no_tts_provider` | No TTS binding/key configured. |
