Claude Code "invalid x-api-key": Your Request Went to Anthropic
Wokey Team · 2026-09-24
Short answer: if you see invalid x-api-key, your request went to Anthropic's own api.anthropic.com, not to the gateway you configured. Wokey never returns that message; its key error is invalid_api_key, with underscores. So don't rotate your key yet. Find out why the base URL wasn't applied.
First, find out who answered
Match the JSON in your error against this table to see where the request actually landed. Every row is a raw response captured with curl:
| Response contains | Answered by | Meaning |
|---|---|---|
"message":"invalid x-api-key" |
Anthropic | An sk- key was sent as Authorization: Bearer, or the x-api-key value doesn't look like a key |
"message":"API key is invalid." |
Anthropic | An sk- key not issued by Anthropic was sent as x-api-key |
"message":"x-api-key header is required" |
Anthropic | No key was sent at all |
"message":"Invalid bearer token" |
Anthropic | The bearer value doesn't start with sk- |
"code":"invalid_api_key" |
Wokey | The request reached Wokey, but the key is missing, mistyped or deleted |
Anthropic's full response looks like this. Note the outer "type":"error" and, usually, a request_id:
{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"},"request_id":"req_011C..."}
Wokey's response has no outer type: "error", and the code uses underscores:
{"error":{"code":"invalid_api_key","message":"invalid_api_key","type":"invalid_request_error"}}
Wokey keys start with sk-. Put one in ANTHROPIC_AUTH_TOKEN (sent as a bearer token) and let the request go to Anthropic, and you get invalid x-api-key. Put it in ANTHROPIC_API_KEY (sent as x-api-key) and you get API key is invalid. The cause is the same in both cases.
Why the base URL wasn't applied
1. The variable isn't in the process running claude. An export only lasts for the current terminal. A variable set in another terminal, one added to ~/.zshrc without opening a new terminal, or an editor launched from the Dock or Start menu won't see it. In the terminal that starts claude, check that both variables are there:
env | grep ANTHROPIC
2. The variable name is wrong. Claude Code only reads ANTHROPIC_BASE_URL. ANTHROPIC_API_BASE, ANTHROPIC_BASE, CLAUDE_BASE_URL and OPENAI_BASE_URL are all ignored, and the request silently goes to the official address.
3. settings.json has the key but not the address. If the env block of ~/.claude/settings.json sets ANTHROPIC_AUTH_TOKEN without ANTHROPIC_BASE_URL, the key is sent to Anthropic. Keep the two together:
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.wokey.ai",
"ANTHROPIC_AUTH_TOKEN": "your Wokey API key"
}
}
4. You're using the VS Code extension. The extension's login check doesn't read shell variables. Set them in claudeCode.environmentVariables in VS Code's user settings; see the VS Code extension guide.
5. You're using the Claude desktop app. The desktop app reads neither ANTHROPIC_BASE_URL nor settings.json. Set the gateway under Developer → Configure Third-Party Inference; see troubleshooting the desktop app with a third-party API.
6. You're calling the SDK from your own code. Anthropic's Python and TypeScript SDKs default to the official address. Pass base_url explicitly (baseURL in TypeScript), again without /v1:
from anthropic import Anthropic
client = Anthropic(base_url="https://api.wokey.ai", api_key="your Wokey API key")
Confirm inside Claude Code
Run /status in Claude Code. The Status tab should show an Anthropic base URL line set to https://api.wokey.ai. If the line is missing, Claude Code is using the official address and one of the causes above applies.
Compare with curl
In the same terminal that starts claude, run:
curl -X POST "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-5","max_tokens":1,"messages":[{"role":"user","content":"."}]}'
What the result tells you:
curl: (3) URL rejected: No host part in the URL:ANTHROPIC_BASE_URLis empty; the variable was never set.- Starts with
{"id":"msg_: the gateway and key are fine. The problem is the config Claude Code reads; work through causes 3 to 5 above. invalid_api_key: the request reached Wokey, so now it really is the key. Check in the console that it was copied in full and hasn't been deleted.
Seeing it with the official API
If you're using Anthropic's API directly, invalid x-api-key means what it says: the key is wrong or revoked, or it was copied with a stray newline or space. Claude Code blocks a key containing a line break before sending it, with an error like Invalid X-Api-Key header value from ANTHROPIC_API_KEY: it contains a line break.
FAQ
Does Wokey return "invalid x-api-key"?
No. That message comes from Anthropic's own API. For a missing or wrong key, Wokey returns a 401 with {"error":{"code":"invalid_api_key",...}}. If you see "invalid x-api-key", the request never reached Wokey because ANTHROPIC_BASE_URL was not applied.
How do I check which base URL Claude Code is using?
Run /status in Claude Code and look at Anthropic base URL. With Wokey it should read https://api.wokey.ai. If the line is missing or shows Anthropic's address, the variable did not reach the running process.
echo shows the variable, so why does it still fail?
Usually the variable was set without export, or Claude Code was launched from another terminal, an IDE or a desktop icon that never saw it. The most reliable fix is to put ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in the env block of ~/.claude/settings.json.
What if the VS Code extension or the desktop app shows this error?
For the VS Code extension, set the variables in claudeCode.environmentVariables in your user settings. The Claude desktop app ignores environment variables: configure the gateway under Developer → Configure Third-Party Inference, then click Save & Restart.
Can I use a Wokey key with Anthropic directly?
No. A Wokey key is sk- followed by 32 hex characters and only works at api.wokey.ai. Sent to Anthropic, it gets a 401.