Claude API Invalid API Key Format Error (2026)

The Error

Error 401: Invalid API Key
{
  "type": "error",
  "error": {
    "type": "authentication_error",
    "message": "Invalid API key format. API keys must start with 'sk-ant-api03-'."
  }
}

The Fix

  1. Verify your key format and strip hidden characters
# Check the key prefix (first 15 chars only — never print full key)
echo $ANTHROPIC_API_KEY | cut -c1-15
# Expected: sk-ant-api03-
# Check for hidden whitespace or newlines
echo -n "$ANTHROPIC_API_KEY" | wc -c
# A valid key is typically 108 characters
  1. Re-export the key with proper quoting
# Remove any trailing whitespace/newlines that crept in from copy-paste
export ANTHROPIC_API_KEY=$(echo -n "sk-ant-api03-YOUR_KEY_HERE" | tr -d '[:space:]')
  1. Verify the fix:
curl -s https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"claude-sonnet-4-20250514","max_tokens":5,"messages":[{"role":"user","content":"hi"}]}' \
  | python3 -c "import sys,json; print(json.load(sys.stdin).get('content',[{}])[0].get('text','ERROR'))"
# Expected: A short response (not an error message)

Why This Happens

Anthropic API keys follow a strict format: they must begin with sk-ant-api03- and contain only alphanumeric characters plus hyphens. The most common cause of format errors is invisible whitespace — a trailing newline from pbpaste, a zero-width space from a web dashboard copy, or carriage returns from Windows line endings. Storing keys in .env files without proper quoting also introduces these issues.

If That Doesn’t Work

  • Alternative 1: Generate a fresh key at console.anthropic.com/settings/keys — old keys with sk-ant-api02- prefix are deprecated
  • Alternative 2: Check if your shell config file (.bashrc/.zshrc) has a conflicting ANTHROPIC_API_KEY export that overrides yours
  • Check: Run env | grep ANTHRO to see all Anthropic-related environment variables and spot conflicts

Prevention

Add to your CLAUDE.md:

Store API keys in .env files with no trailing whitespace. Always validate key format starts with sk-ant-api03- before making requests. Use a secrets manager for production deployments.

Related articles: Claude API 401 Auth Error, Invalid API Key After Rotation, Claude Code Config File Location

See Also

This fix also applies if you see variations of this error:

  • Connection or process errors with similar root causes in the same subsystem
  • Timeout variants where the operation starts but does not complete
  • Permission variants where access is denied to the same resource
  • Configuration variants where the same setting is missing or malformed

If your specific error message differs slightly from the one shown above, the fix is likely the same. The key indicator is the operation that failed (shown in the stack trace) rather than the exact wording of the message.

Frequently Asked Questions

Does this error affect all operating systems?

This error can occur on macOS, Linux, and Windows (WSL). The exact error message may differ slightly between platforms, but the root cause and fix are the same. macOS users may see additional Gatekeeper or notarization prompts. Linux users should check that the relevant system packages are installed. Windows users should ensure they are running inside WSL2, not native Windows.

Will this error come back after updating Claude Code?

Updates can occasionally reintroduce this error if the update changes default configurations or dependency requirements. After updating Claude Code, verify your project still builds and runs correctly. If the error returns, reapply the fix and check the changelog for breaking changes.

Can this error cause data loss?

No, this error occurs before or during an operation and does not corrupt existing files. Claude Code’s edit operations are atomic — they either complete fully or not at all. However, if the error occurs during a multi-step operation, you may have partial changes that need to be reviewed with git diff before continuing.

How do I report this error to Anthropic if the fix does not work?

Open an issue at github.com/anthropics/claude-code with: (1) the full error message including stack trace, (2) your Node.js version (node --version), (3) your Claude Code version (claude --version), (4) your operating system and version, and (5) the command or operation that triggered the error.

Try it: Paste your error into our Error Diagnostic for an instant fix.