API Key Setup
What you'll learn in this chapter: How to get an API key, configure it securely, and choose the right pricing plan
You should complete the Installation Guide first.
What Is an API Key?
An API key is an authentication credential for accessing the Claude AI service. Claude Code uses it to communicate with Anthropic's servers.
Never upload your API key to a public repository (e.g., GitHub). If it leaks, unauthorized charges may be incurred.
How to Get an API Key
Step 1: Go to the Anthropic Console
Visit console.anthropic.com and sign up or log in.
Step 2: Navigate to the API Keys Menu
In the left sidebar, click API Keys.
Step 3: Create a New API Key
Click Create Key, enter a name (e.g., claude-code-personal), and click Create Key.
Step 4: Copy the API Key
Copy the generated key (sk-ant-api03-...). You won't be able to see it again after leaving this page, so store it somewhere safe.
How to Configure the API Key
Method 1: Interactive Setup in Claude Code (Recommended)
An API key prompt appears on first launch.
claude
# Paste your key and press Enter
Method 2: Set as an Environment Variable
export ANTHROPIC_API_KEY="sk-ant-api03-..."
claude
Method 3: Save Permanently in Your Shell Profile
macOS (zsh, the default shell):
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-..."' >> ~/.zshrc
source ~/.zshrc
macOS/Linux (bash):
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-..."' >> ~/.bashrc
source ~/.bashrc
Windows PowerShell:
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "sk-ant-api03-...", "User")
Understanding Pricing
Claude Code uses the Anthropic API, so costs are incurred based on usage.
Pricing Tiers (as of 2026)
| Model | Price | Characteristics |
|---|---|---|
| Claude Haiku | Low | Fast, suited for simple tasks |
| Claude Sonnet | Medium | Balanced, default model |
| Claude Opus | High | Best performance |
For exact pricing, check the Anthropic pricing page.
Typical Usage Examples
For a typical 30-minute development session:
- Small bug fix: $0.01 -- $0.05
- New feature implementation: $0.10 -- $0.50
- Large-scale refactoring: $0.50 -- $2.00
Cost Management Tips
- Set a spending limit: Console -> Settings -> Limits to set a monthly cap
- Use the Haiku model: For simple tasks, switch to Haiku (
/model claude-haiku-4-5-20251001) - Leverage caching: Prompt caching can save up to 90% on repeated context
Getting Started for Free
Claude.ai Subscribers (Max Plan)
Claude.ai Max subscribers can use Claude Code at no additional cost.
claude
# Select the "Login with Claude.ai" option
API Credits
You can register a card on the Anthropic Console and start with a minimum of $5 in credits.
Security Best Practices
Make sure to add these to your .gitignore:
.env
*.env
Never do the following:
- Hardcode API keys in your source code
- Upload keys to public repositories on GitHub, GitLab, etc.
If your key is compromised:
- Immediately go to Console -> API Keys -> Deactivate the key
- Generate a new key
- Replace the key in all environments
Key Takeaways
- Get your API key from console.anthropic.com
- Setting it via the
ANTHROPIC_API_KEYenvironment variable is the safest approach - Costs are usage-based -- setting a monthly limit is recommended
- Claude.ai Max plan subscribers pay no additional cost
Next Step
IDE Integration -- Learn how to use Claude Code more conveniently with VS Code and JetBrains