Claude Code Invalid API Key: Causes and Fixes
Fix Claude Code Invalid API key and Please run /login errors: remove a stale ANTHROPIC_API_KEY, fix the base URL and pasted key. Windows and macOS steps.
GoiAPI · · 15 min read
Ask AI and more

Claude Code shows "Invalid API key" when the key it sends is rejected by the server, or when Claude Code itself blocks the key before sending it. The usual causes are a leftover ANTHROPIC_API_KEY that overrides your login or your new key, a key pasted with a hidden line break or quote marks, or a valid key sent to the wrong address. The quick fix: run /status to see which credential Claude Code is actually using, remove the variables you don't need, then set exactly one base URL and one key.
This guide covers both cases: you pay for a Claude subscription and want the error gone, or you use your own API key with a custom endpoint through ANTHROPIC_BASE_URL. Every command below is given for Windows PowerShell and for macOS/Linux.
Disclosure: GoiAPI sells API access to the models mentioned in this article.
What each "Invalid API key" message means
Claude Code prints several different messages for authentication problems, and each one points to a different fix. The Anthropic messages below are quoted from the Claude Code error reference (checked on December 5, 2026).
| Message | What it means | What to do |
|---|---|---|
Invalid API key · Fix external API key | The key from ANTHROPIC_API_KEY or an apiKeyHelper script was rejected by the API, or Claude Code blocked it before sending | Check the key, remove stale variables, run /status |
Invalid auth token · Fix external auth token | The value of ANTHROPIC_AUTH_TOKEN contains a character an HTTP header can't carry, such as a line break | Re-enter the key cleanly |
Please run /login · API Error: 401 Invalid authentication credentials | Anthropic recognized the credential format but rejected the account or organization behind it | Run /status, then replace the key or log in again |
Not logged in · Please run /login | No usable credential at all. In our test this is what you get when you set a custom base URL but forget the key | Set ANTHROPIC_AUTH_TOKEN (or run /login for a subscription) |
Your ANTHROPIC_API_KEY belongs to a disabled organization · ... | An old Console key from a disabled organization is overriding your subscription | Unset ANTHROPIC_API_KEY |
Failed to authenticate. API Error: 401 followed by the gateway's own text | The request reached your custom endpoint, but the endpoint rejected the key | Fix the key for that endpoint; /login won't help |
That last row matters if you connect Claude Code to any gateway. Anthropic's docs say it plainly: when ANTHROPIC_BASE_URL points to an LLM gateway, the text after 401 is the gateway's message, not Anthropic's, and /login doesn't change it. With GoiAPI, a wrong or revoked key returns The API key is invalid or has been revoked. and a missing key returns Missing API key. Send your key in the Authorization: Bearer <key> header or the x-api-key header. (see GoiAPI authentication docs). If you see one of those, fix your GoiAPI key, not your Claude account.
For errors that are not about authentication (402, 429, 529, timeouts, connection errors), see the Claude Code API error codes guide.
Start with /status
Before changing anything, open claude and type /status. It shows which credential source is active. According to the Claude Code authentication docs (checked on December 5, 2026), when both a login and an API key are configured, /status marks the one that isn't in use.
Use what it tells you:
- An
API keyrow that isn't marked as unused: an environment key is winning over your login. See the stale key section below. - Only your login: run
/loginonce. If the same 401 comes back for the same account, the account or organization itself has lost access. - An
apiKeyHelperrow: a script in your settings is supplying the key. Run that script by hand to see what it prints, or remove the setting if you don't use it. - A custom base URL with a bearer token: the problem is the key or URL for that endpoint. See the section on setting the token and base URL below.
A stale ANTHROPIC_API_KEY overrides your login
This is the most common cause of "Invalid API key · Please run /login" for people who pay for Pro, Max or Ultra. When several credentials exist, Claude Code picks one in a fixed order. Here is the order from the authentication docs (checked on December 5, 2026), shortened to the sources most users have (Bedrock, Vertex and Foundry credentials and Anthropic profiles left out):
| Priority | Source | Sent as |
|---|---|---|
| 1 | ANTHROPIC_AUTH_TOKEN environment variable | Authorization: Bearer <key> header |
| 2 | ANTHROPIC_API_KEY environment variable | X-Api-Key header |
| 3 | apiKeyHelper script in settings | Whatever key the script prints |
| 4 | CLAUDE_CODE_OAUTH_TOKEN environment variable | Long-lived token from claude setup-token |
| 5 | Subscription login from /login (Pro, Max, Ultra, Team, Enterprise) | Saved login |
Your subscription login is at the bottom. Anything above it wins, so /login can't fix an error caused by a key further up the list. Three situations cause most failures:
- You have a subscription but an old
ANTHROPIC_API_KEYis still set. In interactive mode Claude Code asks once whether to use the key and remembers your answer; in non-interactive mode (claude -p) it always uses the key. If the key belongs to an expired or disabled organization, every request fails. To change your earlier answer, use the "Use custom API key" toggle in/config(it only appears whileANTHROPIC_API_KEYis set). - You put a third-party key in
ANTHROPIC_API_KEYbut forgotANTHROPIC_BASE_URL. The request goes straight to Anthropic, which has never seen that key and rejects it. - An old Anthropic Console key sits in
ANTHROPIC_API_KEYwhile your base URL points to another endpoint. That endpoint receives a key it doesn't know and returns 401.
The variable can come from more places than you think. Anthropic's error reference warns that direnv, dotenv shell plugins and IDE terminals can load a stale key from a project's .env file without you setting it. The env block in a Claude Code settings file is another source, covered in the next section.
Remove the variable on Windows PowerShell
List every ANTHROPIC_ variable in the current session:
Get-ChildItem Env:ANTHROPIC*Remove it from the current session:
Remove-Item Env:ANTHROPIC_API_KEYA variable set with setx, or under User variables in the Environment Variables dialog, is stored for your user account and comes back in every new window. Delete it permanently with:
[Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $null, "User")Then close every terminal and every VS Code window and open them again. Programs that are already running keep the old value.
Remove the variable on macOS and Linux
List the variables:
env | grep ANTHROPICRemove it from the current session:
unset ANTHROPIC_API_KEYIf the variable is exported in a shell startup file, it returns in every new terminal. macOS uses Zsh by default, so check ~/.zshrc; for Bash, check ~/.bashrc and ~/.bash_profile. Find the line:
grep -n ANTHROPIC ~/.zshrc ~/.bashrc ~/.bash_profile 2>/dev/nullDelete the export ANTHROPIC_API_KEY=... line, save the file, and open a new terminal.
If you only want your subscription back, remove ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL everywhere, start claude, and confirm with /status.
Set ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL correctly
If you use your own key with a custom endpoint, the setup needs two variables plus a model name. For GoiAPI:
ANTHROPIC_BASE_URL=https://api.goiapi.com, with no/v1at the end. Claude Code adds/v1/messagesitself.ANTHROPIC_AUTH_TOKEN= your GoiAPI key, which starts withsk-.ANTHROPIC_MODEL= a Claude model name from the model list, such asclaude-haiku-5-5orclaude-opus-5-5.
Anthropic's docs recommend ANTHROPIC_AUTH_TOKEN for gateways that take bearer tokens, and it also sits above ANTHROPIC_API_KEY in the priority list, so a leftover API key won't override it.
Put it in settings.json
The most durable place is the env block of your user settings file: ~/.claude/settings.json on macOS and Linux, %USERPROFILE%\.claude\settings.json on Windows (per the Claude Code settings docs, checked on December 5, 2026). The CLI and the VS Code extension both read it.
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.goiapi.com",
"ANTHROPIC_AUTH_TOKEN": "sk-...",
"ANTHROPIC_MODEL": "claude-haiku-5-5"
}
}One detail catches many people. The environment variables reference (checked on December 5, 2026) says that when the same variable is set in both your shell and a settings env block, the settings value applies in most sessions: Claude Code replaces the value inherited from the shell. So if your settings file still holds an old key, fixing the variable in your terminal usually does nothing. Also check the project file .claude/settings.json in your repository: it has its own env block, it is often committed to Git, and a real key should never go there.
Or set it for one terminal session
This matches the GoiAPI Claude Code setup page.
macOS / Linux:
export ANTHROPIC_BASE_URL="https://api.goiapi.com"
export ANTHROPIC_AUTH_TOKEN="sk-..."
export ANTHROPIC_MODEL="claude-haiku-5-5"
claudeWindows PowerShell:
$env:ANTHROPIC_BASE_URL = "https://api.goiapi.com"
$env:ANTHROPIC_AUTH_TOKEN = "sk-..."
$env:ANTHROPIC_MODEL = "claude-haiku-5-5"
claudeFor the full walkthrough, including VS Code, see the Claude Code API key setup guide.
A wrong base URL gives a different error
A bad base URL usually does not produce "Invalid API key". In our test on December 3, 2026 (Claude Code 2.1.325 on Windows 10, claude -p "hi" --max-turns 1), a base URL with an extra /v1, or one using http:// instead of https://, both produced a model error instead:
There's an issue with the selected model (claude-opus-5-5). It may not exist or you may not have access to it. Run --model to pick a different model.If you see that message right after changing your base URL, check the URL before you check the model name.
Also note what happens if you set only the base URL. Anthropic's LLM gateway docs (checked on December 5, 2026) say that setting ANTHROPIC_BASE_URL without a gateway credential keeps your saved claude.ai login as the active credential, and requests still go to the gateway. A third-party endpoint can't use that login, so set the key too. Without any saved login, our test produced Not logged in · Please run /login.
Hidden line breaks, quotes and spaces in a pasted key
Keys copied from email, notes apps or chat messages sometimes carry invisible characters. Before sending a request, Claude Code checks every value that goes into a header. If it finds a line break, a NUL byte, or a character above U+00FF (such as a curly quote or a zero-width space), it stops the request and describes the problem. The error reference gives this example:
Invalid auth token · Fix external auth token · Invalid Authorization header value from ANTHROPIC_AUTH_TOKEN: it contains a line break at character 41 (120 characters on 2 lines).The same check on ANTHROPIC_API_KEY shows up as Invalid API key · Fix external API key followed by a similar description. The message gives the position only and never prints the key, and nothing was sent to the server. Copy the key again from your dashboard rather than from the same note or message, or retype the characters around the reported position.
Two other paste problems to know about:
- Quote marks inside the value. In Windows CMD,
set ANTHROPIC_AUTH_TOKEN="sk-..."keeps both quote marks as part of the variable. Use PowerShell ($env:ANTHROPIC_AUTH_TOKEN = "sk-...") or settings.json instead. - Leading or trailing spaces. In our tests, GoiAPI trimmed ordinary spaces at the start or end of a key, so they did not cause a 401. Other endpoints may not, so remove them anyway.
Test the key outside Claude Code
To find out whether the problem is the key or Claude Code's setup, call the model list directly. On Windows PowerShell, type curl.exe instead of curl:
curl https://api.goiapi.com/v1/models -H "Authorization: Bearer sk-..."A valid key returns the list of models that key may use. A wrong or revoked key returns 401 with The API key is invalid or has been revoked. The list models docs show the full response.
If curl works but Claude Code still fails, the key is fine and Claude Code is reading a different value. Go back to /status, the shell startup files, .env files and the settings env blocks.
Where the VS Code extension gets its key
The authentication docs say ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and apiKeyHelper apply to the CLI and to the VS Code extension. Based on the Claude Code VS Code docs (checked on December 5, 2026), the extension can pick up variables from three places:
- The
envblock in~/.claude/settings.json. Shared by the CLI and the extension. This is the best place for the base URL and key. - The environment VS Code was started with. If you open VS Code from the Start menu or the Dock, it may not inherit variables you set in a terminal. The docs suggest launching it from a terminal with
code .. - The
claudeCode.environmentVariablessetting in VS Code, which sets variables for the Claude process only.
With a third-party endpoint, turn on Disable Login Prompt (claudeCode.disableLoginPrompt) so the extension stops asking you to sign in to a Claude account.
Two VS Code problems come up often: an old VS Code window still holds the old key even though you fixed it in your terminal, and a forgotten key sits in claudeCode.environmentVariables. We have not tested the VS Code extension with GoiAPI ourselves. After you configure it, send one short message and check the Request history page in your GoiAPI dashboard to confirm the request arrived.
Still failing? Check these next
If you removed the extra variables and set the configuration again but the error remains, go through this list in order:
- Is the key still active? Open the API Keys page in your dashboard. A key is shown only once, when you create it. If you're not sure you copied all of it, create a new key.
- Is it a 403, not a 401? With GoiAPI,
This API key cannot be used from IP address {ip}.orThis API key is not allowed to use the model {model}.means the key works but is restricted by IP or model. Change the key's settings in the dashboard. The GoiAPI error codes page explains 401 vs 403. - A model error appears right after you fix the key. GoiAPI checks the key before the model: in our test, a wrong key with a made-up model name still returned only 401. Once the key is right, a wrong model name can surface. Check it against the model list.
- An
apiKeyHelpersetting is still in a settings file. Its output ranks above your saved login. Remove it if you don't use it. - Another session or tool sets the variable. CI jobs, Docker containers and dev containers have their own environment. Run
/statusinside the same environment that fails.
Every GoiAPI response includes an x-request-id header. Send that ID to support if you need a request looked up. Never send your key: support will never ask for it.
Subscription or API key: pick one per machine
If you have a Pro, Max or Ultra plan with usage left, the simplest fix is to remove ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL and use /login. If you use a gateway instead, Anthropic's LLM gateway docs explain that requests carry the gateway credential, the subscription's usage limits don't apply, and the traffic is billed per token to whoever owns that credential. Keeping both set up on one machine is what produces most of the confusing errors above. If you're deciding between the two, see Claude Code Max vs API cost.
FAQ
Why does Claude Code say Invalid API key when I'm already logged in?
Because ANTHROPIC_API_KEY ranks above your subscription login. If the variable is still set in your shell, in ~/.zshrc or ~/.bashrc, in a .env file loaded by your terminal, or in the env block of a settings file, Claude Code uses that key instead of your login. Remove it everywhere, open a new terminal, and confirm with /status.
How do I fix "Invalid API key · Please run /login"?
Run /status first. If an API key is the active credential, unset ANTHROPIC_API_KEY (PowerShell: Remove-Item Env:ANTHROPIC_API_KEY; macOS/Linux: unset ANTHROPIC_API_KEY) and remove it from startup files and settings. If you meant to use the key, replace it with a valid one. Running /login alone won't help while the key is set.
How do I remove ANTHROPIC_API_KEY permanently on Windows?
Run [Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $null, "User") in PowerShell, also run Remove-Item Env:ANTHROPIC_API_KEY for the current window, then close and reopen every terminal and VS Code window. The Windows section above has the full steps.
Should I use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY with a custom base URL?
Use ANTHROPIC_AUTH_TOKEN when your endpoint accepts bearer tokens, which is the case Anthropic describes for gateways. GoiAPI accepts the key in either header, and if a request carries both, x-api-key is used. Since that can mean a stale key wins, keep the key in only one variable.
Where does the Claude Code VS Code extension read the API key from?
From the env block in ~/.claude/settings.json, from the environment VS Code was launched with, and from the claudeCode.environmentVariables setting. Putting it in settings.json lets the CLI and the extension share one configuration.
Why do I get a model error instead of Invalid API key after changing the base URL?
A base URL ending in /v1, or using http://, makes Claude Code report There's an issue with the selected model (...) in our tests. Set the base URL to exactly https://api.goiapi.com.
Get a working key with GoiAPI
If you want Claude Code on a pay-per-token key without a subscription or a credit card:
- Create a GoiAPI account.
- Top up with USDT (TRC20 or BEP20) or Binance Pay. There is no monthly fee. Top-ups are final and non-refundable, so only add what you plan to use. New to paying with crypto? See how to pay for the Claude API with USDT, no credit card.
- Create an API key in the dashboard and copy it straight into settings.json or your terminal.
- Set
ANTHROPIC_BASE_URLtohttps://api.goiapi.comandANTHROPIC_AUTH_TOKENto your key, as shown above.
Prices checked on December 5, 2026, per 1M tokens (current rates on the pricing page):
| Model | Model name | Input | Output |
|---|---|---|---|
| Claude Haiku 5.5 | claude-haiku-5-5 | $0.0221 | $0.1105 |
| Claude Opus 5.5 | claude-opus-5-5 | $0.442 | $2.21 |
The quickstart walks through your first request in a few minutes.


