AuthenticationError
e2b.auth.AuthenticationError
Stack trace
e2b.auth.AuthenticationError: API key not set. Please set the E2B_API_KEY environment variable.
File "app.py", line 12, in <module>
client = E2BClient()
File "e2b/sdk.py", line 45, in __init__
raise AuthenticationError("API key not set. Please set the E2B_API_KEY environment variable.") Why it happens
The E2B SDK requires an API key to authenticate requests. If the environment variable E2B_API_KEY is missing or empty, the SDK raises an AuthenticationError to prevent unauthorized access. This ensures secure usage but requires proper environment setup.
Detection
Check for AuthenticationError exceptions during client initialization or API calls. Log and verify that the E2B_API_KEY environment variable is set and non-empty before making requests.
Causes & fixes
The E2B_API_KEY environment variable is not set in the runtime environment.
Set the E2B_API_KEY environment variable with your valid API key before running your application.
The environment variable is set but empty or contains only whitespace.
Ensure E2B_API_KEY is assigned a non-empty, valid API key string without extra spaces.
The application is running in an environment where environment variables are not loaded (e.g., missing .env loading).
Load environment variables properly using a library like python-dotenv or configure your deployment environment to export E2B_API_KEY.
Code: broken vs fixed
from e2b import E2BClient
client = E2BClient() # Raises AuthenticationError: API key not set
response = client.call_api('some_endpoint')
print(response) import os
from e2b import E2BClient
os.environ['E2B_API_KEY'] = 'your_actual_api_key_here' # Set your API key securely
client = E2BClient() # Now initializes without error
response = client.call_api('some_endpoint')
print(response) Workaround
Catch AuthenticationError exceptions and prompt the user or log a clear message to set the E2B_API_KEY environment variable before retrying.
Prevention
Use environment management tools or secrets managers to ensure E2B_API_KEY is always set in all deployment environments before application startup.