# Speech and voices

> Turn text into speech in the format you need, list a model's voices, and clone a voice for your project with the speaker's consent.

`POST /audio/speech` turns text into audio. The request is OpenAI compatible, so the OpenAI SDKs' `audio.speech.create` works against it. Voices come from [`GET /audio/voices`](https://www.getlykia.com/en/docs/api-reference/audio-voices-list), and a project can add its own cloned voices with [`POST /audio/voices`](https://www.getlykia.com/en/docs/api-reference/audio-voices-create). Field by field reference: [POST /audio/speech](https://www.getlykia.com/en/docs/api-reference/audio-speech).

## Generate speech

**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()));
```

The response body is the audio itself, streamed as it is produced, with a `Content-Type` that matches `response_format`: `mp3` (default), `opus`, `aac`, `flac`, `wav` or `pcm`. Which formats a model supports is in `media.capabilities.speech.formats` in [`GET /models?kind=speech`](https://www.getlykia.com/en/docs/api-reference/models).

Speech is priced per character of `input` (or per token for models whose provider bills tokens), so the cost is known before the audio starts: it comes in the `X-Lykia-Cost` header, next to the request id in `X-Generation-Id`. If the connection drops halfway, the characters the provider has billed are still charged.

Other fields: `speed` (within the model's `speech.speed` range), `language`, and `instructions` for models that take a speaking style (`speech.instructions`). The longest `input` a model accepts is `speech.maxInputChars`.

## List voices

`voice` is a voice id. `GET /audio/voices?model=…` lists the model's ready-made voices and the voices your project has cloned for it:

**cURL**

```bash
curl "https://api.getlykia.com/v1/audio/voices?model=openai/gpt-4o-mini-tts" \
  -H "Authorization: Bearer $LYKIA_API_KEY"
```

**Python**

```python
import os

import requests

response = requests.get(
    "https://api.getlykia.com/v1/audio/voices",
    headers={"Authorization": f"Bearer {os.environ['LYKIA_API_KEY']}"},
    params={"model": "elevenlabs/eleven_multilingual_v2"},
    timeout=30,
)
response.raise_for_status()
for voice in response.json()["data"]:
    print(voice["id"], voice["name"], voice["category"])
```

**TypeScript**

```typescript
const params = new URLSearchParams({ model: "elevenlabs/eleven_multilingual_v2" });
const response = await fetch(`https://api.getlykia.com/v1/audio/voices?${params}`, {
  headers: { Authorization: `Bearer ${process.env.LYKIA_API_KEY}` },
});
if (!response.ok) throw new Error(`Lykia returned ${response.status}: ${await response.text()}`);

const voices = (await response.json()) as { data: { id: string; name: string; category: string }[] };
for (const voice of voices.data) console.log(voice.id, voice.name, voice.category);
```

```json
{
    "object": "list",
    "model": "openai/gpt-4o-mini-tts",
    "provider": "openai",
    "data": [
        {
            "id": "alloy",
            "object": "voice",
            "name": "Alloy",
            "category": "premade",
            "provider": "openai"
        },
        {
            "id": "ash",
            "object": "voice",
            "name": "Ash",
            "category": "premade",
            "provider": "openai"
        },
        {
            "id": "coral",
            "object": "voice",
            "name": "Coral",
            "category": "premade",
            "provider": "openai"
        }
    ]
}
```

Without `model`, the list is for the model of your `@tts` preset.

## Clone a voice

Some models can speak in a voice cloned from a short recording. Send one or more recordings of the speaker as `multipart/form-data`, with a `name` for the voice and `consent=true`:

**cURL**

```bash
# consent=true confirms that you have the speaker's permission to clone this voice
curl https://api.getlykia.com/v1/audio/voices \
  -H "Authorization: Bearer $LYKIA_API_KEY" \
  -F model=elevenlabs/eleven_multilingual_v2 \
  -F name="Store narrator" \
  -F description="Calm narration voice" \
  -F consent=true \
  -F file=@sample.mp3
