# Synthesize a dialogue

`POST https://my.neuronai.uz/api/v1/tts/dialogue`

API maʼlumotnomasi › Matnni ovozga aylantirish

Turns a script of up to 100 turns, each spoken by its own voice, into one WAV file.

Each turn is billed at its own voice's rate. The `X-Turns` header says where each turn starts and ends in the audio, so a player can jump to a line.

**Authentication:** `Authorization: Bearer <API key>`, a key with `tts:write`.

**Pricing:** per character: Default voice 0.3 credits; Voice clone 0.5 credits.

## Sarlavhalar

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string | No | Makes a retry safe: the same key again returns the original audio and headers instead of synthesizing and charging again. At most 191 characters, scoped to your organization and kept for 7 days. |

## Soʻrov tanasi (`application/json`)

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `language` | string \| null | No | The language of every turn that does not name its own. One of `uz`, `ru`, `en`, `kk`, `ky`, `tk`, `tg`, `az`, `tr`, `ug`, `tt`, `ba` and 25 more. |
| `gap_ms` | integer \| null | No | Silence between turns, in milliseconds, from 0 to 5000. Minimum 0. Maximum 5000. |
| `quality` | string \| null | No | `fast`, `standard` (the default) or `high`, for every turn that does not name its own. One of `fast`, `standard`, `high`, `null`. |
| `speed` | number \| null | No | From 0.5 to 2, for every turn that does not name its own. Minimum 0.5. Maximum 2. |
| `turns` | object[] | Yes | The script, in order: 1 to 100 turns, at most 10000 characters in all. |
| `turns[].voice_id` | string | Yes | The voice that speaks this turn. At most 128 characters. |
| `turns[].text` | string | Yes | What this turn says. Emotion tags such as `[laughter]` or `[sigh]` are acted out, not read aloud, and count as one character each. The tags: `[laughter]`, `[sigh]`, `[dissatisfaction-hnn]`, `[confirmation-en]`, `[question-en]`, `[question-ah]`, `[question-oh]`, `[question-ei]`, `[question-yi]`, `[surprise-ah]`, `[surprise-oh]`, `[surprise-wa]`, `[surprise-yo]`. At least 1 character. |
| `turns[].language` | string \| null | No | This turn's language, if it differs from the dialogue's. One of `uz`, `ru`, `en`, `kk`, `ky`, `tk`, `tg`, `az`, `tr`, `ug`, `tt`, `ba` and 25 more. |
| `turns[].quality` | string \| null | No | This turn's quality, if it differs from the dialogue's. One of `fast`, `standard`, `high`, `null`. |
| `turns[].speed` | number \| null | No | This turn's speed, if it differs from the dialogue's. Minimum 0.5. Maximum 2. |

## Javob (200, `audio/wav`)

WAV audio of the stitched dialogue. Per-turn offsets are in `X-Turns`; pricing, latency and the request id are in headers.

### Sarlavhalar

| Header | Type | Description |
| --- | --- | --- |
| `X-Cost` | number | The price billed, in credits. |
| `X-Balance` | number | Your balance after the charge, in credits. |
| `X-Character-Count` | integer | The characters billed. An emotion tag counts as one. |
| `X-Voice-Custom` | string | `1` when any turn used one of your clones, else `0`. |
| `X-Latency-Ms` | number | Time the voices took, in milliseconds. May have a fraction. |
| `Idempotency-Replayed` | string | `1` when the answer is a replay of an earlier request with the same `Idempotency-Key`. |
| `X-Turns` | string | Where each turn is in the audio, so a client can seek to a line: a JSON array with one object per turn, holding `index`, `voice_id`, `start_s`, `end_s` and `duration_s`. The times are in seconds. |
| `X-Turn-Count` | integer | The number of turns rendered. |
| `X-Request-Id` | string | Identifies this request. A JSON body carries it as `request_id` too; quote it to support. |
| `X-RateLimit-Limit` | integer | The requests a minute the rate limit on this endpoint allows. Sent once the limit has counted the request, so not when the key itself is refused. |
| `X-RateLimit-Remaining` | integer | How many of them are left this minute. |

## Xatolar

