Skip to content

Anthropic Claude API Key: Create, Secure, and Use

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.

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.

  1. Sign in at console.anthropic.com.
  2. Click API Keys in the left sidebar.
  3. Click Create Key, give it a descriptive name (for example orders-portal-dev), and click Create.
  4. 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.

The API will return 402 Insufficient credits on every request until you have a positive balance.

  1. Go to Settings › Billing in the console.
  2. Click Add Payment Method and enter your card.
  3. Click Add Credits and purchase a starting amount. The minimum varies by region.
  4. Optionally, go to Settings › Limits and set a monthly spending cap so a runaway loop cannot exhaust your balance overnight.

Never hardcode the key in source files or commit it to version control. Use an environment variable instead.

Linux / macOS (current shell session):

Terminal window
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"

Windows PowerShell (current session):

Terminal window
$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-here

Load it in Python with python-dotenv:

from dotenv import load_dotenv
load_dotenv()

For production, use your platform’s secret store: AWS Secrets Manager, Azure Key Vault, GitHub Actions Secrets, or equivalent.

Install the SDK, then run a minimal request to confirm everything is wired correctly.

Terminal window
pip install anthropic

Save as check_key.py and run with python check_key.py (Python 3.9+, any platform):

import anthropic
import 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: VERIFIED

Or use cURL directly:

Terminal window
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.

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.

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.

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.