Where your Anthropic API key lives and what it can spend

Create an Anthropic API key for your app, keep it server-side, test it properly, and set workspace and spend limits before real users arrive.

Simon, Founder of Shipyard

By · Founder of Shipyard

6 minute read

The key, the variable and the limit

To add Claude to an app you create an API key in the Claude Console at platform.claude.com/settings/keys, store it on your server as ANTHROPIC_API_KEY, and set a monthly spend limit before real users arrive. The old address console.anthropic.com redirects to the same place. The key must never appear in code that runs in a browser.

Billing is separate from the chat apps. API usage is paid from prepaid credits bought in the Console, and when the credits run out the API stops answering until you add more. One exception is recent: Anthropic's help centre says the Max, Ultra and Team plans include monthly API credits, which you claim by linking a Console organization to the plan. The article does not list the Pro plan among them.

A key can pass a check and still fail in your app

The cheapest way to test a key is to list the available models. It costs nothing and returns the current model IDs, which is also the right place to copy a model name from, since names change with each release.

A 200 here proves the key is valid. It does not prove the account can pay for a message. An organization with no credit passes this check and then fails on the first real request, which is exactly the situation a health check built on this endpoint will miss. After adding credit, send one real message before you call the integration done.

Raw HTTP calls need the anthropic-version header. The current value is 2026-10-21; the versions page also lists the older 2023-06-01. The official SDKs add it, along with authentication, on their own. The reference now presents Authorization: Bearer as the primary way to send the key and describes x-api-key as a legacy form that remains supported, so older examples keep working.

Terminal
curl "https://api.anthropic.com/v1/models?limit=1" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21"

Calling the model from a server route

Install the SDK with npm install @anthropic-ai/sdk. A client created with no arguments reads ANTHROPIC_API_KEY from the environment, so the key never has to be written in code. That is enough for a key scoped to a single workspace. Anthropic's reference says a key that is not scoped to a workspace must also send an anthropic-workspace-id header with each request, and answers 400 without it, so scope the key to one workspace when you create it. In Next.js the call belongs in a route handler or a server action. Because the variable name has no NEXT_PUBLIC_ prefix, the framework keeps it out of the browser bundle.

The example reads the model ID from a second variable, ANTHROPIC_MODEL, so that changing models is a configuration change, not a deploy of new code. Nothing sets that variable for you, and Shipyard does not write it: add it on your host yourself, with an ID copied from the models list above. If it is missing, the request goes out without a model and fails.

TypeScript
// app/api/ask/route.ts
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic() // reads ANTHROPIC_API_KEY

export async function POST(req: Request) {
  // Check the session and a per-user rate limit here, before spending tokens.
  const { question } = await req.json()

  const message = await client.messages.create({
    model: process.env.ANTHROPIC_MODEL!, // set this on your host to an ID from GET /v1/models
    max_tokens: 1000,
    messages: [{ role: 'user', content: question }],
  })

  const text = message.content.flatMap((b) => (b.type === 'text' ? [b.text] : [])).join('')
  return Response.json({ text })
}

Moving the key to the server is half the job

A key in browser code is public: anyone can read it in the network tab and spend your credits. But an unprotected server route is the same open tap with one extra hop. If /api/ask answers anyone who posts to it, a stranger with a loop can drain the balance without ever seeing the key.

Three measures close it. Require a signed-in user. Count requests per user and per IP address and refuse above a threshold. And set max_tokens deliberately, since it bounds the cost of a single answer. Anthropic's own guidance adds the housekeeping: keep .env files out of version control, use different keys for development and production, turn on secret scanning in your repository, and rotate keys on a schedule.

Workspaces: a fence around each product

A workspace is a compartment inside your Console organization. Keys can be scoped to one workspace, and each workspace can carry its own monthly spend limit and its own rate limits. One workspace per product means a runaway loop in a side project cannot spend the budget of the product that earns money.

Workspace limits can only sit below the organization's limits, never above them. Every organization also has a Default Workspace that cannot be renamed or archived, and Anthropic notes that a spend limit placed on it may not stop some usage there, Console playground sessions among them. That is one more reason to give a product its own workspace instead of leaving its key in the default one.

Archiving is the emergency brake. Archiving a workspace archives every key created for it within seconds, and it cannot be undone.

Two kinds of spend limit, two different errors

There are two ceilings on spending and they fail differently, which matters for what your app shows the user and whether a retry can ever succeed. The first is a limit you set yourself under Spend limits in the Console, for the organization or for a workspace. The second is a monthly cap that belongs to your usage tier and that you cannot raise without moving up a tier.

Status and typeWhat happenedWill a retry help?
400 invalid_request_error, message starting You have reached your specified API usage limitsUsage reached a spend limit you setNo. Raise or remove the limit in the Console
429 rate_limit_error with error.details.error_code set to enforced_spend_limit_reachedThe organization hit its tier's monthly spend capNo. There is no retry-after header and access returns at 00:00 UTC on the first of the next month, or when you move to a higher tier
429 rate_limit_error with a retry-after headerA per-minute rate limit was exceededYes, after the number of seconds in the header
529 overloaded_errorThe API is temporarily overloaded across all usersYes, with a growing delay
401 authentication_errorThe key is malformed, revoked or expiredNo. Replace the key
402 billing_errorA problem with billing or payment informationNo. Fix it in the Console

Rate limits, briefly

Limits apply to the whole organization and are set by usage tier. They are measured three ways per model: requests per minute, input tokens per minute and output tokens per minute. Anthropic says new organizations may begin in an Evaluation tier with limits below the published standard ones, rising automatically as usage history builds. Your current numbers are on the Rate limits page of the Console.

Two properties are useful to know when you design around them. Capacity refills continuously, a token bucket, so a limit of 60 requests a minute can be enforced as one a second and a short burst can trip it. And tokens read from the prompt cache do not count toward the input-token limit, so caching a long system prompt raises your real throughput as well as lowering cost.

The official SDK already retries rate limits, connection errors and 5xx responses twice with exponential backoff and honours retry-after. Do not wrap it in a second retry loop. Do catch the two spend-limit cases from the table and show a plain message, because retrying those only delays the bad news.

Shipyard and this key

Shipyard never sends a prompt to Anthropic. When you add the key it makes the one free call shown earlier, GET /v1/models?limit=1, to confirm the key is accepted, and stores it in the keychain on your computer. During a launch it writes the key to your host as ANTHROPIC_API_KEY, and the build prompt it generates tells your coding agent to call the API from server code only and to look up current model names instead of guessing them.

Be aware of one design choice: the key you store is written into every project you launch. If you want the per-product isolation described above, create a workspace key for the product and replace the variable on that host. Creating the Console account, buying credit and setting spend limits are yours to do, and because the check uses the models endpoint, Shipyard cannot tell you that the account has no credit.

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

See how it works

Read next