> ## Documentation Index
> Fetch the complete documentation index at: https://smallestai-ff1e543d.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Sync & Async Synthesis

> Generate speech synchronously or concurrently — REST API and SDK examples.

Generate speech via the REST API or Python SDK — synchronously (one request, complete audio) or asynchronously (multiple requests in parallel).

**Sample output (sync, voice: magnus):**

<video controls style={{ width: '100%', maxWidth: '500px', height: '54px' }}>
  <source src="https://mintcdn.com/smallestai-ff1e543d/__rdeLT6wbSp7Z7Q/audio/tts-sample-hello.wav?fit=max&auto=format&n=__rdeLT6wbSp7Z7Q&q=85&s=bdb0210e6e55870d289dd5709b56e062" type="audio/wav" data-path="audio/tts-sample-hello.wav" />
</video>

## Requirements

* An API key from the [Smallest AI Console](https://app.smallest.ai/dashboard/settings/apikeys?utm_source=documentation\&utm_medium=text-to-speech)
* For Python: `requests` (or `smallestai` for the SDK)
* For JavaScript: Node.js 18+ (built-in `fetch`)

```bash theme={null}
export SMALLEST_API_KEY="your-api-key-here"
```

## Synchronous Text to Speech

Send text, receive complete audio in the response:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.smallest.ai/waves/v1/lightning-v3.1/get_speech" \
    -H "Authorization: Bearer $SMALLEST_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "text": "Hello, this is a test of synchronous speech synthesis.",
      "voice_id": "magnus",
      "sample_rate": 24000,
      "output_format": "wav"
    }' --output sync_output.wav
  ```

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

  API_KEY = os.environ["SMALLEST_API_KEY"]

  response = requests.post(
      "https://api.smallest.ai/waves/v1/lightning-v3.1/get_speech",
      headers={
          "Authorization": f"Bearer {API_KEY}",
          "Content-Type": "application/json",
      },
      json={
          "text": "Hello, this is a test of synchronous speech synthesis.",
          "voice_id": "magnus",
          "sample_rate": 24000,
          "output_format": "wav",
      },
  )

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

  ```javascript JavaScript theme={null}
  const fs = require("fs");

  const response = await fetch(
    "https://api.smallest.ai/waves/v1/lightning-v3.1/get_speech",
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.SMALLEST_API_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        text: "Hello, this is a test of synchronous speech synthesis.",
        voice_id: "magnus",
        sample_rate: 24000,
        output_format: "wav",
      }),
    }
  );

  const buffer = Buffer.from(await response.arrayBuffer());
  fs.writeFileSync("sync_output.wav", buffer);
  ```

  ```python Python SDK theme={null}
  from smallestai.waves import WavesClient

  client = WavesClient(api_key="SMALLEST_API_KEY")
  audio = client.synthesize(
      "Hello, this is a test of synchronous speech synthesis.",
  )
  with open("sync_output.wav", "wb") as f:
      f.write(audio)
  ```
</CodeGroup>

## Asynchronous Text to Speech

For concurrent requests (e.g., generating multiple audio files in parallel):

<CodeGroup>
  ```python Python (asyncio) theme={null}
  import os
  import asyncio
  import aiohttp

  API_KEY = os.environ["SMALLEST_API_KEY"]
  URL = "https://api.smallest.ai/waves/v1/lightning-v3.1/get_speech"

  async def synthesize(session, text, filename):
      async with session.post(URL, headers={
          "Authorization": f"Bearer {API_KEY}",
          "Content-Type": "application/json",
      }, json={
          "text": text,
          "voice_id": "magnus",
          "sample_rate": 24000,
          "output_format": "wav",
      }) as resp:
          audio = await resp.read()
          with open(filename, "wb") as f:
              f.write(audio)
          print(f"Saved {filename}")

  async def main():
      async with aiohttp.ClientSession() as session:
          await asyncio.gather(
              synthesize(session, "First sentence.", "async_1.wav"),
              synthesize(session, "Second sentence.", "async_2.wav"),
              synthesize(session, "Third sentence.", "async_3.wav"),
          )

  asyncio.run(main())
  ```

  ```python Python SDK theme={null}
  import asyncio
  import aiofiles
  from smallestai.waves import AsyncWavesClient

  async def main():
      client = AsyncWavesClient(api_key="SMALLEST_API_KEY")
      async with client as tts:
          audio_bytes = await tts.synthesize(
              "Hello, this is a test of the async synthesis function."
          )
          async with aiofiles.open("async_output.wav", "wb") as f:
              await f.write(audio_bytes)

  asyncio.run(main())
  ```
</CodeGroup>

## Parameters

| Parameter             | Type   | Default    | Description                                                                                       |
| --------------------- | ------ | ---------- | ------------------------------------------------------------------------------------------------- |
| `text`                | string | *required* | Text to synthesize (max \~250 chars recommended)                                                  |
| `voice_id`            | string | *required* | Voice to use (e.g., `magnus`, `olivia`, `aarush`)                                                 |
| `sample_rate`         | int    | `44100`    | `8000`, `16000`, `24000`, or `44100` Hz                                                           |
| `speed`               | float  | `1.0`      | Speech rate multiplier (`0.5` to `2.0`)                                                           |
| `language`            | string | `auto`     | Language code: `en`, `hi`, `es`, `ta`, or `auto`                                                  |
| `output_format`       | string | `pcm`      | Audio format: `pcm`, `wav`, `mp3`, or `mulaw`                                                     |
| `pronunciation_dicts` | array  | —          | List of [pronunciation dictionary](/v4.0.0/content/text-to-speech/pronunciation-dictionaries) IDs |

You can override any parameter per request:

```python theme={null}
# Override speed and sample rate for a single call
response = requests.post(URL, headers=headers, json={
    "text": "Fast and high quality.",
    "voice_id": "magnus",
    "speed": 1.5,
    "sample_rate": 44100,
    "output_format": "mp3",
})
```

## When to Use Each Mode

* **Synchronous**: Real-time voice assistants, chatbot responses, single audio generation
* **Asynchronous**: Batch processing, generating multiple audio files, audiobook chapters, concurrent API calls

For real-time streaming where audio starts playing before generation completes, see [Streaming TTS](/v4.0.0/content/text-to-speech/stream-tts).

## Need Help?

Check out the [API Reference](/v4.0.0/content/api-references/lightning-v3.1) for the full endpoint specification, or ask on [Discord](https://discord.gg/9WtSXv26WE).
