Skip to main content

Getting Your API Key

To use the ElevenLabs Python SDK, you need an API key from your ElevenLabs account.
1

Sign Up or Log In

Visit ElevenLabs and create an account or log in to your existing account.
2

Access Your Profile

Navigate to your profile settings by clicking on your avatar in the top-right corner.
3

Generate API Key

Go to the API section and generate a new API key. Copy this key and store it securely.
Keep your API key secret! Never commit it to version control or share it publicly.

Configuration Methods

There are three ways to configure authentication with the ElevenLabs SDK: The most secure and convenient method is using environment variables.
1

Create a .env file

Create a .env file in your project root:
.env
2

Install python-dotenv

Install the python-dotenv package to load environment variables:
3

Load in your code

Load the environment variables and initialize the client:
The client automatically looks for the ELEVENLABS_API_KEY environment variable. You don’t need to pass it explicitly.

Method 2: Direct API Key Parameter

Pass the API key directly when initializing the client:
Avoid hardcoding API keys in your source code. Use this method only for testing or when reading the key from a secure source.

Method 3: System Environment Variable

Set the API key as a system environment variable:
Then initialize the client without parameters:

Client Configuration Options

The ElevenLabs client accepts several configuration parameters:

Configuration Parameters

str
Your ElevenLabs API key. If not provided, the client reads from the ELEVENLABS_API_KEY environment variable.
str
Custom base URL for API requests. Useful for enterprise deployments or testing.
ElevenLabsEnvironment
default:"PRODUCTION"
The environment to use for requests. Defaults to ElevenLabsEnvironment.PRODUCTION.
float
default:"240"
Request timeout in seconds. Default is 240 seconds (4 minutes).
httpx.Client
Custom pre-configured httpx client. Useful for advanced HTTP configurations like proxies or custom SSL certificates.

Async Client Authentication

The async client (AsyncElevenLabs) uses the same authentication methods:

Async Client Configuration

The async client accepts the same parameters, but with an async httpx client:

Testing Your Authentication

Verify your authentication is working correctly:

Security Best Practices

Add .env to your .gitignore file:
.gitignore
Create separate API keys for development, staging, and production:
Generate new API keys periodically and revoke old ones through your ElevenLabs dashboard.
If available, use API keys with limited scopes for specific use cases.
Regularly check your API usage in the ElevenLabs dashboard to detect any unauthorized access.

Custom HTTP Client Configuration

For advanced use cases, configure a custom httpx client:

Proxy Configuration

Custom Timeouts

SSL Certificate Verification

Troubleshooting

Common Authentication Errors

Cause: Invalid or missing API key.Solution:
  • Verify your API key is correct
  • Check that the environment variable is set correctly
  • Ensure you’re using the latest SDK version
Cause: The .env file is not being loaded or is in the wrong location.Solution:
  • Ensure .env is in the same directory where you run your script
  • Verify load_dotenv() is called before creating the client
  • Check that python-dotenv is installed
Cause: API key doesn’t have permission for the requested operation.Solution:
  • Check your subscription plan limits
  • Verify the API key has the necessary permissions
  • Contact ElevenLabs support if the issue persists
Cause: Request taking longer than the configured timeout.Solution:
  • Increase the timeout parameter when initializing the client
  • Check your internet connection
  • Verify the ElevenLabs API status

Next Steps

Now that you’ve configured authentication, you’re ready to:

Quickstart Guide

Generate your first text-to-speech audio

API Reference

Explore all available API methods