← Blog

How to Generate a Claude API Key (Step by Step)

2026-09-01 · 5 min read · SubToAPI Team

Generating a Claude API key means creating a credential from Anthropic's developer console that lets your code call Claude's models over HTTPS, separate from any Claude.ai subscription you might have. It takes about five minutes once you know where to click, and the process is the same whether you're building a script, a backend service, or a full product.

This guide walks through the exact steps to generate a working key, how to test it immediately, and what to do if you'd rather not manage a separate billing account just to get programmatic access.

Step 1: Create an Anthropic Console account

Go to the Anthropic Console (console.anthropic.com) and sign up with an email address or an existing Google account. This account is distinct from a personal Claude.ai login — even if you already use Claude.ai daily, you'll need a separate console account for API access. Verify your email if prompted, since key generation is blocked until verification completes.

Step 2: Add a payment method

The Claude API is pay-as-you-go, billed per token. Before you can generate a key that actually works for requests, you need to add a credit card under Settings > Billing and typically load a small amount of prepaid credit (Anthropic has changed the exact minimums over time, so check the current figure in your console). Without billing configured, you can still create a key, but calls will fail with a billing or permission error.

Step 3: Generate the API key

Once billing is set up:

  1. Navigate to Settings > API Keys in the console.
  2. Click Create Key.
  3. Give it a descriptive name — something like prod-backend or local-dev — so you can tell it apart later if you generate more.
  4. Copy the key immediately. It's shown only once; if you lose it, you'll need to generate a new one, since the console won't display the full value again.

The key looks like a long string starting with sk-ant-. Store it somewhere safe — a password manager or a secrets vault, not a text file in your repo.

Step 4: Store the key as an environment variable

Never hardcode the key in source files. On most systems:

export ANTHROPIC_API_KEY="sk-ant-your-key-here"

Add that line to your shell profile, or set it in your deployment platform's environment variable settings (Vercel, Railway, Docker secrets, etc.). If you're using a .env file locally, make sure .env is in .gitignore before you commit anything.

Step 5: Test the key with a simple request

A quick way to confirm the key works is a direct curl call:

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-4-5",
    "max_tokens": 100,
    "messages": [{"role": "user", "content": "Say hello in one sentence."}]
  }'

A successful response returns a JSON object with the model's reply and token usage counts. If you get a 401, double-check the key was copied without extra whitespace. If you get a billing error, go back and confirm your prepaid credit or card is active.

Common mistakes when generating a key

A few things trip people up consistently:

Rotating and revoking keys

If a key is exposed or a team member leaves, go back to Settings > API Keys, find the key, and revoke it. Any request using that key will immediately start failing. Generate a replacement, update your environment variables, and redeploy — there's no way to "recover" a revoked key, only replace it.

If you don't want a separate Anthropic billing account

Some teams already pay for Claude through a Pro or Team plan and don't want to open a second, separate pay-as-you-go account just to get programmatic access — especially for small internal tools, prototypes, or side projects where setting up usage-based billing feels like overkill.

SubToAPI exists for that case. It turns your existing Claude access into a standard HTTPS API: you get an application key (sub_live_...) instead of managing raw Anthropic credentials, with streaming, tool use, and per-key usage metadata built in. Generating a key takes a couple of minutes:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 100,
    "messages": [{"role": "user", "content": "Say hello in one sentence."}]
  }'

Plans start at €9/month for solo use, with Team (€19/seat) and Scale (€49/seat) tiers for shared dashboards and multiple keys per project. There's a free trial at /signup, plan details at /pricing, and setup instructions at /docs/quickstart if you want to compare the two approaches before committing to one.

Whichever route you pick, the underlying pattern is the same: generate a key, store it as an environment variable, test it with a minimal request, and rotate it whenever something feels off.

Questions

Do I need a credit card to generate a Claude API key? You can create the key itself without one, but Anthropic requires a payment method or prepaid credit before requests will succeed, so add billing early to avoid confusion later.

Can I use my Claude.ai subscription as an API key? No. Claude.ai login credentials and the API are separate systems with separate billing. You need a console account and a generated key, or a service like SubToAPI that bridges the two.

What should I do if I lose my API key after generating it? Anthropic only shows the full key once at creation time. If you lose it, generate a new key and revoke the old one — there's no way to retrieve the original value again.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →