Invalid API Key
The most common Claude API error for new developers. You'll see invalid x-api-key or authentication_error when the API key is missing, malformed, or not loaded into the environment correctly.
What the error looks like
# Python SDK
anthropic.AuthenticationError: Error code: 401 - {
"type": "error",
"error": {
"type": "authentication_error",
"message": "invalid x-api-key"
}
}
# If key is missing entirely:
anthropic.AuthenticationError: Error code: 401 - {
"error": {
"type": "authentication_error",
"message": "x-api-key is required"
}
}
# TypeScript SDK
APIError [AuthenticationError]: 401 invalid x-api-key
Step 1 — Get a valid API key
- Go to console.anthropic.com → Settings → API Keys
- Click Create Key and give it a descriptive name
- Copy the key immediately — Anthropic shows it only once
- The key starts with
sk-ant-(e.g.,sk-ant-api03-eyJ...)
If you lost an existing key, you must revoke it and create a new one.
Step 2 — Set the environment variable correctly
# Terminal (Linux/Mac) — for current session only
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"
# Terminal (Windows PowerShell)
$env:ANTHROPIC_API_KEY = "sk-ant-api03-your-key-here"
# Permanently (Linux/Mac) — add to ~/.bashrc or ~/.zshrc
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"' >> ~/.zshrc
source ~/.zshrc
# Verify it's set
echo $ANTHROPIC_API_KEY
Common mistakes:
- Including quotes in the value:
ANTHROPIC_API_KEY="sk-ant-..."— the quotes should only be in the shell command, not stored as part of the value - Spaces around the
=:ANTHROPIC_API_KEY = sk-ant-...— no spaces allowed in shell assignment - Setting it in a different terminal tab than where you run the script
Step 3 — Using .env files (Python)
# .env file (in project root, never commit to git)
ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
# ⚠️ No quotes around the value in .env files
# ⚠️ Add .env to .gitignore immediately
# your Python script
from dotenv import load_dotenv # pip install python-dotenv
import os
import anthropic
load_dotenv() # MUST call this before creating the client
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
raise ValueError("ANTHROPIC_API_KEY not found in environment")
client = anthropic.Anthropic(api_key=api_key)
# Or let the SDK pick it up automatically after load_dotenv():
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY automatically
Step 4 — Using .env files (Node.js / TypeScript)
# Install dotenv
npm install dotenv
# .env file
ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
// your script.js — add this FIRST
require("dotenv").config(); // CommonJS
// OR
import "dotenv/config"; // ESM
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads process.env.ANTHROPIC_API_KEY
// Or pass explicitly:
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
Verify your key with curl (fastest debug)
# Test your key directly — bypasses all SDK complexity
curl -s https://api.anthropic.com/v1/models \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" | jq .
# If valid: {"data": [...list of models...]}
# If invalid: {"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}
Deploy environments (Vercel, Railway, Render, etc.)
Environment variables set in your local shell are not available in deployed apps. Set them in the platform's dashboard:
- Vercel: Project Settings → Environment Variables → add
ANTHROPIC_API_KEY - Railway: Project → Variables → add key
- Render: Service → Environment → add key
- Heroku:
heroku config:set ANTHROPIC_API_KEY=sk-ant-... - GitHub Actions: Settings → Secrets → Actions → New repository secret
- AWS Lambda: Configuration → Environment variables → edit
Diagnostic checklist
# Python — check if key is visible to your script
import os
key = os.getenv("ANTHROPIC_API_KEY", "NOT FOUND")
print(f"Key starts with: {key[:10]}...") # should be "sk-ant-api"
print(f"Key length: {len(key)}") # should be ~100+ chars
# Check for invisible whitespace corruption
print(repr(key[:20])) # should NOT show \n, \t, or spaces