# Generate speech

From the Lero API docs: https://lero.studio/docs/en#fala. Everything in one file: https://lero.studio/docs/en/llms-full.txt.

`POST /v1/tts`

Turns the text into speech and returns the MP3 (`audio/mpeg`). Costs 1 credit per character of the text, charged before generating and refunded if generation fails.

Prefer JSON? Send `Accept: application/json` (without `*/*`) and the audio comes as base64 in the `audio` field.

Authentication: key with the Fala (speech) scope.

Request body (JSON):

- `texto` (string, required): What Lero will say, 1 to 500 characters. Leading and trailing spaces do not count.
- `voz` (string, required): The voice: `rio` (Rio de Janeiro), `sp` (São Paulo) or `niteroi` (Niterói).
- `model` (string, optional): `Lero v1`, the only speech model for now.

Response headers:

- `X-Pedido-Id` (string): The request id. Send it if you need support.
- `X-Creditos-Custo` (integer): The credits charged for this request.
- `X-Creditos-Restantes` (integer): The account balance after the request.
- `X-Duracao` (number): The audio duration, in seconds.

Responses:

- `200`: The speech MP3, with the headers above. With `Accept: application/json`, the JSON on the side.
- `400`: `texto_invalido`
- `400`: `voz_invalida`
- `400`: `modelo_invalido`
- `400`: `pedido_invalido`
- `401`: `sem_chave`
- `401`: `chave_invalida`
- `401`: `chave_vencida`
- `402`: `sem_credito`
- `402`: `teto_da_chave`
- `403`: `plano`
- `403`: `escopo`
- `403`: `bloqueado`
- `429`: `muitos_pedidos`
- `429`: `simultaneas`
- `429`: `ocupado`
- `503`: `carregando`
- `503`: `motor_desligado`
- `502`: `falhou`
- `504`: `demorou`
- `500`: `interno`

cURL:

```bash
curl -X POST "https://app.lero.studio/api/v1/tts" \
  -H "Authorization: Bearer $LERO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"texto": "Fala, tudo bem? Aqui é o Lero, direto do Rio.", "voz": "rio"}' \
  --output fala.mp3
```

Python:

```python
import os

import requests

r = requests.post(
    "https://app.lero.studio/api/v1/tts",
    headers={"Authorization": f"Bearer {os.environ['LERO_API_KEY']}"},
    json={"texto": "Fala, tudo bem? Aqui é o Lero, direto do Rio.", "voz": "rio"},
    timeout=300,
)
r.raise_for_status()
with open("fala.mp3", "wb") as f:
    f.write(r.content)
print("credits left:", r.headers["X-Creditos-Restantes"])
```

JavaScript:

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

const res = await fetch("https://app.lero.studio/api/v1/tts", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.LERO_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ texto: "Fala, tudo bem? Aqui é o Lero, direto do Rio.", voz: "rio" }),
});
if (!res.ok) throw new Error(`${res.status} ${(await res.json()).erro}`);
await writeFile("fala.mp3", Buffer.from(await res.arrayBuffer()));
```

Response · Accept: application/json (200):

```json
{
  "pedido": "3f9c1e0a7b2d4c6e8a1b3d5f7e9c0a2b",
  "custo": 45,
  "saldo": 1499955,
  "model": "Lero v1",
  "voz": "rio",
  "duracao": 3.12,
  "formato": "mp3",
  "audio": "SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjYx..."
}
```