Every error has the same JSON shape:

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_request",
    "message": "The request is malformed or failed validation.",
    "param": null
  },
  "request_id": "7c4a8d09-ca37-4f3c-9e2b-1b0f3c2a9d41"
}
```

| Status | Meaning |
| --- | --- |
| 400 | The `Idempotency-Key` is over 191 characters: `invalid_request`. |
| 401 | The API key is missing or not recognized (`invalid_api_key`), revoked (`api_key_revoked`), disabled (`api_key_disabled`) or expired (`api_key_expired`). |
| 402 | Not enough balance (`insufficient_balance`), or the charge would pass this key's monthly spend limit (`spend_limit_exceeded`). |
| 403 | The key lacks the permission (`insufficient_permissions`), the IP is not allowed (`ip_not_allowed`), or the voice cannot be used (`voice_unavailable`). |
| 409 | The `Idempotency-Key` was already used with a different body: `idempotency_conflict`. |
| 413 | Either the script is over 10000 characters in total, or the request body itself is larger than the server accepts — either way, `input_too_large`. |
| 422 | The request failed validation (`invalid_request`). `error.fields` names each invalid field. |
| 429 | A rate or concurrency limit was reached (`rate_limit_exceeded`, `concurrency_limit_exceeded`). Retry after `Retry-After` seconds. |
| 500 | Synthesis failed (`synthesis_failed`) or something else went wrong (`server_error`). Nothing is charged. |
| 503 | Either the audio was made but could not be stored for an idempotent retry (`storage_failed`), or the API itself is down for maintenance (`service_unavailable`). |
| default | Any other error, in the same shape, such as `server_error` (500) or `service_unavailable` (503). |

## Examples

### cURL

```bash
curl -X POST "https://my.neuronai.uz/api/v1/tts/dialogue" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <your-api-key>" \
  -d '{
    "turns": [
      {"voice_id": "<voice-a>", "text": "Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz."},
      {"voice_id": "<voice-b>", "text": "Xush kelibsiz! Birinchi yangilikni tinglaymiz."}
    ],
    "gap_ms": 300
  }' \
  --output dialogue.wav
```

### TypeScript SDK

```typescript
import {NeuronAI} from "@naiuz/sdk";

const client = new NeuronAI();

const dialogue = await client.tts.dialogue({
    turns: [
        {voice_id: "kamron", text: "Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz."},
        {voice_id: "madina", text: "Xush kelibsiz! Birinchi yangilikni tinglaymiz."},
    ],
    gap_ms: 300,
});

await dialogue.save("dialogue.wav");
console.log(dialogue.turns);
```

### Python SDK

```python
from naiuz import NeuronAI

client = NeuronAI()

dialogue = client.tts.dialogue(
    turns=[
        {"voice_id": "kamron", "text": "Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz."},
        {"voice_id": "madina", "text": "Xush kelibsiz! Birinchi yangilikni tinglaymiz."},
    ],
    gap_ms=300,
)

dialogue.save("dialogue.wav")
print(dialogue.turns)
```

### PHP SDK

```php
$client = new \Naiuz\NeuronAI();

$dialogue = $client->tts->dialogue([
    'turns' => [
        ['voice_id' => 'kamron', 'text' => 'Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz.'],
        ['voice_id' => 'madina', 'text' => 'Xush kelibsiz! Birinchi yangilikni tinglaymiz.'],
    ],
    'gap_ms' => 300,
]);

$dialogue->save('dialogue.wav');
print_r($dialogue->turns);
```

### JavaScript

```javascript
const response = await fetch('https://my.neuronai.uz/api/v1/tts/dialogue', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer <your-api-key>',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        turns: [
            { voice_id: '<voice-a>', text: 'Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz.' },
            { voice_id: '<voice-b>', text: 'Xush kelibsiz! Birinchi yangilikni tinglaymiz.' },
        ],
        gap_ms: 300,
    }),
});

if (!response.ok) {
    throw new Error(`Request failed: ${response.status}`);
}

// Per-turn start/end offsets, so a client can seek to a line.
const turns = JSON.parse(response.headers.get('X-Turns'));

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

### Python

```python
import json

import requests

response = requests.post(
    "https://my.neuronai.uz/api/v1/tts/dialogue",
    headers={"Authorization": "Bearer <your-api-key>"},
    json={
        "turns": [
            {"voice_id": "<voice-a>", "text": "Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz."},
            {"voice_id": "<voice-b>", "text": "Xush kelibsiz! Birinchi yangilikni tinglaymiz."},
        ],
        "gap_ms": 300,
    },
)

response.raise_for_status()

# Per-turn start/end offsets, so a client can seek to a line.
turns = json.loads(response.headers["X-Turns"])

with open("dialogue.wav", "wb") as f:
    f.write(response.content)
```

### PHP

```php
$ch = curl_init('https://my.neuronai.uz/api/v1/tts/dialogue');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer <your-api-key>',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'turns' => [
            ['voice_id' => '<voice-a>', 'text' => 'Assalomu alaykum! Bugun sizga yangiliklarni yetkazamiz.'],
            ['voice_id' => '<voice-b>', 'text' => 'Xush kelibsiz! Birinchi yangilikni tinglaymiz.'],
        ],
        'gap_ms' => 300,
    ]),
]);

$audio = curl_exec($ch);
curl_close($ch);

file_put_contents('dialogue.wav', $audio);
```

---

Source: https://docs.neuronai.uz/uz/api/reference/text-to-speech/synthesize-dialogue
