ElevenLabs Python SDK

2.42.0 · active · verified Thu Apr 09

The `elevenlabs` Python SDK is the official client library for the ElevenLabs API, enabling developers to integrate advanced AI voice capabilities into their applications. It supports a wide range of features including text-to-speech, voice cloning, speech-to-text, and conversational AI. The library is actively maintained with very frequent releases (often multiple times a week), typically driven by 'Fern Regeneration' to reflect the latest API schema changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the ElevenLabs client and convert text to speech. It automatically attempts to load the API key from the `ELEVENLABS_API_KEY` environment variable. The generated audio can be played directly using the `play` function, which requires the `pyaudio` optional dependency.

import os
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play

# Initialize the client. It automatically picks up ELEVENLABS_API_KEY from environment variables.
# You can also pass it explicitly: ElevenLabs(api_key="YOUR_API_KEY")
elevenlabs = ElevenLabs(api_key=os.environ.get("ELEVENLABS_API_KEY", ""))

if not elevenlabs.api_key:
    print("Error: ELEVENLABS_API_KEY environment variable not set.")
    print("Please set your ElevenLabs API key before running this example.")
else:
    print("Generating speech...")
    try:
        audio = elevenlabs.text_to_speech.convert(
            text="The quick brown fox jumps over the lazy dog.",
            voice_id="21m00Tz activations", # A common pre-made voice ID (e.g., 'Rachel')
            model_id="eleven_v3", # Recommended model, or "eleven_multilingual_v2", "eleven_flash_v2.5", etc.
            output_format="mp3_44100_128",
        )
        print("Speech generated. Playing audio (requires pyaudio installed)...")
        play(audio)
        print("Audio played.")
    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure 'elevenlabs[pyaudio]' is installed if you want to play audio directly.")
        print("Also check your API key and subscription plan for model/voice access.")

view raw JSON →