Anthropic 401 "invalid x-api-key": what it means and how to fix it
Why the Claude API answers 401 authentication_error with "invalid x-api-key", how to tell an expired, disabled or wrong key apart, and the env variables that cause it.
The answer first
A 401 with authentication_error and the message "invalid x-api-key" means the Claude API received a key it will not accept. Anthropic's error reference lists the usual reasons: the key is malformed, revoked or expired. In a deployed app the most common cause is simpler: the server is not sending the key you think it is. It may be an old value on the host, a key from another provider, or an empty variable. Check the value the server actually sends before you create yet another key.
One 401 is not about the key at all. If the message starts with CORS requests are not allowed for this Organization, the request came from a browser and your organisation's settings block that. Anthropic says no change to the browser request fixes it: move the call to your own back end.
Match the cause to what you see
Each row points to a different place to look. Read the full message and the request_id in the error body first; Anthropic includes a request ID in every response, in the request-id header and in the error body.
| What you see | Likely cause | Where to fix it |
|---|---|---|
| 401 invalid x-api-key on every request, locally and in production | The key was deleted, disabled or mistyped | Claude Console, API keys page. A disabled key can be re-enabled; a deleted one cannot |
| Worked yesterday, 401 today, nothing changed in code | The key expired. Keys can now be created with an expiry of 3 hours, 1 day, 7 days or 30 days | Create a new key. Anthropic says an expired key can only be deleted, not turned back on |
| Works on your laptop, 401 after deploy | The host holds an old value, or the variable is missing in that environment | Your host's variables, then redeploy |
| 401 from a library such as LiteLLM or a framework wrapper | The wrapper sends a different variable, for example your OpenAI key, to Anthropic | The wrapper's configuration, or remove the stray variable |
| 401 using an admin key | Admin API keys manage the organisation and, in Anthropic's words, can't call Claude | Create a Developer API key instead |
| 401 that begins "CORS requests are not allowed" | A browser call blocked by your organisation's settings | Call the API from server code |
A worked example: the key that expired on a Friday
Take a small Next.js app called Tidyline that summarises support tickets with Claude, hosted on Railway. On Monday a new key is created in the Console and pasted into Railway as ANTHROPIC_API_KEY. On Friday afternoon every summary fails and the logs show 401 {"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}.
Nobody changed the code, and the key in Railway still matches the one in the password manager. The API keys page in the Console shows why: when the key was created, the expiry was left at 7 days, a preset rather than Never. The key expired exactly on schedule. The fix is a new key with an expiry chosen on purpose, pasted into Railway, followed by a redeploy. Anthropic says you cannot change a key's expiry after you create it, so the choice at creation is the one that counts. If your organisation sets a maximum expiry, Never is not offered at all, and you need a rotation reminder instead.
Check the value the server sends
Never print a full key. Log its length and the last four characters from the running server, and compare them with the API keys page. A value two characters longer than your other keys usually has quotes pasted into it; one longer usually has a trailing space or newline.
Then make one direct call with the same value. Anthropic's current docs send the key as Authorization: Bearer and still accept the older x-api-key header, with the instruction to send only one of them. If this call succeeds and the app still fails, the key is fine and something in the app changes it on the way out.
Environment variables that change the request
The official TypeScript SDK reads three variables when you create a client with no arguments, according to its source: ANTHROPIC_API_KEY for the key, ANTHROPIC_AUTH_TOKEN for a bearer token, and ANTHROPIC_BASE_URL for the address. A leftover value in any of them can break a correct key. Look for these on the host and in every .env file the build may load:
ANTHROPIC_BASE_URLpointing at a proxy, a gateway or a local server. Your Anthropic key then goes somewhere that does not accept it.ANTHROPIC_AUTH_TOKENleft over from a gateway or CLI experiment. If it is set next toANTHROPIC_API_KEY, the SDK sends both headers, while Anthropic's docs say to send only one.- A key passed explicitly in code, such as
new Anthropic({ apiKey: process.env.CLAUDE_KEY }). Anthropic says a key your code passes comes before the environment variable, so a staleCLAUDE_KEYwins over a correctANTHROPIC_API_KEY. - A variable set for one environment only, such as Preview on Vercel or one service on Railway, while the request runs in another.
Workspaces give a 400, not a 401
If a key is scoped to the whole organisation rather than one workspace, every request must name a workspace in the anthropic-workspace-id header. Anthropic answers a missing header with a 400 invalid_request_error, not a 401. If you see a 400 after switching keys, that is the more likely cause. Scoping each key to one workspace avoids the header entirely; the [Anthropic API key setup guide](/blog/anthropic-api-key-setup-for-your-app) explains workspaces and spend limits.
Confirm the fix
- Update the variable on the host, then redeploy or restart. Most hosts apply new variables only to new deployments.
- Run the length and last-four check again from the running server.
- Send one real, small message through the app, not only a test call, and confirm it in the Console's usage view.
- Watch the logs for an hour for 401s from background jobs, which may run in a separate service with their own variables.
Where Shipyard fits, and where it does not
Shipyard checks an Anthropic key with one free call when you add it, and during a launch writes it to your host as ANTHROPIC_API_KEY on the server side only, so the name matches what the SDK reads. It does not watch for expiry or rotate keys for you. If you choose a key that expires, put a reminder in your calendar, or create the key with Never and keep it in a secrets manager, which is the case Anthropic's own docs describe for that option. For the same error on the other big provider, see [OpenAI 401 incorrect API key](/blog/openai-401-incorrect-api-key).
Sources
- Claude API docs: errorsplatform.claude.com
- Claude API docs: Developer API keysplatform.claude.com
- Claude API docs: authentication overviewplatform.claude.com
- Anthropic TypeScript SDK source: client optionscdn.jsdelivr.net
- screenshot-to-code issue 272: invalid x-api-key reportsgithub.com
Launch in an hour, not a weekend
Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.