How to Fix Claude API 401 Unauthorized Error
Updated 8/21/2026
Understanding the Claude API 401 Unauthorized Error
When integrated with Anthropic's Claude, receiving an HTTP 401 Unauthorized response means the API gateway has rejected your authentication credentials. This is not a code bug or a rate-limiting issue; it is a direct failure in how your API key is being presented to, or verified by, Anthropic's servers.
This error typically stems from one of four root causes: * An expired, deleted, or incorrect API key. * A malformed or missing HTTP request header. * Environment variables that are not loading properly into your runtime environment. * Workspace or billing issues that have suspended authorization permissions.
Use the following step-by-step troubleshooting sequence to identify and fix the issue.
Step 1: Verify API Key Status in the Anthropic Console
Before refactoring your code, confirm that the API key you are using is still active and valid in the Anthropic Console.
- Sign in to your account at [console.anthropic.com](https://console.anthropic.com).
- Navigate to the API Keys section.
- Locate the key you are currently using in your application config.
- Check the Status column. If the status is "Inactive" or if the key is missing entirely, it will throw a 401 error.
- If the key has been compromised or deactivated, click Create Key to generate a new active token. Copy it immediately, as you cannot view it again.
Step 2: Test the Key with a Raw cURL Request
To rule out SDK bugs or framework configuration issues, test the raw credentials directly against the Anthropic endpoint using your terminal.
Run the following command, replacing your_api_key_here with your actual API key:
`bash curl -https://api.anthropic.com/v1/messages \ -H "x-api-key: your_api_key_here" \ -H "anthropic-version: 2023-06-01" \ -H "content-type: application/json" \ -d '{ "model": "claude-3-5-sonnet-20241022", "max_tokens": 100, "messages": [{"role": "user", "content": "Hello"}] }' `
- If this command succeeds (HTTP 200): Your API key is valid. The 401 error in your application is caused by how your code passes the key or loads environmental variables.
- If this command fails with a 401: Your key is invalid, your account has been suspended, or your workspace permissions have changed. Proceed to Step 4.
Step 3: Audit Environment Variables and SDK Initialization
If the raw cURL command worked, the error lies in how your application environment initializes. Anthropic's SDKs automatically look for an environment variable named ANTHROPIC_API_KEY.
Python Implementation
In Python, ensure you are loading the .env file before initializing the client:
`python import os from dotenv import load_dotenv from anthropic import Anthropic
Force load the .env file from the local directory load_dotenv()
Ensure the variable is actually loaded api_key = os.environ.get("ANTHROPIC_API_KEY") if not api_key: raise ValueError("ANTHROPIC_API_KEY environment variable is not set")
client = Anthropic() # Automatically picks up ANTHROPIC_API_KEY `
Node.js Implementation
In JavaScript/TypeScript, ensure dotenv is configured at the very entry point of your script:
`javascript import dotenv from 'dotenv'; import Anthropic from '@anthropic-ai/sdk';
dotenv.config();
if (!process.env.ANTHROPIC_API_KEY) { throw new Error("Missing ANTHROPIC_API_KEY environmental variable"); }
const anthropic = new Anthropic(); `
Avoid hardcoding keys. If your development server (Vite, Next.js, Django) is running, restart the server after adding new environment variables to your .env files. Runtimes do not hot-reload system variables.
Step 4: Correct Custom Headers (Non-SDK Implementations)
If you are writing custom HTTP requests without using the official Anthropic SDK, verify your headers match the required structure exactly. The Claude API requires two custom headers:
- x-api-key: Must contain the raw key string without any prefix (do not add "Bearer ").
- anthropic-version: Must be set to 2023-06-01.
Example of incorrect headers that trigger a 401: `http /* INCORRECT */ Authorization: Bearer sk-ant-api03... `
Example of correct headers: `http /* CORRECT */ x-api-key: sk-ant-api03... anthropic-version: 2023-06-01 `
Step 5: Check Workspace Member Permissions
If you are part of a shared organization, your workspace administrator may have revoked your access, changed your workspace role, or deleted the workspace itself.
- In the Console, check the top-left dropdown to confirm you are in the correct workspace.
- Go to Settings > Members and check your permission tier.
- If your account is restricted or has been removed from a specific workspace, any keys tied to that workspace will automatically return a 401 Unauthorized status.
When to Escalate
If you have verified that your keys are active, billing has an active balance, and raw cURL requests still fail with a 401, check the [Anthropic Status Page](https://status.anthropic.com/) to see if there is an active authentication outage. If status is normal, contact Anthropic Support via the chat widget in the Anthropic Console with your account ID and the last four characters of the affected API key.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude