Fix: Claude Code ANTHROPIC_API_KEY Not Set Error
How to fix the ANTHROPIC_API_KEY environment variable not set error in Claude Code.
You just installed Claude Code, ran claude in your terminal, and got this error:
Error: ANTHROPIC_API_KEY environment variable is not set
This is the most common first-time setup issue. Here’s how to fix it.
Why This Happens
Claude Code needs an API key to authenticate with Anthropic’s servers. Without it, the CLI cannot make any requests. The error means either:
- You haven’t set the environment variable yet
- You set it in a different terminal session
- Your shell configuration isn’t loading properly
Solution
Step 1: Get Your API Key
- Go to console.anthropic.com
- Sign in or create an account
- Navigate to API Keys section
- Create a new key and copy it
Step 2: Set the Environment Variable
For macOS/Linux (zsh):
echo 'export ANTHROPIC_API_KEY="your-key-here"' >> ~/.zshrc
source ~/.zshrc
For macOS/Linux (bash):
echo 'export ANTHROPIC_API_KEY="your-key-here"' >> ~/.bashrc
source ~/.bashrc
For Windows (PowerShell):
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', 'your-key-here', 'User')
Then restart your terminal.
Step 3: Verify
echo $ANTHROPIC_API_KEY
You should see your key (or at least the first few characters). Now run claude again.
Common Mistakes
Wrong shell config file
Check which shell you’re using:
echo $SHELL
/bin/zsh→ use.zshrc/bin/bash→ use.bashrc
Typo in variable name
It must be exactly ANTHROPIC_API_KEY. Not ANTHROPIC_KEY, not CLAUDE_API_KEY.
Forgot to source
After editing your shell config, either:
- Run
source ~/.zshrc(or.bashrc) - Or open a new terminal window
Key has extra spaces
Make sure there are no spaces around the equals sign or inside the quotes:
# Wrong
export ANTHROPIC_API_KEY = "sk-..."
export ANTHROPIC_API_KEY="sk-... "
# Correct
export ANTHROPIC_API_KEY="sk-..."
Using a .env File (Alternative)
If you prefer not to set a global environment variable, you can use a .env file in your project:
# .env
ANTHROPIC_API_KEY=your-key-here
Then run Claude Code with:
source .env && claude
Or use a tool like direnv to auto-load it.
Security Note
Never commit your API key to git. Add .env to your .gitignore:
echo ".env" >> .gitignore
If you accidentally exposed your key, revoke it immediately in the Anthropic console and create a new one.
Still Not Working?
If the error persists after following these steps:
- Open a completely new terminal window
- Check if the variable is set:
printenv | grep ANTHROPIC - Try setting it inline:
ANTHROPIC_API_KEY="your-key" claude
If inline works but the export doesn’t, your shell config file isn’t being loaded. Check if you have a .zprofile or .bash_profile that might be conflicting.