← Blog

Claude API Authentication: API Key Setup Guide

2026-11-15 · 5 min read · SubToAPI Team

Setting up Claude API authentication comes down to three things: getting a valid API key, sending it correctly in your request headers, and storing it somewhere that doesn't leak it. This guide walks through each step, shows the exact header format Claude expects, and covers the mistakes that cause most "401 Unauthorized" errors.

If you just want the fastest path to a working request, skip to the "Minimal working example" section below. If you're troubleshooting an existing setup, jump to the FAQ.

How Claude API authentication works

Claude's API uses a simple bearer-style key authentication model. Every request needs an API key sent in a header. There's no OAuth flow, no signed requests, no token refresh cycle to manage for basic usage — just a static key string that identifies your account and tracks usage against your plan.

The key gets passed in one of two ways depending on which API surface you're calling:

If you're building against SubToAPI's endpoint instead of calling Claude directly, you'll always use the Authorization: Bearer format, which matches what most HTTP clients, SDKs, and API testing tools expect by default.

Getting your API key

  1. Create an account with whichever provider you're using.
  2. Navigate to the API keys or credentials section of the dashboard.
  3. Generate a new key. It will typically be shown once — copy it immediately.
  4. Store it in an environment variable or secrets manager, never in source code.

If you're setting this up through SubToAPI, the process is identical in shape: sign up at /signup, and the dashboard issues an application key in the format sub_live_.... This key authenticates all your requests to https://api.subtoapi.app/v1/... and is scoped to your account's usage, rate limits, and team seat permissions.

Minimal working example

Here's a bare-bones authenticated request using curl:

export SUBTOAPI_KEY="sub_live_your_key_here"

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

The same pattern in JavaScript with fetch:

const response = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "claude-sonnet-4",
    max_tokens: 256,
    messages: [{ role: "user", content: "Say hello in one sentence." }]
  })
});

const data = await response.json();
console.log(data);

Both examples read the key from an environment variable instead of hardcoding it. This is the single most important habit in API key setup — it keeps the key out of your git history, your logs, and your client-side bundles.

For a full breakdown of request and response fields, see the messages API reference. If you're setting this up for the first time, the quickstart guide walks through the same steps with more context on request parameters.

Where to store the key safely

Common authentication errors and fixes

401 Unauthorized — the key is missing, malformed, or revoked. Check that the header name matches what the API expects (Authorization: Bearer vs x-api-key) and that there's no extra whitespace or quote character accidentally included in the key string.

403 Forbidden — the key is valid but doesn't have permission for the requested resource, often because it belongs to a different team or project scope than the one you're trying to access.

429 Too Many Requests — this isn't an authentication error, but it's frequently mistaken for one. It means your key is working but you've hit a rate or usage limit tied to your plan.

A quick sanity check: print the first and last four characters of your key variable in a test script to confirm it's actually being loaded, without exposing the whole secret in your terminal history.

Rotating and revoking keys

Treat API keys like passwords. If a key is ever exposed — committed to a public repo, logged in plaintext, or shared over chat — revoke it immediately from your dashboard and generate a replacement. Update every environment that references the old key (local, staging, production, CI) before the old one expires, to avoid a gap in service.

For teams, it's worth generating separate keys per environment (development, staging, production) rather than reusing one key everywhere. This makes it much easier to isolate which environment caused unusual usage spikes or to revoke a single environment's access without taking down the others. SubToAPI's pricing plans include multiple seats precisely so each team member or environment can have its own scoped key rather than sharing credentials.

Testing your setup end to end

Once your key is stored and your first request works, test the streaming path too, since authentication headers behave the same way but the response handling differs — see the streaming docs for the event format. If your application calls tools or functions, check the tools guide for how authentication interacts with tool-use requests, since the header setup is identical but the payload structure changes.

questions

Do I need a different API key for streaming vs. non-streaming requests? No. The same key authenticates both. Only the request payload (and how you parse the response) changes between streaming and standard calls.

Can I use the same API key across multiple apps? You can technically, but it's not recommended. Separate keys per app or environment make it easier to track usage, rotate credentials without downtime, and isolate problems if one key gets compromised.

What's the difference between x-api-key and Authorization: Bearer? They're two different header conventions for passing the same kind of secret. Anthropic's native API uses x-api-key; OpenAI-compatible wrappers and gateways like SubToAPI use Authorization: Bearer, which is more widely supported by standard HTTP clients and SDKs.

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 →