> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sawt.sa/llms.txt
> Use this file to discover all available pages before exploring further.

# Text-to-Speech (TTS)

> Generate speech from text using streaming response

<Note>
  API key is optional but recommended for better service limits.
</Note>

## Base URL

```
http://tts.sawt.sa/
```

## Authentication

<ParamField header="Authorization">
  Bearer your-api-key-here (optional)
</ParamField>

## POST /v1/text-to-speech/generate

Generate speech from text using streaming response.

### Request Headers

<ParamField header="Authorization">
  Bearer YOUR\_API\_KEY (optional)
</ParamField>

<ParamField header="Content-Type" required>
  application/json
</ParamField>

### Request Body

<ParamField body="text" type="string" required>
  Text to convert to speech
</ParamField>

<ParamField body="format" type="string" required>
  Audio format for output (e.g., "wav")
</ParamField>

<ParamField body="chunk_size" type="number" required>
  Size of audio chunks for streaming (e.g., 128, 32)
</ParamField>

<Accordion title="Example Request">
  ```json theme={null}
  {
    "text": "السلام عليكم، كيف الحال",
    "format": "wav",
    "chunk_size": 32
  }
  ```
</Accordion>

## Response

The API returns a streaming audio response with:

<ResponseField name="Content-Type" type="string">
  audio/{format} (e.g., audio/wav)
</ResponseField>

<ResponseField name="Response Body" type="binary">
  Streaming audio data in the specified format
</ResponseField>

## Error Codes

| Status Code | Description                                       |
| ----------- | ------------------------------------------------- |
| 200         | Success - Audio stream returned                   |
| 400         | Bad Request - Invalid request body or parameters  |
| 401         | Unauthorized - Invalid or missing API key         |
| 422         | Validation Error - Request body validation failed |
| 500         | Internal Server Error - Server processing error   |

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "http://tts.sawt.sa/v1/text-to-speech/generate" \
    -H "Authorization: Bearer your-api-key" \
    -H "Content-Type: application/json" \
    -d '{"text": "السلام عليكم، كيف الحال", "format": "wav", "chunk_size": 32}' \
    --output output.wav
  ```

  ```javascript Node.js theme={null}
  const axios = require('axios');
  const fs = require('fs');

  const generateSpeech = async () => {
    try {
      const response = await axios.post(
        'http://tts.sawt.sa/v1/text-to-speech/generate',
        {
          text: "السلام عليكم، كيف الحال",
          format: "wav",
          chunk_size: 32
        },
        {
          headers: {
            'Authorization': 'Bearer your-api-key',
            'Content-Type': 'application/json'
          },
          responseType: 'stream'
        }
      );

      const writer = fs.createWriteStream('output.wav');
      response.data.pipe(writer);

      return new Promise((resolve, reject) => {
        writer.on('finish', resolve);
        writer.on('error', reject);
      });
    } catch (error) {
      console.error(error);
    }
  };

  generateSpeech();
  ```

  ```python Python theme={null}
  import requests

  def generate_speech():
      url = "http://tts.sawt.sa/v1/text-to-speech/generate"
      
      headers = {
          "Authorization": "Bearer your-api-key",
          "Content-Type": "application/json"
      }
      
      data = {
          "text": "السلام عليكم، كيف الحال",
          "format": "wav",
          "chunk_size": 32
      }
      
      try:
          response = requests.post(url, headers=headers, json=data, stream=True)
          response.raise_for_status()
          
          with open('output.wav', 'wb') as f:
              for chunk in response.iter_content(chunk_size=1024):
                  if chunk:
                      f.write(chunk)
          
          return "Audio saved to output.wav"
      except requests.exceptions.RequestException as e:
          print(f"Error: {e}")
          return None

  result = generate_speech()
  print(result)
  ```
</RequestExample>
