Skip to main content
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",
        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

Run this example

cd cartesia-python
CARTESIA_API_KEY=YOUR_KEY python3 examples/examples.py tts_generate_to_file