Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Claude Code · Sign-in

Invalid API key · Fix external API key

Fix “Invalid API key · Fix external API key” in Claude Code: find which key is used, replace or remove it, or switch back to your subscription.

Message

The exact error

Claude Code
Invalid API key · Fix external API key

Also appears as:

When the key contains a character headers can’t carry
Invalid API key · Fix external API key · Invalid X-Api-Key header value from ANTHROPIC_API_KEY: it contains a line break at character 41 (120 characters on 2 lines).

Meaning

What does “Fix external API key” mean?

“External” means the key came from outside Claude Code’s own login: the ANTHROPIC_API_KEY variable or an apiKeyHelper script. Either the API rejected it (wrong, revoked or from another account) or Claude Code stopped it first because it contains a character an HTTP header can’t hold, such as a line break from a bad copy-paste. Because an approved key takes priority over your saved login, running /login alone doesn’t fix it: the key has to be replaced or removed.

Causes

Where is Claude Code getting the key from?

  • A typo, a truncated paste, or a key that was revoked in the Console
  • A stale key loaded from a .env file by direnv, a shell plugin or your IDE’s terminal
  • An old export ANTHROPIC_API_KEY=… line in your shell profile or Windows user variables
  • A line break, space or quote character inside the variable
  • An apiKeyHelper script printing something other than the key

Fix

How do I fix an invalid API key in Claude Code?

  1. Find out where the key comes from

    Run /status in Claude Code to see which credential it is actually using. Look for .env files in your project if you didn’t set the variable yourself.

    macOS / Linux
    env | grep ANTHROPIC
    Windows PowerShell
    Get-ChildItem Env:ANTHROPIC*
  2. Check the key in the Console

    Confirm it exists and isn’t revoked at platform.claude.com/settings/keys. Create a new one if in doubt and paste it without surrounding quotes or line breaks. To test a key outside Claude Code, the API key checker sends it straight to Anthropic from your browser; how to get a Claude API key covers creating a new one.

  3. Or use your subscription instead

    Unset ANTHROPIC_API_KEY and run /login.

  4. Remove the key for good

    Unsetting it only fixes the current shell. Delete export ANTHROPIC_API_KEY=… lines from ~/.zshrc, ~/.bashrc or ~/.profile; on Windows, check your PowerShell profile ($PROFILE) and your User environment variables. Then open a new terminal and confirm with /status.

    Windows PowerShell: remove the user-level variable
    [Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', $null, 'User')
  5. Test an apiKeyHelper script

    Run the script by hand: it must print only the key on standard output.

Explained

Why doesn’t /login fix an invalid API key?

Claude Code uses one credential, chosen in a fixed order: ANTHROPIC_AUTH_TOKEN, then ANTHROPIC_API_KEY, then an apiKeyHelper script, all ahead of the login you create with /login. In an interactive session you approve an ANTHROPIC_API_KEY once and the choice is remembered (the Use custom API key toggle in /config changes it while the variable is set); in -p mode a key that is present is always used. So a bad key keeps winning until you fix or remove it.

Explained

Why do I get “Invalid API key” in the VS Code extension?

The extension uses the same credentials as the CLI, including ANTHROPIC_API_KEY and apiKeyHelper, taken from the environment VS Code was started with. An old key in your shell profile, or one loaded from a project .env file, therefore reaches the extension too. Remove it from that environment, fully quit VS Code and reopen it, or sign in with your Claude account instead.

Explained

What about “This organization has been disabled”?

Your ANTHROPIC_API_KEY belongs to a disabled organization and API Error: 400 ... This organization has been disabled. have the same root cause: a stale key, often from a previous employer or project, overriding your working subscription. Unset it, remove it from your shell profile, relaunch claude and confirm with /status. If a key is accepted but its organization has no credit left, you see Credit balance is too low instead; if no credential is found at all, Not logged in.

FAQ

Questions people ask

Why does Claude Code use my API key instead of my Pro or Max subscription?

When ANTHROPIC_API_KEY is set and you approved it, Claude Code uses that key ahead of your subscription login, and in -p mode a present key is always used. An old key in your shell profile or a project .env file therefore overrides /login. Unset the variable, remove it from the profile, open a new terminal and run /login.

How do I fix “Invalid API key” in VS Code?

The VS Code extension reads the same ANTHROPIC_API_KEY and apiKeyHelper as the CLI, from the environment VS Code started with. Remove the stale key from your shell profile or the project’s .env file, fully quit VS Code and reopen it so the extension restarts, or unset the key and sign in with your Claude account.

How do I test whether my Anthropic API key works?

Use a key checker that sends the key straight from your browser to Anthropic, such as the API key checker on this site, or look it up in the Claude Console under API keys. If the key fails or is missing there, it is wrong, revoked or from another organization: create a new one and paste it without quotes or line breaks.

Related

Search all Claude Code errors

Keep exploring