> ## 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.

# Generate to File

> Use generate() and write_to_file() to write a wav file.

> [tab] Python
>
> ```python theme={null}
> def tts_generate_to_file(client: Cartesia) -> None:
>     """Use generate() and write_to_file() to write a wav file."""
>     response = client.tts.generate(
>         model_id="sonic-3.6",
>         transcript="Hello, world!",
>         voice="6ccbfb76-1fc6-48f7-b71d-91ac6298247b",
>         output_format={"container": "wav", "encoding": "pcm_s16le", "sample_rate": 44100},
>         language="en",
>     )
>     response.write_to_file("output.wav")
>     print(f"Saved audio to output.wav")
>     print(f"Play with: ffplay -f wav output.wav")
> ```
>
> From [cartesia-python/examples/examples.py:29](https://github.com/cartesia-ai/cartesia-python/blob/v4.0.1/examples/examples.py#L29)

> [tab] Python (Async)
>
> ```python theme={null}
> async def tts_generate_async(client: AsyncCartesia) -> None:
>     """Async TTS generation to file."""
>     response = await client.tts.generate(
>         model_id="sonic-3.6",
>         transcript="Hello, world!",
>         voice="6ccbfb76-1fc6-48f7-b71d-91ac6298247b",
>         output_format={"container": "wav", "encoding": "pcm_s16le", "sample_rate": 44100},
>         language="en",
>     )
>     await response.write_to_file("output_async.wav")
>     print("Saved audio to output_async.wav")
>     print("Play with: ffplay -f wav output_async.wav")
> ```
>
> From [cartesia-python/examples/async\_examples.py:19](https://github.com/cartesia-ai/cartesia-python/blob/v4.0.1/examples/async_examples.py#L19)

> [tab] TypeScript
>
> ```typescript theme={null}
> async function ttsGenerateToFile(client: Cartesia): Promise<void> {
>   const response = await client.tts.generate({
>     model_id: 'sonic-3.6',
>     transcript: 'Hello, world!',
>     voice: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b',
>     output_format: { container: 'wav', encoding: 'pcm_f32le', sample_rate: 44100 },
>     language: 'en',
>   });
>
>   const buffer = Buffer.from(await response.arrayBuffer());
>   fs.writeFileSync('output.wav', buffer);
>   console.log('Saved audio to output.wav');
>   console.log('Play with: ffplay -f wav output.wav');
> }
> ```
>
> From [cartesia-js/examples/node\_examples.ts:111](https://github.com/cartesia-ai/cartesia-js/blob/v4.0.1/examples/node_examples.ts#L111)

## Run this example

> [tab] Python
>
> ```sh theme={null}
> git clone --branch v4.0.1 https://github.com/cartesia-ai/cartesia-python
> cd cartesia-python
> uv sync
> CARTESIA_API_KEY=YOUR_KEY uv run examples/examples.py tts_generate_to_file
> ```

> [tab] Python (Async)
>
> ```sh theme={null}
> git clone --branch v4.0.1 https://github.com/cartesia-ai/cartesia-python
> cd cartesia-python
> uv sync
> CARTESIA_API_KEY=YOUR_KEY uv run examples/async_examples.py tts_generate_async
> ```

> [tab] TypeScript
>
> ```sh theme={null}
> git clone --branch v4.0.1 https://github.com/cartesia-ai/cartesia-js
> cd cartesia-js
> pnpm i
> CARTESIA_API_KEY=YOUR_KEY pnpm tsn examples/node_examples.ts ttsGenerateToFile
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.