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.
Configuration Methods
There are three ways to configure authentication with the ElevenLabs SDK:Method 1: Environment Variables (Recommended)
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:Method 3: System Environment Variable
Set the API key as a system environment variable:Client Configuration Options
TheElevenLabs 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
Never commit API keys to version control
Never commit API keys to version control
Add
.env to your .gitignore file:.gitignore
Use different keys for different environments
Use different keys for different environments
Create separate API keys for development, staging, and production:
Rotate API keys regularly
Rotate API keys regularly
Generate new API keys periodically and revoke old ones through your ElevenLabs dashboard.
Limit API key permissions
Limit API key permissions
If available, use API keys with limited scopes for specific use cases.
Monitor API usage
Monitor API usage
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
Environment variable not found
Environment variable not found
Cause: The
.env file is not being loaded or is in the wrong location.Solution:- Ensure
.envis in the same directory where you run your script - Verify
load_dotenv()is called before creating the client - Check that
python-dotenvis installed
403 Forbidden
403 Forbidden
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
Timeout errors
Timeout errors
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