```

**Python**

```python
import os

import requests

with open("sample.mp3", "rb") as sample:
    response = requests.post(
        "https://api.getlykia.com/v1/audio/voices",
        headers={"Authorization": f"Bearer {os.environ['LYKIA_API_KEY']}"},
        data={
            "model": "elevenlabs/eleven_multilingual_v2",
            "name": "Store narrator",
            # You confirm that you have the speaker's permission to clone this voice
            "consent": "true",
        },
        files={"file": ("sample.mp3", sample, "audio/mpeg")},
        timeout=120,
    )
response.raise_for_status()
voice = response.json()
print(voice["id"], voice["name"])  # use the id as `voice` in speech requests of this project
```

**TypeScript**

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

const form = new FormData();
form.append("model", "elevenlabs/eleven_multilingual_v2");
form.append("name", "Store narrator");
// You confirm that you have the speaker's permission to clone this voice
form.append("consent", "true");
form.append("file", new Blob([await readFile("sample.mp3")], { type: "audio/mpeg" }), "sample.mp3");

const response = await fetch("https://api.getlykia.com/v1/audio/voices", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.LYKIA_API_KEY}` },
  body: form,
});
if (!response.ok) throw new Error(`Lykia returned ${response.status}: ${await response.text()}`);

const voice = (await response.json()) as { id: string; name: string };
console.log(voice.id, voice.name); // use the id as `voice` in speech requests of this project
```

> **Consent is required:** Clone only a voice you have the right to use: your own, or that of a person who has given you permission to clone it. `consent=true` is your statement that you have that permission; Lykia rejects the request without it (`400`, `consent_required`) and records the statement with your API key and project.

The answer is a voice object with an id starting with `lvc_`:

```json
{
    "id": "lvc_xg3eZdWjqZ19YY04ozRw",
    "object": "voice",
    "name": "Narrator",
    "category": "cloned",
    "description": "Calm narration voice",
    "provider": "elevenlabs",
    "model": "elevenlabs/eleven_multilingual_v2",
    "created_at": 1795463459,
    "requires_verification": false
}
```

Use that id as `voice` in speech requests. A cloned voice belongs to the project that created it: it works only with that project's keys, with the same provider and the same key (Lykia's or your own) it was created with, and requests from other projects get `404` (`voice_not_found`). Cloning itself is not charged; speech with the voice is priced like any other speech request. Some providers ask the speaker to verify a clone before it can be used; `requires_verification` says so.

## Delete a cloned voice

`DELETE /audio/voices/{id}` deletes the clone at the provider and in Lykia:

**cURL**

```bash
curl -X DELETE https://api.getlykia.com/v1/audio/voices/lvc_xg3eZdWjqZ19YY04ozRw \
  -H "Authorization: Bearer $LYKIA_API_KEY"
```

**Python**

```python
import os

import requests

VOICE_ID = "lvc_xg3eZdWjqZ19YY04ozRw"

response = requests.delete(
    f"https://api.getlykia.com/v1/audio/voices/{VOICE_ID}",
    headers={"Authorization": f"Bearer {os.environ['LYKIA_API_KEY']}"},
    timeout=60,
)
response.raise_for_status()
print(response.json()["deleted"])
```

## Errors

| Code                      | Status | When                                                                                                                   |
| ------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------- |
| `unsupported_input`       | `400`  | An unsupported `response_format`, `speed`, or a longer `input` than the model takes. `metadata.param` names the field. |
| `consent_required`        | `400`  | `POST /audio/voices` without `consent=true`.                                                                           |
| `platform_sales_disabled` | `403`  | The model's provider is not offered on Lykia credits yet. Add your own key for that provider to the project.           |
| `voice_not_found`         | `404`  | The voice id is unknown or belongs to another project.                                                                 |
| `provider_busy`           | `429`  | Lykia's connection to the provider is at its limit of requests at once. Retry after `Retry-After`.                     |

See [Errors](https://www.getlykia.com/en/docs/guides/errors) for the error body and the other status codes.
