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.
Watch related tutorials
13:30
12:48
10:22
11:30
16:05
14:18