Fix: Claude Code 'API Key Not Found' Error — Complete Troubleshooting Guide

Step-by-step guide to fixing the 'API key not found' error in Claude Code for Windows, macOS, and Linux. Covers .env setup, environment variables, CLI config, and common pitfalls.

·10 min read

The Problem

You install Claude Code, run your first command, and get:

Error: API Key Not Found

Or variations like:

Claude API Error: 401 Unauthorized — No API key provided
Error: ANTHROPIC_API_KEY is not set

This is the #1 stumbling block for new Claude Code users. The fix is straightforward — you just need to get your API key into the right place. Let's walk through every possible cause and solution.

---

Step 1: Get Your API Key (If You Haven't Already)

First, make sure you actually have an API key:

1. Go to the Anthropic Console 2. Log in (or create an account — you'll need to add billing) 3. Navigate to API KeysCreate Key 4. Copy the key (you'll only see it once — save it somewhere safe) 5. Top up your account with at least \$5 credit

> 💡 Tip: The API key starts with sk-ant-.... If yours doesn't look like that, you might have the wrong key.

External Links: - Anthropic Console — API Keys - Anthropic API Pricing - Anthropic API Documentation

---

Step 2: Set the Environment Variable (The Standard Way)

Claude Code looks for your API key in the ANTHROPIC_API_KEY environment variable.

For Linux/macOS — add to your terminal profile (~/.zshrc, ~/.bashrc, or ~/.zprofile):

export ANTHROPIC_API_KEY="sk-ant-your-key-here"

Then reload:

source ~/.zshrc

For Windows (PowerShell) — add to your PowerShell profile:

[System.Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY','sk-ant-your-key-here','User')

Or add this to your $PROFILE:

$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"

Method B: Export Per-Session (Quick Test)

export ANTHROPIC_API_KEY="sk-ant-your-key-here"
claude

This only works for the current terminal session — close the terminal and it's gone.

Method C: .env File (For Project-Specific Setup)

Create a .env file in your project root:

ANTHROPIC_API_KEY=sk-ant-your-key-here

Then source it before running Claude Code:

export $(grep -v '^#' .env | xargs) && claude

> ⚠️ Important: Never commit your .env file to git! Add .env to .gitignore — and never use .env.example as your actual config file. If you accidentally expose your key, rotate it immediately.

External Links: - Dotenv (npm package) — auto-load .env files in Node.js projects - GitHub — .env.gitignore template

---

Step 3: Verify It Worked

# Check that the env var is set
echo $ANTHROPIC_API_KEY

# Expected output: sk-ant-xxxxxxxxxxxxx

# Run Claude Code to confirm claude --print "Hello, can you hear me?"

If Claude responds, you're all set.

---

Step 4: If It Still Doesn't Work — Advanced Debugging

Problem: The Variable Shows Empty After Export

Possible cause: You exported it in the wrong shell profile, or the profile didn't source.

# Check your active shell
echo $SHELL  # Is it /bin/zsh or /bin/bash?

Fix: Edit the correct profile file — ~/.zshrc for Zsh, ~/.bashrc for Bash, ~/.bash_profile for login shells.

Problem: "Claude: command not found"

You haven't installed Claude Code yet:

npm install -g @anthropic-ai/claude-code

Or with the standalone installer:

curl -fsSL https://claude.ai/install | bash

External Links: - Claude Code Installation Guide - Claude Code GitHub Repository

Problem: "403 Forbidden" or "Billing required"

Your API key exists but either: - Your account has no billing set up — add credit - You're on a free trial that expired - The key was revoked — generate a new one

Problem: Multiple Terminal Windows / IDE Terminal

Environment variables are session-scoped. If you open a new terminal tab or an IDE's built-in terminal, that session won't have the variable. You need to source it in every new terminal, or use a global method (like your shell profile).

Problem: Using Zsh with Oh My Zsh

Oh My Zsh users should add the export to ~/.zshrc — not .zprofile or .bash_profile. The .zshrc file is sourced for every interactive Zsh session.

Problem: VS Code Terminal Doesn't Pick It Up

VS Code terminals might not source your shell profile. Two fixes:

1. Set it in VS Code settings: Add to .vscode/settings.json:

{
  "terminal.integrated.env.linux": {
    "ANTHROPIC_API_KEY": "sk-ant-your-key-here"
  }
}

2. Or add this to your VS Code settings.json (File → Preferences → Settings → Open Settings JSON):

"terminal.integrated.env.linux": {
  "ANTHROPIC_API_KEY": "sk-ant-your-key-here"
}

External Links: - VS Code Integrated Terminal Configuration - Oh My Zsh Documentation

---

Verification Checklist

Use this checklist to confirm everything is working:

| Check | Command | Expected | |-------|---------|----------| | Key exists | echo $ANTHROPIC_API_KEY | sk-ant-... | | Claude Code installed | claude --version | A version number | | API reachable | claude --print "ping" | A response | | Works in VS Code | Open VS Code terminal, repeat above | Same results |

---

Summary

The "API Key Not Found" error has three causes 99% of the time:

1. You don't have an API key yet → Get one at console.anthropic.com 2. You have a key but haven't set the env var → Add export ANTHROPIC_API_KEY=... to your shell profile 3. You set it but in the wrong session → Source your profile, restart terminal, or use a global setting

If none of the above works, check Anthropic's status page for any ongoing incidents, and consider asking on the Anthropic Discord or browsing GitHub Issues.

Related Articles: - How to Install Claude Code in 5 Minutes — full installation walkthrough - Effective Claude Code Prompts — get better results after you're set up - Claude Code vs GitHub Copilot — compare the tools

Ad Unit Placeholder

Related Articles