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
- Visit Anthropic Console
- Click "Sign Up"
- Register using Email or Google account
- Verify email address
Step 2: Add Account Balance
- After login, go to "Billing" page
- Click "Add Credit"
- Select amount (recommended $20-$50 for first time)
- Complete payment with credit card
Step 3: Create API Key
- Go to "API Keys" page
- Click "Create Key"
- Set key name (e.g., claude-code-dev)
- (Optional) Set usage limits and permissions
- Click "Create"
- Important: Copy and save API Key immediately (shown only once!)
API Key Format:
Configuring API Key
There are multiple ways to configure your API Key—choose what works best:
Method 1: Interactive Login (Recommended)
Method 2: Environment Variable
Temporary (Current Session):
Permanent:
Linux/macOS (Bash):
Linux/macOS (Fish):
Windows (PowerShell):
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):
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:
API Pricing and Usage
Model Pricing (January 2026)
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
Cost Control Tips
-
Choose Right Model
- Use Haiku for simple tasks
- Use Sonnet for general tasks
- Use Opus only for complex tasks
-
Optimize Prompts
- Clear, concise task descriptions
- Reduce unnecessary context
-
Use Subagents
- Use Explore Agent (haiku) for exploration
- Use Plan Agent for planning
-
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
2. Use Environment Variables
3. Limit Key Permissions
- Set key permissions in Anthropic Console
- Create separate keys for different environments
- Rotate API Keys regularly
4. Monitor Usage
5. Secure Storage
⚠️ Response to Leakage
If API Key is leaked:
-
Revoke Key Immediately
- Log into Anthropic Console
- Delete leaked key on API Keys page
-
Check Usage Records
- Look for abnormal calls
- Check billing
-
Create New Key
- Generate new API Key
- Update all configurations
-
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:
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:
You can wrap these in shell aliases for daily use.
Scenario 3: Individual Keys for Team Members
Team Configuration Recommendations:
- Each member uses their own API Key
- Put shared config in
.claude/settings.json(committed to git) and personal keys in.claude/settings.local.json(ignored via .gitignore) - Or distribute keys centrally with an
apiKeyHelperscript in the shared config
Verify Configuration
After configuration, verify everything works:
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!