How to Fix Claude API 401 Unauthorized Error
Updated 8/18/2026
An HTTP 401 Unauthorized error returned by the Anthropic API indicates that your request was rejected due to failed authentication. This means the server could not validate your API key, or the key is not being transmitted in the format the gateway expects.
This guide explains how to isolate and fix the most common causes of the 401 error across your SDK configuration and HTTP header patterns.
1. Verify Your Environment Variables
Most SDK-based integrations load the Claude API key from a global environment variable. If this variable is missing, incorrectly named, or improperly loaded, the client will fail to authenticate.
* Check the variable name: The official Python and TypeScript/JavaScript SDKs automatically look for an environment variable named ANTHROPIC_API_KEY. If you have named it CLAUDE_API_KEY or API_KEY, the SDK will not find it unless you pass it explicitly. * Scan for formatting issues: Ensure there are no trailing whitespace characters, hidden carriage returns (\r), or quotation marks wrapped inside your environment file (e.g., in .env). * Test in terminal: Run the following command in your terminal to check if the variable is active and printed correctly: `bash echo $ANTHROPIC_API_KEY ` If nothing is returned, you must export the key globally: `bash export ANTHROPIC_API_KEY="your-actual-api-key-here" `
2. Format Direct HTTP Request Headers Correctly
If you are bypassing the official SDKs and calling the API directly via curl, fetch, or a backend HTTP library, you must provide the headers manually. Missing headers will result in an immediate 401 error.
Ensure your raw request includes the following exact headers:
- x-api-key: This is the header containing your actual secret API key. Do not prefix this value with "Bearer". It should be passed as a raw string (e.g., sk-ant-api03-...).
- anthropic-version: The API requires you to declare the version. Use the current stable value: 2023-06-01.
- content-type: Must be set to application/json.
Example of a correct direct HTTP request using curl:
`bash curl https://api.anthropic.com/v1/messages \ --header "x-api-key: YOUR_API_KEY" \ --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"}] }' `
3. Avoid Explicit SDK Initialization Pitfalls
If you initialize the Anthropic client within your code by passing the API key directly, confirm you are not passing an undefined reference.
Python SDK Correct Pattern Ensure your variable loading mechanism does not resolve to `None`:
`python import os from anthropic import Anthropic
Securely load key api_key = os.environ.get("ANTHROPIC_API_KEY") if not api_key: raise ValueError("ANTHROPIC_API_KEY is not set")
client = Anthropic(api_key=api_key) `
Node.js SDK Correct Pattern Avoid passing an empty string from your environment loader:
`javascript import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, }); `
If process.env.ANTHROPIC_API_KEY is undefined because your runtime environment did not load your .env file (e.g., missing dotenv/config or incorrect file paths), the client will throw a 401 error.
4. Confirm Key Status and Workspace Permissions
If your headers and environmental configurations are perfect but you still receive a 401 error, the API key itself may no longer be valid.
- Log in to the Anthropic Console: Go to console.anthropic.com.
- Check Key Status: Navigate to the API Keys section. Confirm that the key you are using is listed as active and has not been revoked or deleted.
- Review Workspace Restrictons: If you belong to a multi-member organization, ensure your key resides in the correct Workspace. If a workspace is paused or archived by an administrator, all keys generated for that workspace will return a 401 error code.
- Check for Suspensions: Check your email for any system notifications regarding your account status. Account-wide restrictions or unpaid past-due invoices can lead to API key invalidation.
When to escalate
If you have confirmed your environment variables are functional, validated your HTTP header formats, verified the key's active status in the console, and verified that your billing profile has positive credits, yet you still receive a 401 error, contact Anthropic Support. Log into the Anthropic Console, click the Help widget in the bottom-right corner, and provide your Organization ID along with a sample timestamp and the truncated key (never send your full API key over support channels).
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude