DEV Community

Cover image for How to Get Your Anthropic Claude API Key
Serhii Kalyna
Serhii Kalyna

Posted on • Originally published at kalyna.pro

How to Get Your Anthropic Claude API Key

To use Claude in your applications, you need an Anthropic API key. This guide shows you exactly how to get one, how to set it up securely, and what you need to know about rate limits and billing.

This article was originally published at kalyna.pro

Step 1: Create an Anthropic Account

Go to console.anthropic.com and click Sign Up. You can register with Google or an email address. Email verification is required.

Once logged in, you land on the Anthropic Console — the dashboard where you manage keys, usage, and billing.

Step 2: Get Your API Key

In the Console, navigate to Settings → API Keys (or click your avatar → API Keys). Then:

  1. Click Create Key
  2. Give it a descriptive name (e.g., my-app-dev)
  3. Click Create Key — copy it immediately
  4. Store it somewhere safe — you won't see the full key again

Your key looks like: sk-ant-api03-...

Step 3: Add a Payment Method

API access requires a credit card on file. Go to Settings → Billing and add your card. Usage is billed monthly — you're only charged for what you use.

Step 4: Set Your API Key Securely

Never paste your API key directly into source code. Use environment variables:

# Linux / macOS — add to ~/.bashrc or ~/.zshrc
export ANTHROPIC_API_KEY="sk-ant-api03-..."

# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-api03-..."
Enter fullscreen mode Exit fullscreen mode

For projects, use a .env file (and add it to .gitignore):

# .env
ANTHROPIC_API_KEY=sk-ant-api03-...
Enter fullscreen mode Exit fullscreen mode
# .gitignore
.env
*.env
Enter fullscreen mode Exit fullscreen mode

Step 5: Test Your Key

Python:

import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY automatically

message = client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=64,
    messages=[{"role": "user", "content": "Say hello!"}],
)
print(message.content[0].text)
Enter fullscreen mode Exit fullscreen mode

Node.js:

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const message = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 64,
  messages: [{ role: "user", content: "Say hello!" }],
});

console.log(message.content[0].text);
Enter fullscreen mode Exit fullscreen mode

curl:

curl https://api.anthropic.com/v1/messages \
  --header "x-api-key: $ANTHROPIC_API_KEY" \
  --header "anthropic-version: 2023-06-01" \
  --header "content-type: application/json" \
  --data '{
    "model": "claude-haiku-4-5",
    "max_tokens": 64,
    "messages": [{"role": "user", "content": "Say hello!"}]
  }'
Enter fullscreen mode Exit fullscreen mode

Understanding Rate Limits

Tier RPM TPM TPD
Tier 1 ($5+ spent) 50 40,000 1,000,000
Tier 2 ($100+/30d) 1,000 80,000 2,500,000
Tier 3 ($500+/30d) 2,000 160,000

When rate-limited (HTTP 429), use exponential backoff:

import time
import anthropic

client = anthropic.Anthropic()

def call_with_retry(prompt, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.messages.create(
                model="claude-haiku-4-5",
                max_tokens=512,
                messages=[{"role": "user", "content": prompt}],
            )
        except anthropic.RateLimitError:
            if attempt == max_retries - 1:
                raise
            wait = 2 ** attempt
            print(f"Rate limited. Waiting {wait}s...")
            time.sleep(wait)
Enter fullscreen mode Exit fullscreen mode

Multiple Keys Best Practices

Create separate keys per environment:

  • Development my-app-dev — local testing
  • Staging my-app-staging — pre-production
  • Production my-app-prod — live traffic
  • CI/CD my-app-ci — automated tests

Revoke a compromised key without affecting others: API Keys → three dots → Delete.

Pricing Overview

Model Input (per 1M tokens) Output (per 1M tokens)
claude-haiku-4-5 $0.80 $4.00
claude-sonnet-4-5 $3.00 $15.00
claude-opus-4-5 $15.00 $75.00

Summary

  • Sign up at console.anthropic.com and create a key under Settings → API Keys
  • Store the key in an environment variable, never in code
  • Add a payment method — billing is pay-as-you-go
  • Test with claude-haiku-4-5 before switching to Sonnet or Opus
  • Create separate keys per environment; revoke immediately if leaked
  • Monitor spending via the Console and set billing alerts

Top comments (0)