Guides

Claude API Key Guide: Setup and Safety

ClaudeAIHub. All setup steps refer to the official Anthropic Console.

What Is a Claude API Key?

A Claude API key is a secret token that authenticates your requests to the Anthropic API. Every API call you make must include your key. Without it, requests are rejected. Because the key is tied to your billing, anyone who has it can make API calls charged to your account — so protecting it is critical.

Where to Create Your Claude API Key

  1. Go to platform.claude.com and sign in or create an account.
  2. Add a payment method if you haven’t already (required for API access).
  3. Click your account name → SettingsAPI Keys.
  4. Click Create Key, give it a name (e.g., “my-project”), and copy the key immediately.

Important: The key is shown only once after creation. If you close the window without saving it, you will need to generate a new key. There is no way to retrieve a key after you leave the creation screen.

How to Store Your API Key Safely

The most common safe storage method is an environment variable. Your code reads the key at runtime without it ever appearing in your source files.

macOS / Linux / WSL

# Add to ~/.zshrc or ~/.bashrc
export ANTHROPIC_API_KEY="your-key-here"

# Then reload:
source ~/.zshrc

Windows PowerShell (Current Session)

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

Windows — Permanent (System)

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

Using a .env File (Python / Node.js Projects)

Create a .env file in your project root:

ANTHROPIC_API_KEY=your-key-here

Load it in Python with python-dotenv:

from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.environ.get("ANTHROPIC_API_KEY")

Or in Node.js with the dotenv package:

require('dotenv').config();
const apiKey = process.env.ANTHROPIC_API_KEY;

Critical: Add .env to your .gitignore immediately:

# .gitignore
.env
.env.local
.env.*

Security Rules for Claude API Keys

  • Never paste your key into Claude, ChatGPT, or any AI chat. AI conversations may be logged or reviewed.
  • Never commit a .env file to GitHub or any public or private repo.
  • Never share your key via Slack, email, or chat — use short-lived tokens or per-team keys instead.
  • Use one key per project. This way you can rotate a single key without disrupting everything.
  • Set spend limits in the Console so a leaked key has a capped blast radius.
  • Monitor your Console usage dashboard. Unexpected activity is an early sign of key exposure.
  • Rotate keys on a schedule or after any team change.

Using Your Key with the Official SDK

The Anthropic Python SDK reads the key from the environment automatically if you named it ANTHROPIC_API_KEY:

import anthropic

# SDK picks up ANTHROPIC_API_KEY from environment automatically
client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello, Claude!"}]
)
print(message.content[0].text)

Troubleshooting Common API Key Errors

ErrorLikely CauseFix
401 UnauthorizedWrong or missing API keyCheck the key is correct and the env variable is loaded
403 ForbiddenKey lacks permissions or is revokedGenerate a new key in the Console
429 Too Many RequestsRate limit exceededSlow down requests; check Console limits
402 Payment RequiredNo payment method or balance exhaustedAdd/update billing in Console
Key visible in codeHardcoded keyMove to environment variable immediately
Env var not loading.env not loaded or wrong nameVerify variable name is ANTHROPIC_API_KEY and dotenv is called

What to Do if Your Key Is Leaked

  1. Go to Console → API Keys and delete the exposed key immediately.
  2. Generate a replacement key.
  3. Update all services and environment files with the new key.
  4. Review your Console usage for any unexpected API calls.
  5. If charges were incurred, contact Anthropic support through the Console.

Related Guides

Frequently Asked Questions