Anthropic Claude API Key: Create, Secure, and Use
What you’ll build
Section titled “What you’ll build”By the end of this guide you will have an Anthropic API key stored in an environment variable, billing configured, and a working Python and cURL call that returns a Claude response. The same approach applies to Node.js and any other runtime that can read environment variables.
Prerequisites
Section titled “Prerequisites”| Requirement | Notes |
|---|---|
| Anthropic account | Sign up at console.anthropic.com |
| Verified email | Required before the console lets you create keys |
| Payment method | Credit card required for pay-as-you-go API access |
| Python 3.9+ or Node.js 18+ | To run the verification samples |
New accounts may have a brief review period. If you see a “not yet approved” message, check your inbox for an approval email.
Step 1 — Create the API key
Section titled “Step 1 — Create the API key”- Sign in at console.anthropic.com.
- Click API Keys in the left sidebar.
- Click Create Key, give it a descriptive name (for example
orders-portal-dev), and click Create. - Copy the full key immediately — it starts with
sk-ant-api03-and is shown only once. If you close the dialog without copying, delete the key and create a new one.
Store the key in a password manager or secrets vault before closing the dialog.
Step 2 — Add billing credits
Section titled “Step 2 — Add billing credits”The API will return 402 Insufficient credits on every request until you have a positive balance.
- Go to Settings › Billing in the console.
- Click Add Payment Method and enter your card.
- Click Add Credits and purchase a starting amount. The minimum varies by region.
- Optionally, go to Settings › Limits and set a monthly spending cap so a runaway loop cannot exhaust your balance overnight.
Step 3 — Store the key securely
Section titled “Step 3 — Store the key securely”Never hardcode the key in source files or commit it to version control. Use an environment variable instead.
Linux / macOS (current shell session):
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"Windows PowerShell (current session):
$env:ANTHROPIC_API_KEY = "sk-ant-api03-your-key-here".env file for local development (add .env to .gitignore):
ANTHROPIC_API_KEY=sk-ant-api03-your-key-hereLoad it in Python with python-dotenv:
from dotenv import load_dotenvload_dotenv()For production, use your platform’s secret store: AWS Secrets Manager, Azure Key Vault, GitHub Actions Secrets, or equivalent.
Step 4 — Verify the key works
Section titled “Step 4 — Verify the key works”Install the SDK, then run a minimal request to confirm everything is wired correctly.
pip install anthropicSave as check_key.py and run with python check_key.py (Python 3.9+, any platform):
import anthropicimport os
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.messages.create( model="claude-sonnet-5-5", max_tokens=64, messages=[{"role": "user", "content": "Reply with the word VERIFIED."}],)
print(response.content[0].text)# Expected output: VERIFIEDOr use cURL directly:
curl https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "anthropic-version: 2023-06-01" \ -H "content-type: application/json" \ -d '{ "model": "claude-sonnet-5-5", "max_tokens": 64, "messages": [{"role": "user", "content": "Reply with the word VERIFIED."}] }'A successful response returns HTTP 200 with a JSON body containing "type": "message" and your text in content[0].text.
Available models
Section titled “Available models”| Model | Input / output (per MTok) | Best for |
|---|---|---|
claude-fable-5-2 |
$8 / $40 | The hardest reasoning and agentic tasks |
claude-opus-5-5 |
$4 / $20 | Long-running agentic coding and knowledge work |
claude-sonnet-5-5 |
$2 / $10 | General-purpose applications |
claude-haiku-5-5 |
from $0.10 / $0.50 | Classification, extraction, routing |
Pricing and model IDs change with each release. Always check the Anthropic models documentation and the Anthropic Models guide for the current list before hardcoding a model string.
Managing keys
Section titled “Managing keys”View keys: Console › API Keys shows the name, a masked preview, creation date, and last-used timestamp.
Revoke a key: Click the trash icon next to the key and confirm. Access using that key stops immediately. Any application relying on it will start receiving 401 Unauthorized responses.
Rotation policy: Create a replacement key first, roll it out to your applications, then delete the old one. This avoids downtime.
Troubleshooting
Section titled “Troubleshooting”| Error | Likely cause | Fix |
|---|---|---|
401 Unauthorized |
Key missing, wrong header name, or key revoked | Confirm the header is x-api-key, not Authorization. Re-check the key value. |
402 Insufficient credits |
Zero balance | Add credits in Console › Billing. |
429 Too many requests |
Rate limit exceeded | Add exponential backoff. See rate limits documentation. |
400 Bad request |
Missing required field | Check that model, max_tokens, and messages are all present. |
Next steps
Section titled “Next steps”- Anthropic Models guide — model tiers, context windows, and when to use each
- Claude Code agent — running Claude autonomously in your terminal
- API Tokens overview — comparison of all supported providers
Official references
Section titled “Official references”- Anthropic Console — key management and billing
- Anthropic API documentation — versioned REST reference
- Claude models list — current model IDs and pricing
- Python SDK —
anthropicpackage on PyPI