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

# Batch Text-to-Speech

> Generate a WAV file from text using the Cartesia SDK.

Generate speech from text and save it as a WAV file.

## Prerequisites

For this tutorial, you need a Cartesia API key in your shell environment. Get your API key
at [https://play.cartesia.ai/keys](https://play.cartesia.ai/keys), then run this command or
add it to your `.bashrc` or `.zshrc`:

```sh theme={null}
export CARTESIA_API_KEY=<your api key here>
```

## Generate a WAV file

<Tabs>
  <Tab title="Python">
    <Steps>
      <Step title="Install the SDK">
        ```sh theme={null}
        pip install cartesia
        ```
      </Step>

      <Step title="Generate speech">
        ```python generate_speech.py theme={null}
        from cartesia import Cartesia
        import os
        import sys

        client = Cartesia(api_key=os.getenv("CARTESIA_API_KEY"))

        response = client.tts.generate(
            model_id="sonic-3.5",
            transcript="Hello, world! Welcome to Cartesia.",
            voice={"mode": "id", "id": "694f9389-aac1-45b6-b726-9d9369183238"},
            output_format={"container": "wav", "encoding": "pcm_f32le", "sample_rate": 44100},
        )
        sys.stdout.buffer.write(response.content)
        ```
      </Step>

      <Step title="Run it">
        ```sh theme={null}
        python3 generate_speech.py | ffplay -nodisp -autoexit -loglevel quiet -
        # Or save to a file:
        python3 generate_speech.py > output.wav
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="JavaScript">
    <Steps>
      <Step title="Install the SDK">
        ```sh theme={null}
        npm install @cartesia/cartesia-js
        ```
      </Step>

      <Step title="Generate speech">
        ```js generate_speech.mjs theme={null}
        import Cartesia from "@cartesia/cartesia-js";

        const client = new Cartesia({ apiKey: process.env["CARTESIA_API_KEY"] });

        const response = await client.tts.generate({
          model_id: "sonic-3.5",
          transcript: "Hello, world! Welcome to Cartesia.",
          voice: { mode: "id", id: "694f9389-aac1-45b6-b726-9d9369183238" },
          output_format: { container: "wav", encoding: "pcm_f32le", sample_rate: 44100 },
        });

        process.stdout.write(Buffer.from(await response.arrayBuffer()));
        ```
      </Step>

      <Step title="Run it">
        ```sh theme={null}
        node generate_speech.mjs | ffplay -nodisp -autoexit -loglevel quiet -
        # Or save to a file:
        node generate_speech.mjs > output.wav
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="cURL">
    <Steps>
      <Step title="Generate speech">
        ```bash generate_speech.sh theme={null}
        #!/usr/bin/env bash
        curl -X POST "https://api.cartesia.ai/tts/bytes" \
          -H "Cartesia-Version: 2025-04-16" \
          -H "X-API-Key: $CARTESIA_API_KEY" \
          -H "Content-Type: application/json" \
          -d '{
            "model_id": "sonic-3.5",
            "transcript": "Hello, world! Welcome to Cartesia.",
            "voice": {"mode": "id", "id": "694f9389-aac1-45b6-b726-9d9369183238"},
            "output_format": {"container": "wav", "encoding": "pcm_s16le", "sample_rate": 44100}
          }'
        ```
      </Step>

      <Step title="Run it">
        ```sh theme={null}
        bash generate_speech.sh | ffplay -nodisp -autoexit -loglevel quiet -
        # Or save to a file:
        bash generate_speech.sh > output.wav
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>

The voice used above can be found [on the playground](https://play.cartesia.ai/voices/694f9389-aac1-45b6-b726-9d9369183238). Browse more voices at [play.cartesia.ai/voices](https://play.cartesia.ai/voices).

## What's next

<CardGroup cols={3}>
  <Card title="Text-to-Speech Quickstart" icon="comment-dots" href="/get-started/realtime-text-to-speech-quickstart">
    Pipe LLM output to TTS in real time using WebSocket streaming.
  </Card>

  <Card title="Choose a Voice" icon="microphone" href="/build-with-cartesia/capability-guides/choosing-a-voice">
    Browse voices and learn how to pick the right one for your use case.
  </Card>

  <Card title="TTS output audio format" icon="sliders" href="/build-with-cartesia/capability-guides/tts-output-audio-format">
    Pick the right output format, sample rate, and encoding for your use case.
  </Card>
</CardGroup>
