Fix Claude API Key Invalid or Not Working Error
Updated 8/16/2026
Understanding the Claude 401 Unauthorized Error
When your application fails to connect to Anthropic's servers, the Claude API returns an HTTP 401 Unauthorized status, typically accompanied by an invalid_api_key error code. This error means the gateway rejected your credential before processing your actual prompt.
This issue usually stems from copy-paste mistakes, misconfigured environment variables, billing suspensions, or initializing the official SDKs incorrectly. Follow these step-by-step diagnostic and resolution methods to get your API connection working again.
Step 1: Verify the Prefix and Format of the Key
Anthropic keys follow a strict format. If yours does not look like the standard structure, the SDK or gateway will reject it instantly.
- Log in to the Anthropic Console.
- Navigate to API Keys.
- Create a new key or check your current configuration. Anthropic API keys always begin with the prefix sk-ant- followed by a long string of alphanumeric characters.
- Ensure you have not accidentally prepended or appended spaces, quotation marks, or the variable name (such as ANTHROPIC_API_KEY=) when copying the key into your environment file or code interface.
Step 2: Check Workspace Funding and Active Key Status
An API key will return an authentication error if the underlying workspace is locked or has run out of funds, even if the key itself is structurally valid.
- In the Anthropic Console, go to the Billing section.
- Verify that your account has a positive balance. If you are on a pay-as-you-go plan, add at least $5 to test if the key reactivates.
- Go to the API Keys tab and check the status of the specific key you are using. If it has been revoked or disabled, generate a new key.
Step 3: Configure Environment Variables Correctly
The official Anthropic SDKs (Python and TypeScript/JavaScript) look for a specific environment variable by default. If you use a different variable name, the SDK will fail to authenticate.
For Linux/macOS: Run this in your terminal before running your script: ```bash export ANTHROPIC_API_KEY="your-actual-sk-ant-key-here" ``` To make this persistent, add that line to your `~/.bashrc`, `~/.zshrc`, or `.env` file.
For Windows (Command Prompt): ```cmd set ANTHROPIC_API_KEY="your-actual-sk-ant-key-here" ```
For Windows (PowerShell): ```powershell $env:ANTHROPIC_API_KEY="your-actual-sk-ant-key-here" ```
Step 4: Validate SDK Initialization Code
Passing the API key explicitly to the SDK can sometimes bypass system environment variable resolution issues. Use these standard boilerplates to ensure you are initiating the client properly.
Python Implementation: Avoid hardcoding keys directly in production code. Use the standard setup: ```python import os from anthropic import Anthropic
The SDK automatically reads ANTHROPIC_API_KEY from environment client = Anthropic()
Or, pass it explicitly if you manage keys manually: # client = Anthropic(api_key="your-sk-ant-key")
message = client.messages.create( model="claude-3-5-sonnet-latest", max_tokens=1000, temperature=0, messages=[{"role": "user", "content": "Hello, Claude"}] ) print(message.content[0].text) `
Node.js Implementation: ```javascript import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, // defaults to this env var });
async function main() { const message = await anthropic.messages.create({ model: 'claude-3-5-sonnet-latest', max_tokens: 1024, messages: [{ role: 'user', content: 'Hello, Claude' }], }); console.log(message.content[0].text); } main(); `
Step 5: Check Proxy, VPC, and Header Interferences
If you are running your application behind a corporate proxy, VPN, or custom gateway, your network infrastructure might be stripping or modifying the HTTP headers before they reach Anthropic.
- Ensure your network allows outbound HTTPS traffic to api.anthropic.com on port 443.
- Check if your proxy changes the X-API-Key or Authorization headers.
- Temporarily disable local VPNs or firewalls to rule out local network routing issues.
When to Escalate
If you have verified that your key is correctly formatted, your environment variable is set, your billing account has an active balance, and simple curl requests still return a 401 error, the issue may be a backend account hold. Check the Anthropic status page first to confirm there is no ongoing API outage. If all systems are operational, log in to the Anthropic Console and click the Support or Help icon in the bottom right corner to submit a ticket requesting an account verification status check.
Quick fixes
- Claude is down or not loading
- Claude Pro billing or payment problem
- Can't sign in to Claude