Quick answer: To use the ElevenLabs API, create an API key in your account settings, store it as ELEVENLABS_API_KEY, install the official SDK (pip install elevenlabs or npm install @elevenlabs/elevenlabs-js), then call text_to_speech.convert with a voice ID, a model ID (for example eleven_v4) and your text. API usage uses the same credits as the website, and you should never put your key in browser code.
Code based on the official ElevenLabs quickstart (October 2026). SDKs change, so always check the latest docs.
What can you build with the API?
- Automatic narration for articles, courses and notifications.
- Real-time voices for chatbots and voice agents.
- Speech to text, sound effects and dubbing in your own app.
Step 1: Get your API key
Sign in, open your account settings and create an API key. Treat it like a password.
Step 2: Save the key safely
Create a .env file in your project:
ELEVENLABS_API_KEY=your_api_key_here
Step 3: Find a voice ID
Open a voice in your library and copy its ID.
Step 4: Python example
pip install elevenlabs python-dotenv
import os
from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs
load_dotenv()
elevenlabs = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
audio = elevenlabs.text_to_speech.convert(
voice_id="YOUR_VOICE_ID",
model_id="eleven_v4",
output_format="mp3_44100_128",
text="Hello! This audio was made with the ElevenLabs API.",
)
with open("hello.mp3", "wb") as f:
for chunk in audio:
f.write(chunk)
Step 5: JavaScript / TypeScript example
npm install @elevenlabs/elevenlabs-js dotenv
import "dotenv/config";
import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";
const elevenlabs = new ElevenLabsClient();
const audio = await elevenlabs.textToSpeech.convert("YOUR_VOICE_ID", {
text: "Hello! This audio was made with the ElevenLabs API.",
modelId: "eleven_v4",
outputFormat: "mp3_44100_128",
});
The client reads ELEVENLABS_API_KEY from your environment automatically.
Watch out for old tutorials
Many popular API tutorials still use older SDK functions such as generate(), set_api_key() or Voice(). Those were replaced in newer versions of the official SDK, so copying them can cause errors. The examples above follow the current pattern: create an ElevenLabs client and call text_to_speech.convert. When in doubt, check the official ElevenLabs developer quickstart.
Output formats
| Format | Use it for |
|---|---|
| mp3_44100_128 | Most apps, podcasts and websites |
| Higher-bitrate MP3 or 44.1 kHz PCM | Audiobooks and editing (needs a higher plan) |
| Low sample-rate PCM or µ-law | Phone systems and real-time agents |
Streaming for faster playback
For apps where people wait for audio, use the SDK’s streaming method so playback starts while audio is still being generated. Pair it with a Flash model for the lowest delay. For two-way voice conversations, use ElevenLabs Agents instead of building everything yourself.
Official SDKs
Use only the official packages from github.com/elevenlabs: the Python SDK (elevenlabs on PyPI) and the JavaScript SDK (@elevenlabs/elevenlabs-js). Avoid the old unscoped npm package elevenlabs.
Which model should you use?
| Model | Use it for |
|---|---|
| Eleven v4 | Most natural, emotional speech |
| Multilingual v2 | Stable long-form narration |
Flash v2.5 (eleven_flash_v2_5) | Real-time apps and agents (about 75 ms latency), cheaper per character |
Keep your API key safe
- Never put the key in website or app code that runs in the browser.
- Call the API from your own server.
- Keep
.envout of your code repository. - Use separate keys per project and revoke any leaked key immediately.
Control your costs
- API calls use your plan’s credits (about 1 credit per character on standard models). Estimate with our credit calculator.
- Cache audio you’ve already generated.
- Use Flash models for drafts and previews.
- Clean input text first — see our script cleaner.
Common errors and fixes
| Error | Meaning | Fix |
|---|---|---|
| 401 | Missing or wrong API key | Check your .env and key |
| 404 | Wrong voice ID or endpoint | Copy the voice ID again |
| 422 | Invalid parameter | Check model ID and output format |
| 429 | Too many requests | Slow down or upgrade your plan |
| Quota exceeded | Out of credits | Wait for renewal or upgrade |
Beginner project ideas
- Turn your latest blog post into an MP3 automatically.
- A daily news or weather briefing.
- A flashcard app that speaks each word.
- A Discord or Telegram bot that replies with voice.
A simple project plan for your first API app
| Step | Task | Tip |
|---|---|---|
| 1 | Generate one MP3 from a hard-coded sentence | Confirms your key and setup work |
| 2 | Read text from a file | Lets you narrate longer content |
| 3 | Split long text into chunks | Keeps requests within limits and makes retries easy |
| 4 | Save each chunk with a numbered filename | Easier to join and fix |
| 5 | Join the files into one audio file | Use an audio tool or library |
| 6 | Add error handling and retries | Handles temporary failures gracefully |
| 7 | Log characters used per run | Helps you track costs |
Tips for clean API audio
- Clean your text first. Remove symbols, expand abbreviations and write numbers as words. Our script cleaner helps.
- Split at natural points, such as paragraphs or sentences, not in the middle of a sentence.
- Use the same voice and settings for every chunk in a project.
- Store generated files so you do not pay to regenerate the same text.
Where to go next
Once the basics work, try streaming for real-time playback, pronunciation dictionaries for brand names and webhooks or background jobs for long texts. Always check the official documentation for current model names and limits, because they change as new models are released.
Security habits from day one
Treat your API key like a password. Store it in an environment variable or a secrets manager, never in code you share, and never in a browser or mobile app where users could find it. Create separate keys for testing and production, and delete keys you no longer use. If a key ever appears in a public repository, revoke it immediately and create a new one.
Testing before you scale
Run your script on a short sample first, listen to the result and check the characters used. Then try a medium-sized text, such as a blog post, and only then process large batches. This step-by-step approach catches problems with formatting, voice choice or pronunciation early, when they are cheap to fix, rather than after you have used thousands of credits.
Frequently asked questions
Is the ElevenLabs API free?
The free plan includes API access with its monthly credits, but some features (like Voice Library voices via API) need a paid plan.
What is the ElevenLabs API key environment variable?
ELEVENLABS_API_KEY.
Which model ID should I use?
eleven_v4 for quality, eleven_flash_v2_5 for speed. Check the docs for the latest IDs.
Can I use the API in a website?
Yes, but call it from your server so your key stays secret.
Why does my old ElevenLabs code no longer work?
The SDK changed. Replace old calls like generate() with the client pattern shown above and update the package.
How do I list my voices with the API?
The SDK has a voices endpoint that returns your available voices and their IDs. Check the API reference for the current method name.
Can I use the API commercially?
Yes, on a paid plan. Free-plan API output follows the same non-commercial rules as the website.
Final thoughts
Get a key, run the quickstart, then build. Keep the key on the server and watch your credits. For plan sizes, see ElevenLabs pricing explained, and for long narration ideas, our AI audiobook guide.
ElevenLabsAIVoice.com is an independent guide and is not affiliated with ElevenLabs. Plans, prices and models change, so always check the official ElevenLabs website for the latest details.

Leave a Reply