Fix Claude Code API Error 401 Authentication
Claude Code throwing “failed to authenticate. api error 401” means your API key is missing, expired, or misconfigured. This guide walks through every cause and gives you a working fix for each.
The Problem
You launch Claude Code or send a prompt, and it immediately returns failed to authenticate. API error 401. The tool cannot reach the Anthropic API because the credentials it sends are rejected. This blocks all Claude Code functionality until authentication is restored.
Quick Solution
Step 1: Check if your API key is set:
echo $ANTHROPIC_API_KEY | head -c 10
If this prints nothing, your key is not exported. If it prints the first 10 characters, verify they start with sk-ant-.
Step 2: If the key is missing, set it. Get your key from console.anthropic.com/settings/keys and export it:
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
Add this to your shell profile (~/.zshrc or ~/.bashrc) so it persists:
echo 'export ANTHROPIC_API_KEY="sk-ant-your-key-here"' >> ~/.zshrc
source ~/.zshrc
Step 3: Verify the key works by making a direct API call:
curl -s https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-4-20250514","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}' \
| head -c 200
If you get a valid JSON response, your key works. If you still get a 401, the key is invalid or revoked.
Step 4: Restart Claude Code:
claude
How It Works
Claude Code reads the ANTHROPIC_API_KEY environment variable at startup and sends it as the x-api-key header with every request to the Anthropic API. A 401 response means the API rejected the key. This can happen for several reasons: the key was never set in the current shell session, it was revoked in the Anthropic console, it belongs to a different organization, or a proxy is stripping the header. Claude Code does not cache or store the key itself — it reads from the environment every time.
Common Issues
Key set in a different shell session. If you exported the key in one terminal but launched Claude Code from another (e.g., from VS Code’s integrated terminal), that session does not have the variable. Add the export to your shell profile file to fix this permanently.
Proxy or VPN stripping auth headers. Corporate proxies sometimes strip custom headers. Test with a direct connection first. If it works without VPN, configure your proxy to pass through x-api-key headers to api.anthropic.com.
Using a Claude Pro subscription key instead of an API key. Claude Code requires an Anthropic API key (starts with sk-ant-), not a Claude.ai login. These are separate systems. You need API credits at console.anthropic.com.
Example CLAUDE.md Section
# API Authentication
## Setup
- API key stored in environment variable ANTHROPIC_API_KEY
- Key sourced from ~/.zshrc on shell startup
- Never commit API keys to version control
## Troubleshooting Auth Errors
- If 401: run `echo $ANTHROPIC_API_KEY | head -c 10` to verify key is loaded
- If key is present but 401 persists: key may be revoked, check console.anthropic.com
- If behind VPN: test direct connection first
- After key rotation: update ~/.zshrc AND restart all terminals
## Key Management
- Rotate keys monthly via console.anthropic.com/settings/keys
- Use separate keys for development and production
- Monitor usage at console.anthropic.com/settings/billing
Best Practices
-
Never hardcode API keys in project files. Always use environment variables. Add
ANTHROPIC_API_KEYto your.gitignorepatterns and.env.examplewithout the actual value. -
Use separate keys per environment. Create one key for local dev and another for CI/CD. If one leaks, revoke it without disrupting the other.
-
Check billing status alongside auth errors. A 401 can also appear when your account has no remaining credits. Verify your billing at the Anthropic console.
-
Add a pre-flight check to your workflow. Before long coding sessions, run a quick API test to confirm auth is working. Catching it early saves time.
Related Reading
- Anthropic API Error 429 Rate Limit
- Claude Code MCP Server Disconnected
- Best Way to Use Claude Code for Debugging Sessions
Built by theluckystrike. More at zovo.one