ElevenLabs API Authentication Error — How to Fix the 401 Response
The ElevenLabs API returns a 401 authentication error when your request cannot be verified as coming from an authorized account. This typically happens when the API key is missing, malformed, or passed using the wrong header name. Developers integrating text-to-speech features are most likely to encounter this error during initial setup or after an API key rotation.
Why does this error happen?
How to fix it
Regenerate Your API Key
Navigate to elevenlabs.io/profile and scroll to the API section to generate a fresh API key. Copy the new key immediately and store it securely, as it may not be shown again in full. Replace your old key everywhere it is used, including environment variables and deployment secrets.
Pass the Key in the Correct Header
ElevenLabs requires the API key to be sent in the `xi-api-key` header, not in a standard `Authorization: Bearer` header. Double-check your fetch or HTTP client configuration to ensure the header name is exactly `xi-api-key` with no typos or capitalization differences. Any deviation from this exact header name will result in a 401 response.
Remove Extra Whitespace from the API Key
When pasting an API key into a `.env` file or config object, it is easy to accidentally include a leading or trailing space that is invisible to the naked eye. Trim the key value in your code using `.trim()` or verify your environment variable contains no surrounding whitespace before passing it to the header. Even a single extra character will cause authentication to fail.
Confirm Your Account Is Active
Log in to elevenlabs.io and verify that your account is in good standing and has not been suspended due to billing issues or a terms violation. If your subscription has expired, your API key may still exist but will return 401 errors until the account is reactivated. Upgrading or renewing your plan will immediately restore API access.
Code example
// Correct ElevenLabs API header
fetch('https://api.elevenlabs.io/v1/text-to-speech/voice_id', {
headers: {
'xi-api-key': process.env.ELEVENLABS_API_KEY,
'Content-Type': 'application/json'
}
})Pro tip
Store your ElevenLabs API key in a `.env` file and always access it via `process.env.ELEVENLABS_API_KEY.trim()` to automatically strip any accidental whitespace before the key is sent in the request header.