API Configuration

Getting Anthropic API Key

The Anthropic API Key is essential for using Claude Code. Let's walk through obtaining one:

Step 1: Register Anthropic Account

  1. Visit Anthropic Console
  2. Click "Sign Up"
  3. Register using Email or Google account
  4. Verify email address

Step 2: Add Account Balance

Important: Anthropic API is a paid service requiring prepaid credits
  1. After login, go to "Billing" page
  2. Click "Add Credit"
  3. Select amount (recommended $20-$50 for first time)
  4. Complete payment with credit card

Step 3: Create API Key

  1. Go to "API Keys" page
  2. Click "Create Key"
  3. Set key name (e.g., claude-code-dev)
  4. (Optional) Set usage limits and permissions
  5. Click "Create"
  6. Important: Copy and save API Key immediately (shown only once!)

API Key Format:

sk-ant-api03-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Configuring API Key

There are multiple ways to configure your API Key—choose what works best:

# Start Claude Code and run /login inside the session
claude
> /login

# Follow the prompts to choose:
# - Claude subscription account (Pro/Max, browser OAuth)
# - Anthropic Console account (pay-per-use API billing)

Method 2: Environment Variable

Temporary (Current Session):

# Linux/macOS
export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"

# Windows (PowerShell)
$env:ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"

Permanent:

Linux/macOS (Bash):

# Edit .bashrc or .zshrc
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"' >> ~/.bashrc
source ~/.bashrc

Linux/macOS (Fish):

set -Ux ANTHROPIC_API_KEY "sk-ant-api03-xxxxx"

Windows (PowerShell):

[System.Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', 'sk-ant-api03-xxxxx', 'User')

Method 3: The env Field in Settings

Edit ~/.claude/settings.json and use the env field to inject environment variables into sessions (useful for proxy gateways and similar setups):

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://your-gateway.example.com",
    "ANTHROPIC_AUTH_TOKEN": "your-token"
  }
}

Method 4: apiKeyHelper Script

If your team distributes keys via a script, configure apiKeyHelper in ~/.claude/settings.json; Claude Code will run the script to obtain the API Key:

{
  "apiKeyHelper": "/path/to/get-api-key.sh"
}

API Pricing and Usage

Model Pricing (January 2026)

ModelInputOutputUse Case
Claude Opus 4.8$5/M tokens$25/M tokensMost complex tasks
Claude Sonnet 4.6$3/M tokens$15/M tokensBalanced (Recommended)
Claude Haiku 4.5$1/M tokens$5/M tokensFast simple tasks

M = Million

Cost Estimation Examples

Simple Task (Fix small bug):

  • Input: ~1,000 tokens
  • Output: ~500 tokens
  • Cost: ~$0.01 (Sonnet 4.6)

Medium Task (Implement new feature):

  • Input: ~5,000 tokens
  • Output: ~2,000 tokens
  • Cost: ~$0.05 (Sonnet 4.6)

Complex Task (Refactor large module):

  • Input: ~20,000 tokens
  • Output: ~8,000 tokens
  • Cost: ~$0.20 (Sonnet 4.6)

View Usage

# Inside a session: view the current session's token cost
> /cost

# Subscription users: view plan usage
> /usage

# API-billed users: see detailed statistics on the
# Anthropic Console "Usage" page

Cost Control Tips

  1. Choose Right Model

    • Use Haiku for simple tasks
    • Use Sonnet for general tasks
    • Use Opus only for complex tasks
  2. Optimize Prompts

    • Clear, concise task descriptions
    • Reduce unnecessary context
  3. Use Subagents

    • Use Explore Agent (haiku) for exploration
    • Use Plan Agent for planning
  4. Set Budget Limits

    • In the Anthropic Console, open "Limits" to set monthly spending caps
    • Configure usage alerts to get notified before hitting the cap

API Security Best Practices

🔒 Protect Your API Key

1. Never Commit to Version Control

# Add to .gitignore
.env
config.json
**/*api-key*

2. Use Environment Variables

# Recommended: use environment variables, not hardcoded
export ANTHROPIC_API_KEY="sk-ant-xxx"

3. Limit Key Permissions

  • Set key permissions in Anthropic Console
  • Create separate keys for different environments
  • Rotate API Keys regularly

4. Monitor Usage

# Inside a session: check the current session's token cost
> /cost

# Subscription users: check plan usage
> /usage

# API-billed users: use the Usage page in the Anthropic Console
# to review usage details and set monthly budgets and alerts

5. Secure Storage

# Limit config file permissions
chmod 600 ~/.claude/settings.json

⚠️ Response to Leakage

If API Key is leaked:

  1. Revoke Key Immediately

    • Log into Anthropic Console
    • Delete leaked key on API Keys page
  2. Check Usage Records

    • Look for abnormal calls
    • Check billing
  3. Create New Key

    • Generate new API Key
    • Update all configurations
  4. Update Security Measures

    • Check .gitignore
    • Strengthen access control

Managing Multiple API Keys

For teams or multi-project scenarios, you might need to manage multiple API Keys:

Scenario 1: Different Projects with Different Keys

In each project's .claude/settings.local.json (personal config, not committed to git), specify the key for that project via the env field:

{
  "env": {
    "ANTHROPIC_API_KEY": "sk-ant-projectA-xxx"
  }
}

Claude Code applies this configuration automatically when started in that project directory.

Scenario 2: Separate Dev and Production Environments

Switch via environment variables at launch:

# Development: use a more economical model
ANTHROPIC_API_KEY=sk-ant-dev-xxx ANTHROPIC_MODEL=claude-haiku-4-5 claude

# Production-related tasks: use a more capable model
ANTHROPIC_API_KEY=sk-ant-prod-xxx ANTHROPIC_MODEL=claude-sonnet-4-6 claude

You can wrap these in shell aliases for daily use.

Scenario 3: Individual Keys for Team Members

Team Configuration Recommendations:

  1. Each member uses their own API Key
  2. Put shared config in .claude/settings.json (committed to git) and personal keys in .claude/settings.local.json (ignored via .gitignore)
  3. Or distribute keys centrally with an apiKeyHelper script in the shared config
// .claude/settings.json (shared by the team, contains no secrets)
{
  "model": "claude-sonnet-4-6",
  "apiKeyHelper": "./scripts/get-team-api-key.sh"
}

Verify Configuration

After configuration, verify everything works:

# 1. Health check (diagnoses installation and auth status)
claude doctor

# 2. View current configuration
claude config list

# 3. Test a simple task
claude -p "say hello"

# 4. Inside a session, check login status and auth method
claude
> /status

Summary

In this chapter, we learned:

  • ✅ How to register Anthropic account and obtain API Key
  • ✅ Four methods to configure API Key
  • ✅ API pricing and cost estimation
  • ✅ API security best practices
  • ✅ Multiple API Key management strategies
  • ✅ Configuration verification methods

Important Reminders:

  • 🔐 Safeguard your API Key
  • 💰 Monitor costs, set budget alerts
  • 🔄 Rotate API Keys regularly
  • 📊 Monitor usage

Next Step: Continue with Quick Start and complete your first task!