How to Set ANTHROPIC_API_KEY for Claude

Written by Michael Lip · Solo founder of Zovo · $400K+ on Upwork · 100% JSS Join 50+ builders · More at zovo.one

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

  1. Go to the Anthropic Console.
  2. Navigate to API Keys.
  3. Click “Create Key” and copy the key. It starts with sk-ant-.
  4. 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)

  1. Open System Properties (Win + Pause, or search “Environment Variables”).
  2. Click “Environment Variables”.
  3. Under User variables, click “New”.
  4. Variable name: ANTHROPIC_API_KEY
  5. Variable value: sk-ant-your-key-here
  6. 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

  1. Never commit keys to git: Always use .env files that are in .gitignore, or use environment variables directly.
  2. Use different keys per environment: Create separate keys for development, staging, and production.
  3. Rotate keys regularly: Generate a new key, update all deployments, then revoke the old key.
  4. Use workspace API keys: Workspace-scoped keys limit the blast radius if a key is compromised.