How to Set ANTHROPIC_API_KEY for Claude
The Anthropic Python and TypeScript SDKs read your API key from the ANTHROPIC_API_KEY environment variable. This guide covers every way to set it across macOS, Linux, and Windows.
Quick Fix
# macOS / Linux
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"
Then test it:
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=100,
messages=[{"role": "user", "content": "Say hello"}]
)
print(message.content[0].text)
Full Solution
Get Your API Key
- Go to the Anthropic Console.
- Navigate to API Keys.
- Click “Create Key” and copy the key. It starts with
sk-ant-. - Store it securely. You cannot view the key again after creation.
macOS / Linux: Temporary (Current Session)
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
This is lost when you close the terminal.
macOS / Linux: Permanent
Add the export to your shell configuration file:
# For bash (most Linux systems)
echo 'export ANTHROPIC_API_KEY="sk-ant-your-key-here"' >> ~/.bashrc
source ~/.bashrc
# For zsh (macOS default)
echo 'export ANTHROPIC_API_KEY="sk-ant-your-key-here"' >> ~/.zshrc
source ~/.zshrc
Windows: PowerShell (Current Session)
$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"
Windows: PowerShell (Permanent)
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "sk-ant-your-key-here", "User")
Restart PowerShell after setting.
Windows: Command Prompt (Current Session)
set ANTHROPIC_API_KEY=sk-ant-your-key-here
Windows: System Environment Variables (Permanent)
- Open System Properties (Win + Pause, or search “Environment Variables”).
- Click “Environment Variables”.
- Under User variables, click “New”.
- Variable name:
ANTHROPIC_API_KEY - Variable value:
sk-ant-your-key-here - Click OK. Restart any open terminals.
Using .env Files (Python)
For project-specific keys, use python-dotenv:
pip install python-dotenv
Create a .env file in your project root:
ANTHROPIC_API_KEY=sk-ant-your-key-here
Load it in your Python script:
from dotenv import load_dotenv
load_dotenv()
import anthropic
client = anthropic.Anthropic() # Reads from environment
Add .env to your .gitignore:
# .gitignore
.env
Using .env Files (TypeScript / Node.js)
npm install dotenv
Create .env:
ANTHROPIC_API_KEY=sk-ant-your-key-here
import "dotenv/config";
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic(); // Reads from environment
Passing the Key Explicitly
If you cannot use environment variables:
import anthropic
client = anthropic.Anthropic(api_key="sk-ant-your-key-here")
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "sk-ant-your-key-here" });
This is not recommended for production. Hardcoded keys can be accidentally committed to version control.
Platform-Specific Clients
Bedrock, Vertex AI, and Foundry use their own authentication and do NOT use ANTHROPIC_API_KEY:
import anthropic
# Amazon Bedrock (uses AWS credentials from environment/config)
client = anthropic.AnthropicBedrock()
# Google Vertex AI (uses Google Cloud credentials)
client = anthropic.AnthropicVertex(
project_id="your-project",
region="us-east5"
)
# Microsoft Foundry
client = anthropic.AnthropicFoundry()
Verify Your Key Is Set
# Check if the variable is set
echo $ANTHROPIC_API_KEY
# Test with a Python one-liner
python3 -c "import anthropic; print(anthropic.Anthropic().messages.create(model='claude-haiku-4-5', max_tokens=50, messages=[{'role':'user','content':'Say hi'}]).content[0].text)"
Debug Authentication Issues
# Enable SDK debug logging to see headers
ANTHROPIC_LOG=debug python your_script.py
Prevention
- Never commit keys to git: Always use
.envfiles that are in.gitignore, or use environment variables directly. - Use different keys per environment: Create separate keys for development, staging, and production.
- Rotate keys regularly: Generate a new key, update all deployments, then revoke the old key.
- Use workspace API keys: Workspace-scoped keys limit the blast radius if a key is compromised.
Related Guides
- Claude API Error 401 authentication_error Fix – troubleshoot when your key is rejected.
- Claude Python SDK Installation Guide – full Python SDK setup.
- Claude TypeScript SDK Installation Guide – full TypeScript SDK setup.
- Claude Python SDK Getting Started – your first API call after setting the key.
- Claude SDK Timeout Configuration – configure the client after authentication.