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

<Tabs>
  <Tab title="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.5",
            transcript="Hello, world!",
            voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
            output_format={"container": "wav", "encoding": "pcm_f32le", "sample_rate": 44100},
        )
        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:30](https://github.com/cartesia-ai/cartesia-python/blob/main/examples/examples.py#L30)
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    async function ttsGenerateToFile(client: Cartesia): Promise<void> {
      /** Use generate() and write_to_file() to write a wav file. */
      const response = await client.tts.generate({
        model_id: 'sonic-3.5',
        transcript: 'Hello, world!',
        voice: { mode: 'id', id: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b' },
        output_format: { container: 'wav', encoding: 'pcm_f32le', sample_rate: 44100 },
      });

      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:29](https://github.com/cartesia-ai/cartesia-js/blob/main/examples/node_examples.ts#L29)
  </Tab>
</Tabs>

## Run this example

<Tabs>
  <Tab title="Python">
    ```sh theme={null}
    cd cartesia-python
    CARTESIA_API_KEY=YOUR_KEY python3 examples/examples.py tts_generate_to_file
    ```
  </Tab>

  <Tab title="TypeScript">
    ```sh theme={null}
    cd cartesia-js
    CARTESIA_API_KEY=YOUR_KEY npx ts-node examples/node_examples.ts ttsGenerateToFile
    ```
  </Tab>
</Tabs>
