anthropic api 401 unauthorized error
Learn how to resolve the Anthropic API 401 unauthorized error by checking API key formats, headers, and environment variables.
Updated 8/18/2026Powered by Tickd.ai
Encountering an HTTP 401 Unauthorized status code when calling the Anthropic API means the server has rejected your credentials. This issue prevents your application from initiating any chat completions or embeddings. Follow these troubleshooting steps to correct your authentication payload and restore access to Claude.
Clean Your API Key Copy
The most common cause of authentication failures is a malformed key string. Ensure that when you copied the key from the Anthropic Console, no leading or trailing whitespace, quotation marks, or hidden control characters were included. If your environment variables are configured via a .env file, write the key directly without quotes, or wrap it strictly in double quotes to prevent parsing errors.
Verify the x-api-key Request Header
If you are using raw HTTP requests (such as cURL or custom fetch wrappers) instead of official SDKs, you must manually construct the authorization headers. Unlike standard APIs that use the Bearer scheme, Anthropic requires a specific header. Your request must pass the API key using the x-api-key key. Sending standard Authorization: Bearer [key] headers will cause the authentication layer to fail and reject your request.
Include the Required Anthropic Version Header
Anthropic calls fail with authentication-related errors if you do not specify the correct version parameter. Make sure your request includes the anthropic-version header, set to the current supported API date (for example, 2023-06-01). If you are utilizing an older SDK that hardcodes an obsolete version string, update your library packages to standardise on the current API structure.
Check Account Status and Workspace Rules
An API key is tied to a specific workspace and billing profile. If your workspace has been archived, or if your developer profile has been deactivated by an administrator, your keys will return a 401 error. Log in to the Anthropic Console to verify that your key is marked as active. If your account configuration is blocked due to billing discrepancies, you may need to resolve any pending payment issues by visiting our guide on Claude Billing and Payment Problems to bring your account back into good standing.
Troubleshoot with a Direct cURL Request
To isolate whether the issue lies in your SDK wrappers or in the key itself, run a direct cURL command from your terminal. Replace YOUR_API_KEY with your actual token:
curl https://api.anthropic.com/v1/messages -H "x-api-key: YOUR_API_KEY" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" -d '{"model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}'
If this command succeeds, your environment configuration, SDK initialisation, or local server caching is corrupting the API token during runtime execution.