Anthropic API Key (2026): How to Get One, Key Types, Claude Code Setup, and Fixes
To get an Anthropic API key, sign in to the Claude Console, open API keys, click Create key, pick a name, an expiration, and a linked account (you or a service account), then copy the key that starts with sk-ant- because the Console shows it only once. Add a payment method or credits before you call the API, since usage is billed per token and new accounts only get a small amount of free test credit, per Anthropic's pricing page as of October 22, 2026. The same key works for every Claude model; there is no separate "Claude API key" or "Sonnet key."
This guide is written for developers who want a key for scripts, coding agents such as Claude Code, or a production service. It covers the 2026 key types (personal, service account, and legacy workspace keys), expiration rules, the anthropic-workspace-id header that trips up many first requests, working code, the common 401/400/429 errors, and when one key for several providers makes more sense.
Anthropic API key in 60 seconds
| Step | Where | What to know (checked Oct 22, 2026) |
|---|---|---|
| 1. Sign in | platform.claude.com | API keys live in the Claude Console, not in the claude.ai chat app |
| 2. Open API keys | Settings → API keys | If Create key is greyed out, your role cannot create keys |
| 3. Create key | Name, expiration, linked account, optional workspace | Expiration presets: 3 hours, 1 day, 7 days, 30 days, custom, or Never |
| 4. Copy once | Shown only at creation | Format starts with sk-ant-; lost keys cannot be viewed again |
| 5. Add billing | Settings → Billing | Pay-as-you-go in USD; tiers have monthly spend caps |
| 6. Test | POST https://api.anthropic.com/v1/messages |
Send the key as Authorization: Bearer (or legacy x-api-key) |
Source: Anthropic's Get your Claude API key and Authentication docs, checked October 22, 2026.
Step by step: create your Anthropic API key
- Create or sign in to a Console account at platform.claude.com. A Claude chat subscription and the API are billed separately, although Max plans include a monthly API credit (details below and in our Anthropic API pricing guide).
- Open the API keys page. If your organization has several workspaces, you can select one now to scope the key to it.
- Click Create key and fill in three fields: a descriptive name (for example
ci-staging-claude), an expiration, and the Linked account. Linking to yourself makes a personal key; linking to a service account makes a key for shared workloads. - Copy the key immediately. Anthropic's docs say the full key is displayed only once. Store it in a password manager or secrets manager, never in a Git repo.
- Add credits or a card on the Billing page. Without funds your first request will fail even with a valid key.
- Export it as an environment variable so the SDKs pick it up automatically:
export ANTHROPIC_API_KEY="sk-ant-api03-..." # macOS/Linux, current shell only
# Windows PowerShell:
# setx ANTHROPIC_API_KEY "sk-ant-api03-..."
Which key type should you choose?
In 2026 Anthropic distinguishes three key types. Picking the wrong one is the most common reason a key "suddenly stops working" when someone leaves a team.
| Key type | Acts as | Stops working when | Use it for |
|---|---|---|---|
| Personal key | You, with your roles | You leave the org, or a single-workspace key loses that workspace | Local development, your own scripts, your own Claude Code |
| Service account key | A service account | The service account is archived or removed from the workspace | CI pipelines, production services, shared agents |
| Workspace key (legacy) | Nobody (belongs to the workspace) | It expires, is disabled/deleted, or the workspace is archived | Existing integrations only; migrate away |
Data from Anthropic's Authentication page, October 22, 2026. Anthropic recommends personal or service account keys because they are identity-backed and cannot outlive the person or workload that owns them. For production on AWS, Google Cloud, Azure, GitHub Actions, or Kubernetes, the same page recommends Workload Identity Federation, which swaps static keys for short-lived tokens.
Expiration rules worth knowing
- Expiration is chosen at creation and cannot be changed later.
- Anthropic emails the creator 7 days before expiry for keys with a lifetime of at least 14 days, and 1 day before for keys with a lifetime of at least 7 days; shorter-lived keys expire silently.
- After expiry, requests return
401 authentication_error, and expired keys cannot be reactivated, only deleted. - Disable is reversible; Delete is permanent.
A practical pattern: use 30-day keys for laptops and coding agents, and service account keys stored in a secrets manager (with rotation) for servers. Our guide to secure API key management for AI products and key rotation behind a gateway go deeper.
Test your key: first request in curl, Python, and Node.js
The model ID below, claude-sonnet-5-5, is the current Sonnet model on Anthropic's models overview as of October 22, 2026.
curl https://api.anthropic.com/v1/messages \
-H "Authorization: Bearer $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"max_tokens": 256,
"messages": [{"role": "user", "content": "Reply with the word ok if this key works."}]
}'
# pip install anthropic
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=256,
messages=[{"role": "user", "content": "Reply with the word ok if this key works."}],
)
print(next(b.text for b in msg.content if b.type == "text"))
print("tokens:", msg.usage.input_tokens, "in /", msg.usage.output_tokens, "out")
// npm install @anthropic-ai/sdk
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // reads ANTHROPIC_API_KEY
const msg = await client.messages.create({
model: "claude-sonnet-5-5",
max_tokens: 256,
messages: [{ role: "user", content: "Reply with the word ok if this key works." }],
});
console.log(msg.content.find((b) => b.type === "text")?.text);
A test like this costs a fraction of a cent: at Sonnet 5.5's official $2 per million input tokens and $10 per million output tokens (Anthropic pricing, October 22, 2026), 30 input and 5 output tokens cost about $0.0001.
The workspace header that breaks first requests
If your key is not scoped to a workspace, every request must include anthropic-workspace-id, or the API returns 400 invalid_request_error with the message "This API key is not scoped to a workspace...". You can find the ID on the Workspaces page in the Console. The fix in Python:
client = anthropic.Anthropic(
default_headers={"anthropic-workspace-id": "wrkspc_..."} # only for keys not scoped to a workspace
)
Keys created for a single workspace can skip the header, which is why Anthropic suggests scoping keys at creation time (Authentication docs).
Use your Anthropic API key with Claude Code
Claude Code can authenticate with a claude.ai subscription, a Console login, a cloud provider, or an API key. According to the Claude Code authentication docs, checked October 22, 2026:
- If
ANTHROPIC_API_KEYis set, Claude Code asks whether to use it at first launch and skips the browser login once you approve. ANTHROPIC_API_KEY,ANTHROPIC_AUTH_TOKEN, and theapiKeyHelpersetting apply to the CLI, the VS Code extension, the Agent SDK, and GitHub Actions, but not to Claude Desktop or cloud sessions.apiKeyHelperruns a script that returns a key, which is useful for pulling a short-lived key from a vault. If the script fails three times, requests fail.- Console users can now sign in without creating an API key (Claude Code v2.1.242 or later), which stores an OAuth profile instead of a static key. Unset
ANTHROPIC_API_KEYbefore you do this.
export ANTHROPIC_API_KEY="sk-ant-api03-..."
claude # approve the key when prompted
# inside Claude Code, /status shows which credential is active
If Claude Code reports token or login errors, see our Claude Code auth token errors guide. To point Claude Code at a router instead of Anthropic directly, see Claude Code API router setup.
Is the Anthropic API key free?
Creating a key is free. Using it is not. Anthropic's pricing FAQ says new users receive "a small amount of free credits to test the API"; after that, every request is billed per token. One exception worth knowing: on claude.com/pricing, checked October 22, 2026, the Max 5x plan ($100/month) lists $100 per month and Max 20x lists $200 per month in API credits for the Claude Platform; Pro ($20/month billed monthly) includes none. Current list prices per million tokens on Anthropic's pricing page, October 22, 2026:
| Model | Model ID | Input | Output | Cache read |
|---|---|---|---|---|
| Claude Fable 5.1 | claude-fable-5-1 |
$10.00 | $50.00 | $0.25 |
| Claude Opus 5.5 | claude-opus-5-5 |
$4.00 | $20.00 | $0.20 |
| Claude Sonnet 5.5 | claude-sonnet-5-5 |
$2.00 | $10.00 | $0.10 |
| Claude Haiku 5.5 (prompts up to 100K) | claude-haiku-5-5 |
$0.10 | $0.50 | $0.01 |
The Batch API halves input and output prices, and US-only inference (inference_geo: "us") adds a 1.1x multiplier. For workload math, see our full Anthropic API pricing guide. If you need a genuinely free model to prototype with, our free LLM API comparison lists options with real limits.
Spend caps and rate limits come with the key
Your organization's usage tier controls both rate limits and a monthly spend cap. Per Anthropic's rate limits page, checked October 22, 2026, the caps are $500 (Start), $1,000 (Build), and $200,000 (Scale) per month; new organizations may begin in a lower Evaluation tier. When you hit the cap, the API returns HTTP 429 until 00:00 UTC on the first of the next month unless you request a higher tier. You can also set your own lower spend limit on the Billing page, which is a good habit for any key used by an autonomous agent.
Common Anthropic API key errors and fixes
| Error | Likely cause | Fix |
|---|---|---|
401 authentication_error |
Typo, revoked, disabled, or expired key | Create a new key; expired keys cannot be reactivated |
400 invalid_request_error mentioning workspace |
Key not scoped to a workspace and no header sent | Add anthropic-workspace-id or create a workspace-scoped key |
404 not_found_error "Workspace ... not found" |
Wrong workspace ID, or the key's identity lacks access | Check the ID on the Workspaces page |
429 "reached your API usage limits" |
Monthly tier spend cap reached | Request a tier increase or wait for the monthly reset |
429 with retry-after |
Requests or tokens per minute exceeded | Back off and retry; see LLM rate limits explained |
529 overloaded_error |
Anthropic capacity, not your key | Retry with backoff or fail over; see API error 529 |
| Create key button disabled | Your Console role cannot create keys | Ask an admin for a role change or a service account key |
Sources: Anthropic Authentication, rate limits, and errors docs, checked October 22, 2026.
Keep the key safe
- Never paste the key into front-end code, mobile apps, or public notebooks. For iOS and macOS apps that must call Claude directly, Anthropic offers App Attest, which issues device tokens that expire after 1 hour.
- Use one key per environment and per agent so you can revoke one without breaking the others.
- Set an expiration, even a long one, so a leaked key has a shelf life.
- Run secret scanning in CI; our AI API secret scanning guide shows a setup.
- Watch usage in the Console and set a spend limit below your tier cap.
When one key for many models is simpler
An Anthropic key only unlocks Claude. If your product or coding agent also calls GPT, Gemini, DeepSeek, Qwen, or Kimi, you end up managing five keys, five balances, and five sets of rate limits. A unified API such as Flatkey issues one key for all of them. On October 22, 2026, Flatkey's model pages listed Claude Sonnet 5.5 at $1.60 input / $8.00 output per million tokens and Claude Opus 5.5 at $3.20 / $16.00, against Anthropic's $2 / $10 and $4 / $20 list prices. The Anthropic SDK works by changing the base URL, per Flatkey's Anthropic SDK guide:
import anthropic
client = anthropic.Anthropic(api_key="YOUR_FLATKEY_KEY", base_url="https://router.flatkey.ai")
msg = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=256,
messages=[{"role": "user", "content": "Summarize this changelog in 3 bullets."}],
)
print(next(b.text for b in msg.content if b.type == "text"))
Stay on a direct Anthropic key if you need Anthropic-only features on launch day, Workload Identity Federation, data residency controls, or an enterprise contract. For a broader comparison, read Flatkey vs direct provider accounts and one API key for multiple AI models.
FAQ
How do I get an Anthropic API key?
Sign in to the Claude Console at platform.claude.com, go to Settings → API keys, click Create key, set a name, expiration, and linked account, and copy the sk-ant- key shown once. Add credits on the Billing page before making requests.
Are Anthropic API keys free?
The key is free to create, and new accounts get a small amount of free test credit, but API usage is billed per token after that. There is no permanent free tier for the Claude API as of October 22, 2026.
Is a Claude API key the same as an Anthropic API key?
Yes. Anthropic's docs say API keys for the Claude API are also called Anthropic API keys. One key works for every Claude model your organization can access.
Where do I find my existing Anthropic API key?
On platform.claude.com/settings/keys you can see each key's name, hint, and expiration, but not the full secret. If you lost it, create a new key and delete the old one.
Does a Claude Pro or Max subscription include API credits?
Pro does not. Max plans list monthly API credits on claude.com/pricing as of October 22, 2026 ($100/month on Max 5x, $200/month on Max 20x). Beyond those credits, API usage through a Console key is billed per token, separately from the chat subscription.
Why does my Anthropic API key return 401?
The key is mistyped, disabled, deleted, or expired. Expired keys cannot be reactivated; create a new one. If the error mentions a workspace, it is a 400 caused by a missing anthropic-workspace-id header instead.
How do I use my Anthropic API key with Claude Code?
Set ANTHROPIC_API_KEY in your shell, run claude, and approve the key when prompted. Use /status inside Claude Code to confirm which credential is active.
Sources
- Anthropic: Get your Claude API key, Authentication, Pricing, Rate limits, Models overview, checked October 22, 2026
- Claude Code: Authentication and IAM, checked October 22, 2026
- Flatkey: model directory and Anthropic SDK guide, checked October 22, 2026


