How to Fix Claude API 401 Invalid API Key Error
Updated 9/19/2026
A 401 Unauthorized error from the Claude API indicates that the Anthropic servers rejected your authentication credentials. This error stops your application from making successful requests and typically points to an invalid API key, an incorrect environment variable configuration, or misformatted request headers.
Follow these practical troubleshooting steps to locate the authentication failure and restore your API connection.
Step 1: Verify your API key status in the Console
Before changing your code, ensure that the API key you are using is active, valid, and belongs to the correct account.
- Log in to the [Anthropic Console](https://console.anthropic.com/).
- Navigate to the API Keys section.
- Check the list of active keys. If the key you are using is not listed, it may have been deleted or deactivated.
- If you suspect the key is compromised or inactive, click Create Key to generate a brand-new API key.
- Copy the new key immediately. Note that you cannot view the full key again once you navigate away from the screen.
*Warning: Never hardcode your API key directly into your codebase or commit it to public repositories like GitHub. Always use environment variables.*
Step 2: Check environment variable configuration
The official Anthropic SDKs look for a specific environment variable named ANTHROPIC_API_KEY by default. If this variable is misspelled or improperly loaded, your application will send empty or default credentials, resulting in a 401 error.
- Open your terminal or hosting environment settings.
- Verify the spelling of the variable. It must be exactly ANTHROPIC_API_KEY. Common mistakes include using CLAUDE_API_KEY or ANTHROPIC_KEY.
- Print the environment variable in your terminal to ensure your system can read it:
- macOS/Linux: echo $ANTHROPIC_API_KEY
- Windows (CMD): echo %ANTHROPIC_API_KEY%
- Windows (PowerShell): $env:ANTHROPIC_API_KEY
- If you use a .env file in Node.js or Python, make sure you call your dotenv loader (e.g., require('dotenv').config() in Node.js or dotenv.load_dotenv() in Python) before initializing the Anthropic client.
Step 3: Inspect authorization header formatting
If you bypass the official SDKs and make direct HTTP requests using curl, fetch, or Python's requests library, you must construct the authorization header manually. A minor formatting error will trigger a 401 error.
- Ensure you are using the x-api-key header to pass your key.
- Do *not* prepend your key with "Bearer" as you might do with other APIs (such as OpenAI). The Claude API expects the raw key string.
- A correct direct HTTP request using curl should look like this:
`bash curl https://api.anthropic.com/v1/messages \ --header "x-api-key: your-actual-api-key-here" \ --header "anthropic-version: 2023-06-01" \ --header "content-type: application/json" \ --data '{ "model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello, Claude"}] }' `
Check for trailing spaces, hidden newlines, or quotation marks inside your environment variables, as these are frequently copied along with the API key and will invalidate the header.
Step 4: Rule out workspace and organization issues
If you belong to multiple organizations or work within a shared corporate workspace, your API key might be tied to a workspace that has been suspended, locked, or lacks billing funds.
- In the Anthropic Console, click on your profile/organization settings in the bottom-left corner.
- Verify that your default workspace has an active status.
- Navigate to the Billing tab and ensure you have a positive credit balance. Even if your API key is structurally valid, some accounts receive a 401 or a modified authentication block if the account balance is frozen at $0.00.
When to escalate
If you have verified that your key is active, your environment variable is read correctly, your headers are formatted without "Bearer", and your account has a positive billing balance, yet you still receive a 401 Unauthorized error, the issue may lie with Anthropic's authentication servers. Check the official Anthropic status page for ongoing outages. If all systems are green, submit a support ticket via the help icon in the Anthropic Console, providing the first 4 characters of your API key (never the full key) and the exact timestamp of the failed requests.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude