← Blog

Anthropic API Authentication Token Setup Guide

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

Setting up Anthropic API authentication means getting an API key from the Anthropic Console, storing it securely as an environment variable, and passing it in the x-api-key header on every request along with an anthropic-version header. That's the entire mechanism — there's no OAuth flow, no token refresh, no expiring sessions to manage. This guide walks through the setup end to end and covers the mistakes that cause most "invalid authentication" errors.

If you're building something that needs to hand that access to other people or apps — teammates, a mobile client, a browser extension — the raw API key model gets awkward fast, and we'll cover a cleaner option for that at the end.

Step 1: Generate an API Key

  1. Sign in to the Anthropic Console.
  2. Go to the API Keys section under your organization settings.
  3. Click "Create Key" and give it a descriptive name (e.g. prod-backend, staging-worker).
  4. Copy the key immediately — Anthropic shows it once and doesn't store it in plaintext, so you can't retrieve it later.

Keys look like sk-ant-api03-.... Each key is tied to a workspace and billing account, so treat it like a password: it grants full access to whatever usage limits are set on that account.

Step 2: Store the Key as an Environment Variable

Never hardcode the key in source files. Set it as an environment variable locally and in your deployment platform's secrets manager:

export ANTHROPIC_API_KEY="sk-ant-api03-xxxxxxxxxxxxxxxxxxxx"

For local development, put it in a .env file that's excluded from version control:

# .env
ANTHROPIC_API_KEY=sk-ant-api03-xxxxxxxxxxxxxxxxxxxx

Add .env to .gitignore before your first commit, not after. Leaked keys in public repos get scraped and abused within minutes.

Step 3: Authenticate Your Requests

Every request to the Messages API needs three headers: x-api-key, anthropic-version, and content-type.

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-20250514",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Confirm this authentication is working."}
    ]
  }'

A common mistake is using Authorization: Bearer instead of x-api-key — that's the pattern most other LLM APIs use, but Anthropic's Messages API expects the key in x-api-key. Mixing up the two is the single most frequent cause of 401 authentication_error responses.

In JavaScript, the same setup with the official SDK:

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

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY, // reads x-api-key internally
});

const message = await client.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Confirm this authentication is working." }],
});

console.log(message.content);

The SDK handles header formatting for you, so if you're getting auth errors with it, the problem is almost always that ANTHROPIC_API_KEY isn't set in the process environment the SDK is running in — check for typos in the variable name and make sure your deployment platform actually injects it at runtime, not just at build time.

Step 4: Pin the anthropic-version Header

The anthropic-version header isn't optional and isn't cosmetic — it locks your requests to a specific API contract. Omitting it or using a stale value can cause requests to fail or behave differently after Anthropic ships changes. Set it once as a constant in your codebase and update it deliberately when you're ready to test against a new version, not accidentally.

Step 5: Scope and Rotate Keys

Since Anthropic API keys don't expire automatically, rotation is a manual discipline:

When a Raw API Key Isn't Enough

The x-api-key model works well for a single backend service calling Anthropic directly. It gets harder to manage once you need to:

This is what SubToAPI is built for. It sits on top of your existing Claude access and issues its own application keys (sub_live_...) that you can hand out per app or per teammate, each with its own usage metadata, without exposing your underlying account credentials. Setup follows the same pattern described above — generate a key, set it as an environment variable, send it as a bearer token:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello"}]
  }'

If you want to compare this against a direct Anthropic setup, the quickstart walks through key generation and your first request, and pricing covers the Solo, Team, and Scale plans if you need multiple seats. You can also start with a free trial at signup before deciding which model fits your setup.

Questions

Do Anthropic API keys expire? No. Keys remain valid until you manually revoke them in the Console. There's no automatic expiration or refresh token flow to manage — rotation is something you have to do deliberately.

Why am I getting a 401 error even though my key looks correct? The most common causes are sending the key as Authorization: Bearer instead of x-api-key, a missing or outdated anthropic-version header, or an environment variable that's set locally but not injected into your deployment environment at runtime.

Can I use one Anthropic API key across a whole team? Technically yes, but it's not recommended — you lose per-user visibility and a single leak compromises everyone. Creating separate keys per person or service, or using a layer like SubToAPI that issues scoped application keys with individual usage tracking, is safer at team scale.

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 →