How to Fix Claude API 401 Unauthorized Error
Updated 9/2/2026
An HTTP 401 Unauthorized response from the Anthropic Claude API means that the API server rejected your request because of invalid or missing authentication credentials. This error stops your application from establishing a secure connection to Claude's language models.
Because the API cannot verify your identity, it refuses to process any tokens. To resolve this error and restore your integration, work through the following troubleshooting steps.
1. Verify Your API Key is Active and Correct
The most frequent cause of a 401 error is an invalid, expired, or mistyped API key. Keys can easily be truncated during copy-pasting, or deactivated by another team member in the Anthropic Console.
- Sign in to the Anthropic Console (console.anthropic.com).
- Navigate to the API Keys section.
- Locate the key your application is using. Ensure its status is active.
- If you suspect the key is corrupted or leaked, click Create Key to generate a new one.
- Copy the new key immediately. Do not manually type or edit the key, as they are case-sensitive and must be copied exactly as displayed.
2. Check Your Authorization Headers
If you are making raw HTTP requests (using curl, fetch, or python's requests library) instead of the official Anthropic SDKs, you must manually format your headers. The Claude API expects specific authorization headers; omitting or formatting them incorrectly triggers a 401 error.
Your headers must include: * x-api-key: This header must contain your raw API key (e.g., sk-ant-api03-...). Do not prefix the key with Bearer unless you are routing your requests through an unofficial gateway or reverse proxy that requires it. * anthropic-version: This header is required for all requests and must specify a valid API version, such as 2023-06-01.
Here is a correct raw curl request example:
`bash curl https://api.anthropic.com/v1/messages \ --header "x-api-key: YOUR_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"}] }' `
Ensure there are no leading or trailing spaces, quotes, or brackets around your key in your header payload.
3. Fix Environment Variable Loading Bugs
If your code relies on environment variables (like ANTHROPIC_API_KEY) to load credentials, the 401 error often occurs because the application is reading an empty, outdated, or undefined variable.
To troubleshoot environment variable loading: 1. Print the Variable: Temporarily add a debug line in your code to print the first 5 characters and the length of your loaded key. Never print the full key to your console logs. If it prints None, null, or an empty string, your code cannot access the environment variable. 2. Check your .env file: If using a library like dotenv in Node.js or Python, verify that your .env file is in the root directory of your project. Check that you are calling the load function (e.g., require('dotenv').config() or load_dotenv()) before initializing the Anthropic client. 3. Syntax Check: Ensure there are no spaces around the equals sign in your environment configuration file: ANTHROPIC_API_KEY=sk-ant-api03-xxxxxx... (Correct) ANTHROPIC_API_KEY = "sk-ant-api03-xxxxxx..." (Incorrect - spaces and quotes can corrupt the key value).
4. Reset the Official SDK Clients
If you are using the official @anthropic-ai/sdk (Node.js) or anthropic (Python) packages, they are configured to look for the ANTHROPIC_API_KEY environment variable automatically. If you override this manually when instantiating the client, you might pass an invalid string.
Verify that you initialize the client properly:
Python: `python import os from anthropic import Anthropic
Initialize without arguments to let the SDK pull automatically from the environment client = Anthropic()
Or, if passing manually, ensure the variable holds the exact key # client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) ```
Node.js: `javascript import Anthropic from '@anthropic-ai/sdk';
// Let the SDK load from process.env.ANTHROPIC_API_KEY const anthropic = new Anthropic(); `
If you manually pass a hardcoded string, check for trailing commas, quotes, or accidental concatenation.
When to escalate
If you have verified that your key is active in the console, your headers are formatted correctly, and your code is reading the correct key but you still receive a 401 error, check your billing status. Anthropic will sometimes return authorization-related errors if your account has been temporarily locked or flagged. Log in to the Anthropic Console, navigate to the Billing tab, and verify your account status is "Active" and not "Suspended". If suspended, contact Anthropic Support directly through the in-app chat widget.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude