Anthropic Claude API key quickstart
Published 2026-10-05 · Updated 2026-10-24
Anthropic keys look like sk-ant-api03- followed by a long token ending in AA. The API is close to OpenAI’s but not identical; here’s what actually matters.
Set the key
export ANTHROPIC_API_KEY="sk-ant-api03-..."
The curl shape is different
Anthropic doesn’t use Authorization: Bearer. It wants two headers: x-api-key and anthropic-version.
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-sonnet-5",
"max_tokens": 256,
"messages": [{"role": "user", "content": "Say hello"}]
}'
Two things in there that bite newcomers:
max_tokensis required. Omit it and you get a 400, not a default.- The endpoint is
/v1/messages, not/v1/chat/completions.
Python SDK
pip install anthropic
from anthropic import Anthropic
client = Anthropic() # reads ANTHROPIC_API_KEY
msg = client.messages.create(
model="claude-sonnet-5",
max_tokens=256,
messages=[{"role": "user", "content": "Say hello"}],
)
print(msg.content[0].text)
Check a key cheaply
GET https://api.anthropic.com/v1/models with the x-api-key header lists models and costs nothing. It’s the cleanest way to confirm a key works before you burn tokens.
If you’d rather stay OpenAI-compatible
Claude models are also on OpenRouter, use an sk-or-v1- key, the openrouter.ai/api/v1 base URL, and model id anthropic/claude-sonnet-5. Your existing OpenAI-shaped code then works untouched. The trade-off is you pay OpenRouter’s pricing and lose a few Anthropic-only features.
Error cheat sheet
- 401: bad or revoked key.
- 400 with “max_tokens”: you forgot the field.
- 429: rate limit or exhausted credit.
- 403: key valid but lacks access to that model.
That’s the whole quickstart: header instead of bearer, messages instead of chat/completions, and never forget max_tokens.