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

# Text to Speech Stream

> Stream text to speech with real-time audio chunks

## Request

### Headers

<ParamField header="Authorization" type="string" required>
  Bearer token with your API key
</ParamField>

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

### Body

<ParamField body="text" type="string" required>
  The text to convert to speech (max 5000 characters)
</ParamField>

<ParamField body="voice_id" type="string">
  ID of the voice to use
</ParamField>

<ParamField body="enhance" type="boolean" default="true">
  Enable audio enhancement
</ParamField>

<ParamField body="speed" type="number" default="1.0">
  Speech speed (0.5 to 2.0)
</ParamField>

## Response

The response is streamed as newline-delimited JSON (NDJSON). Each line contains a JSON object:

### Chunk Object

<ResponseField name="type" type="string">
  Type of message: "chunk", "complete", or "error"
</ResponseField>

<ResponseField name="result" type="object">
  Contains the audio chunk data
</ResponseField>

<ResponseField name="result.audioContent" type="string">
  Base64-encoded audio chunk
</ResponseField>

<ResponseField name="message" type="string">
  Error message (only for error type)
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST https://api.60db.ai/tts-stream \
    -H "Authorization: Bearer your-api-key" \
    -H "Content-Type: application/json" \
    -d '{
      "text": "This is a longer text that will be streamed in real-time.",
      "voice_id": "default-voice"
    }' \
    --no-buffer
  ```

  ```javascript JavaScript theme={null}
  import { SixtyDBClient } from "60db";

  const client = new SixtyDBClient("your-api-key");

  await client.textToSpeechStream(
    {
      text: "This is a longer text that will be streamed in real-time.",
      voice_id: "default-voice",
    },
    {
      onChunk: (chunk) => {
        console.log("Received chunk:", chunk.length, "bytes");
        // Play or process the audio chunk
      },
      onComplete: () => {
        console.log("Streaming complete");
      },
      onError: (error) => {
        console.error("Error:", error);
      },
    },
  );
  ```

  ```python Python theme={null}
  from sixtydb import SixtyDBClient

  client = SixtyDBClient('your-api-key')

  def handle_chunk(chunk):
      print(f"Received {len(chunk)} bytes")
      # Play or process the audio chunk

  def handle_complete():
      print("Streaming complete")

  def handle_error(error):
      print(f"Error: {error}")

  client.text_to_speech_stream(
      text='This is a longer text that will be streamed in real-time.',
      on_chunk=handle_chunk,
      on_complete=handle_complete,
      on_error=handle_error,
      voice_id='default-voice'
  )
  ```
</RequestExample>

<ResponseExample>
  ```json Chunk Response theme={null}
  {"type":"chunk","result":{"audioContent":"SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4..."}}
  {"type":"chunk","result":{"audioContent":"//uQxAAAAAAAAAAAAAAASW5mbwAAAA8AAAAGAAA..."}}
  {"type":"complete"}
  ```

  ```json Error Response theme={null}
  { "type": "error", "message": "Invalid voice_id" }
  ```
</ResponseExample>

## Use Cases

Streaming is ideal for:

* **Real-time applications**: Voice assistants, chatbots
* **Long-form content**: Articles, books, documents
* **Low latency**: Start playing audio before generation completes
* **Progressive enhancement**: Display text while generating audio
