In this categoryAudio & Music ยท 25
- How to Create Your First Original Song in SunoStart
- How to Write Original Lyrics with an AI Chatbot to Use in Suno
- How to Generate Your First Original Track in Udio
- How to Make an Instrumental With No Vocals in Suno
- How to Write Style Prompts That Nail the Genre and Mood
- How to Control Song Structure with Section Tags in Suno and Udio
- How to Extend a Short Clip into a Full Song in Udio
- How to Check Licensing and Share Your AI Songs Safely
- How to Use Suno Covers on Your Own Recording Without Copyright Risk
- How to Download Stems from Suno and Mix Them in a DAW
- How to Replace AI Vocals with Your Own Voice on a Suno or Udio Track
- How to Make Your First Text-to-Speech Voiceover in ElevenLabs
- How to Clone Your Own Voice with Instant Voice Cloning
- How to Turn a Blog Article into a Narrated Podcast with ElevenLabs
- How to Design a Custom Voice from a Text Prompt in ElevenLabs
- How to Dub a Video into Another Language with ElevenLabs Dubbing
- How to Tune Stability and Similarity for Better-Sounding Voiceovers
- How to Produce a Multi-Speaker Podcast Episode in ElevenLabs Studio
- How to Fix Mispronounced Words and Names in ElevenLabs
- How to Generate Speech Programmatically with the ElevenLabs API
- How to Batch-Narrate Many Video Scripts with the ElevenLabs API
How to Generate Speech Programmatically with the ElevenLabs API
Call the Text to Speech REST endpoint with curl and Python to produce an MP3 from your own code.
When you need to generate audio at scale or inside an app, the web studio is not enough. The ElevenLabs API exposes the same Text to Speech engine over HTTP. This guide gets you a working request with curl, then the same call in Python, returning a saved MP3.
What you need
- An ElevenLabs account with API access
- An API key from the account settings page
- A voice ID (copy it from any voice in your Voices library)
- curl, and Python 3 if you want the script version
Step 1: Grab your API key
Open your profile menu, go to the API keys section, and create a key. Treat it like a password. Store it in an environment variable rather than pasting it into code.
Step 2: Find a voice ID
Each voice has a unique ID used in the URL path. Open a voice in the Voices library and copy its ID, or call the voices endpoint to list them all.
Step 3: Make the curl request
Send a POST to the text-to-speech endpoint with the voice ID in the path and your text plus model in the JSON body. The response body is the raw audio, so write it straight to a file.
curl -X POST \
"https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM" \
-H "xi-api-key: $ELEVENLABS_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello from the ElevenLabs API.",
"model_id": "eleven_multilingual_v2"
}' \
--output speech.mp3Step 4: Do the same in Python
For scripting, the requests library is enough. Stream the response into a file. The pattern is identical: voice ID in the URL, key in the header, text and model in the body.
import os, requests
voice_id = "21m00Tcm4TlvDq8ikWAM"
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
resp = requests.post(
url,
headers={
"xi-api-key": os.environ["ELEVENLABS_API_KEY"],
"Content-Type": "application/json",
},
json={
"text": "Hello from the ElevenLabs API.",
"model_id": "eleven_multilingual_v2",
},
)
resp.raise_for_status()
with open("speech.mp3", "wb") as f:
f.write(resp.content)
print("saved speech.mp3")Result: running either command writes a playable speech.mp3 to your folder, which you can now generate on a loop, in a server, or inside a build pipeline.
Ready to try it yourself? Get set up withElevenLabsin a few minutes.(affiliate link. We may earn a commission at no extra cost. Disclosure)
Watch related tutorials
13:30
12:48
12:45
10:22
14:20
18:30New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.