# Create speech

> Turn text into speech. OpenAI compatible, streamed audio, with the cost in a response header.

`POST https://api.getlykia.com/v1/audio/speech`

Turns `input` into audio and streams it back as it is produced. The request is OpenAI compatible, so `audio.speech.create` in the OpenAI SDKs works against this endpoint. Walkthrough: [Speech and voices](https://www.getlykia.com/en/docs/guides/speech-and-voices).

## Request body

- `model` (string, required) — A speech model id (for example `openai/gpt-4o-mini-tts`) or a preset such as `@tts`. List speech models with `GET /models?kind=speech`.
- `input` (string, required) — The text to speak, up to the model's `speech.maxInputChars`.
- `voice` (string, required) — A voice id from [`GET /audio/voices`](https://www.getlykia.com/en/docs/api-reference/audio-voices-list): a ready-made voice or one of the project's clones (`lvc_…`).
- `response_format` (string) — `mp3` (default), `opus`, `aac`, `flac`, `wav` or `pcm`, as the model allows (`speech.formats`).
- `speed` (number) — Within the model's `speech.speed` range.
- `language` (string) — The language of `input`, for models that take it.
- `instructions` (string) — A speaking style, for models with `speech.instructions`.
- `provider_options` (object) — Provider-specific options from the model's allow-list.
- `user` (string) and `metadata` (object) — Tags stored with the request record and never sent to the provider.

## Example

**cURL**

```bash
curl https://api.getlykia.com/v1/audio/speech \
  -H "Authorization: Bearer $LYKIA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-4o-mini-tts",
    "input": "Your order has shipped and will arrive on Thursday.",
    "voice": "alloy",
    "response_format": "mp3"
  }' \
  --output speech.mp3
```

**Python**

```python
import os

from openai import OpenAI

client = OpenAI(base_url="https://api.getlykia.com/v1", api_key=os.environ["LYKIA_API_KEY"])

with client.audio.speech.with_streaming_response.create(
    model="openai/gpt-4o-mini-tts",
    input="Your order has shipped and will arrive on Thursday.",
    voice="alloy",
    response_format="mp3",
) as response:
    # The cost is known before the audio starts, so it comes as a header
    print("cost (USD):", response.headers.get("X-Lykia-Cost"))
    response.stream_to_file("speech.mp3")
```

**TypeScript**

```typescript
import { writeFile } from "node:fs/promises";

const response = await fetch("https://api.getlykia.com/v1/audio/speech", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.LYKIA_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "openai/gpt-4o-mini-tts",
    input: "Your order has shipped and will arrive on Thursday.",
    voice: "alloy",
    response_format: "mp3",
  }),
});
if (!response.ok) throw new Error(`Lykia returned ${response.status}: ${await response.text()}`);

// The cost is known before the audio starts, so it comes as a header
console.log("cost (USD):", response.headers.get("X-Lykia-Cost"), "id:", response.headers.get("X-Generation-Id"));
await writeFile("speech.mp3", Buffer.from(await response.arrayBuffer()));
```

## Response

The body is the audio, with a `Content-Type` that matches the format (`audio/mpeg` for `mp3`). Headers:

| Header            | Meaning                                                                                                   |
| ----------------- | --------------------------------------------------------------------------------------------------------- |
| `X-Generation-Id` | The request id.                                                                                           |
| `X-Lykia-Cost`    | The cost in USD. It is known before the audio starts, from the length of `input` (or the token estimate). |
| `X-Lykia-Preset`  | The preset's name, when `model` was a preset.                                                             |

If the connection drops while the audio is streaming, the characters the provider has billed are still charged.

## Errors

| Status | Code                      | When                                                                                                            |
| ------ | ------------------------- | --------------------------------------------------------------------------------------------------------------- |
| `400`  | `unsupported_input`       | An unsupported `response_format`, `speed` or option, or `input` longer than the model takes (`metadata.param`). |
| `400`  | `unsupported_model`       | The model does not make speech.                                                                                 |
| `403`  | `platform_sales_disabled` | The provider is not offered on Lykia credits yet; add your own key for it.                                      |
| `404`  | `voice_not_found`         | The voice id is unknown or belongs to another project.                                                          |
| `429`  | `provider_busy`           | Lykia's connection to the provider is at its limit of requests at once; retry after `Retry-After`.              |
