Python text to speech API for real-time applications
speech.wav
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.
Set your API key
Create a key in Playground. Replace the placeholder below and keep your key on the server.
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.
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
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 referenceSpeak 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 exampleStream 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

SOC 2 Type 2

GDPR

PCI
Python text-to-speech questions
Keep building with Sonic
Compare voices, check audio formats, and plan your integration.
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.
Start building.
Access our models via API and bring a voice agent into production in minutes.