Claude Code · Sign-in
Invalid API key · Fix external API key
The key from ANTHROPIC_API_KEY or your apiKeyHelper was rejected by the API, or blocked before sending because it’s malformed.
Message
The exact error
Invalid API key · Fix external API keyAlso 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 it means
“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.
Causes
Common causes
- A typo, a truncated paste, or a key that was revoked in the Console
- A stale key loaded from a
.envfile by direnv, a shell plugin or your IDE’s terminal - A line break, space or quote character inside the variable
- An
apiKeyHelperscript printing something other than the key
Fix
How to fix it
Find out where the key comes from
Run
/statusin Claude Code to see which credential it is actually using. Look for.envfiles in your project if you didn’t set the variable yourself.macOS / Linux env | grep ANTHROPICWindows PowerShell Get-ChildItem Env:ANTHROPIC*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.
Or use your subscription instead
Unset
ANTHROPIC_API_KEYand run/login.Test an apiKeyHelper script
Run the script by hand: it must print only the key on standard output.
Related