TroubleshootingBeginner

How to Fix Anthropic Claude API 401 Authentication Errors

Resolve authentication_error responses from the Claude API by fixing the header and key format.

6 minBeginner

The Claude API uses a different auth header than most APIs. If you copied a snippet built for OpenAI you will get a 401, because Anthropic does not use a Bearer token in the Authorization header. It uses an x-api-key header plus a version header. This guide fixes the most common auth mistakes.

  • An Anthropic account with an API key from console.anthropic.com
  • curl or any HTTP client
  • Your key, which begins with sk-ant-

Step 1: Use the x-api-key header, not Authorization

Anthropic expects the key in an x-api-key header and a required anthropic-version header. Sending Authorization: Bearer returns authentication_error. Compare the two side by side.

request.sh
curl 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-opus-4-5", "max_tokens": 64, "messages": [{"role":"user","content":"ping"}] }'

Step 2: Confirm the key shape

A valid Anthropic key starts with sk-ant- and is long. If yours is short or starts with sk-proj or sk- alone, it belongs to a different provider. Open the console and verify the prefix matches.

console.anthropic.com - API keys
API Keys
Name Key prefix Created
server-prod sk-ant-api03-.. Jun 2
local-dev sk-ant-api03-.. May 14
[ Create Key ]
Every Claude key starts with sk-ant-. Anything else will be rejected.

Step 3: Add the version header

The anthropic-version header is not optional. Leaving it out can produce confusing errors. Use a recent dated value like 2023-06-01, which is the stable version string the API expects.

zsh - claude
$# missing version header
{ "type": "error", "error": { "type": "authentication_error", "message": "..." } }
$# add -H "anthropic-version: 2023-06-01"
{ "id": "msg_01...", "content": [ ... ] }
$
Use the SDK to avoid header mistakes
The official @anthropic-ai/sdk and anthropic Python packages set both headers for you. Pass the key once and the SDK handles the rest.

Result

With the x-api-key and anthropic-version headers in place and a valid sk-ant- key, the messages endpoint returns a 200 with a message id. A developer who had copied an OpenAI snippet fixed their 401 by swapping a single header.

Watch related tutorials

Tags
#anthropic#claude#401#api keys