Python text to speech API for real-time applications

Turn text into speech with the Cartesia Python SDK. Save a WAV file, stream audio to your application, or connect Sonic to a real-time voice agent.
speech.py
audio = client.tts.generate(
    transcript="Hello, world.",
    model_id="sonic-3.6",
    voice=voice_id,
    output_format=wav_format,
)
audio.write_to_file("speech.wav")

speech.wav

Full example

Your first text-to-speech request

Turn a script into a WAV recording with the Cartesia Python SDK. Install the package, add your API key, and run the example on your server.

Install the SDK

Use Python 3.9 or later in a virtual environment.

Terminal
python -m pip install --upgrade cartesia

Set your API key

Create a key in Playground. Replace the placeholder below and keep your key on the server.

macOS / Linux
export CARTESIA_API_KEY="your-api-key"

Using PowerShell?$env:CARTESIA_API_KEY="your-api-key"

Generate your first recording

Save this example as speech.py. Run it from your terminal, then open speech.wav to listen.

Terminal
python speech.py

Change the transcript or choose a voice ID from Playground to hear a different result.

Output file
speech.wav
Audio format
44.1 kHz, 16-bit PCM
speech.py
import os
from cartesia import Cartesia

with Cartesia(api_key=os.environ["CARTESIA_API_KEY"]) as client:
    audio = client.tts.generate(
        model_id="sonic-3.6",
        transcript="Hello from your Python application.",
        voice="db6b0ed5-d5d3-463d-ae85-518a07d3c2b4",
        output_format={
            "container": "wav",
            "encoding": "pcm_s16le",
            "sample_rate": 44100,
        },
    )
    audio.write_to_file("speech.wav")

Choose how audio reaches your app

The right transport depends on when your text is ready and how you plan to play the response.

Save a recording

Transport
HTTP
Output
WAV or MP3

Convert a complete script to audio and save it for later playback. The example above produces a WAV file with a header that audio players can read.

Read the audio output reference

Speak as text arrives

Transport
WebSocket
Output
Raw audio chunks

Send text fragments as your LLM writes and receive audio chunks as Sonic generates them. Forward those chunks to a player configured for the requested audio format.

Build a streaming example

Stream audio from a complete script

Already have all your text? Use with_streaming_response to read the HTTP response in chunks.

Use WebSocket streaming when new text arrives during generation, such as a response from an LLM.

Keep your key on the server

A Python backend can own the request while your web app, mobile app, or phone system owns playback.

Your Python server

Read the API key from your environment. Choose the voice, model, and audio settings for each request.

Sonic

Convert the text into speech. Receive an audio response or keep a WebSocket open for incremental input.

Your application

Save the recording or pass audio to your player. Match its encoding and sample rate to the response.

From a script to a production service

A successful API request is the start. Build the surrounding behavior for the application your listeners will use.

Fit your Python stack

Use the synchronous client for scripts and background jobs. Use AsyncCartesia in an async service such as FastAPI. Close the client when its work is finished, then pass the audio to your storage or playback service.

Test the full listening experience

Set request timeouts and handle rate limits. Test realistic scripts with the voice you will ship. For conversations, measure time to audible speech and make sure interruptions stop queued playback. Your network and player affect what a listener hears.

Enterprise-grade security.From Cloud to Local.

  • HIPAA compliant badge

    HIPAA compliant

  • SOC 2 Type 2 badge

    SOC 2 Type 2

  • GDPR badge

    GDPR

  • PCI badge

    PCI

Python text-to-speech questions

Get started today

Talk to an expert.

Connect with a member of our team and learn how Cartesia can help you build world-class voice experiences.

Contact Sales

Start building.

Access our models via API and bring a voice agent into production in minutes.

Try Cartesia