Audio & MusicAdvanced

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.

9 minAdvanced

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.

zsh - export key
$export ELEVENLABS_API_KEY=sk_your_key_here
add this to ~/.zshrc so it persists
$

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.

ElevenLabs - Voice details
Rachel
----------------------------------------
Voice ID: 21m00Tcm4TlvDq8ikWAM [ Copy ]
Category: Premade
Labels: calm, narration, american
The Voice ID sits under the voice name, with a copy button.

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.

tts.sh
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.mp3

Step 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.

tts.py
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")
Never commit your key
Keep the key in an environment variable and add it to your .gitignore patterns. A leaked key can burn through your entire character quota.

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

Tags
#api#text-to-speech#python#automation