Claude API Key: How to Get One and Keep It Safe

How to get a Claude API key in the Claude Console: create it, pick a key type and expiry, set ANTHROPIC_API_KEY, use it in Claude Code and revoke it.

Josh Pocock7 min read

Claude API key cover

AI Coding

Claude API key

Stride AI Academy

You get a Claude API key from the Claude Console at platform.claude.com. Sign in, open the API keys page, click Create key, give it a name and an expiration, then copy the key. It starts with sk-ant- and the Console shows it only once, so store it somewhere safe straight away.

This guide walks through each step, the three key types, setting the ANTHROPIC_API_KEY environment variable on Mac, Linux and Windows, using a key in Claude Code, and how to rotate or revoke a key that leaks. Everything here was checked against Anthropic's docs on getting an API key, authentication, CLI authentication, pricing, Claude Code authentication and the Help Center article on why the API is billed separately on November 22, 2026.

How to get a Claude API key

From the get-an-API-key docs:

  1. Sign in to the Claude Console. Go to platform.claude.com and sign in, or create an account.
  2. Open the API keys page at platform.claude.com/settings/keys.
  3. Create a key. Click Create key, name it, choose an expiration and set Linked account to yourself or a service account. You can also pick a workspace to scope the key to.
  4. Copy and store it. The full key appears once, at creation. If you lose it, you can't view it again. Create a new one instead.

If the Create key button is greyed out, your role in the organization doesn't allow it. Ask an admin to change your role or to create a service account key for you.

The Claude API console and billing

The Claude Console is a separate product from your Claude chat plan. The Help Center says a paid Claude subscription covers the chat experience, and "the Claude API is billed separately." So a Pro or Max plan doesn't give you an API key with usage included.

To add funds, go to the Console's Billing page. The pricing docs say major credit cards are accepted for standard accounts, and billing is based on actual monthly usage. For what each model costs per token, see our Claude API pricing guide.

Max, Ultra and Team plans do include monthly API credits, which you claim by linking a Console organization. Our Claude credits guide covers how that works.

Is there a free Claude API key?

Not an unlimited one. The pricing docs say "New users receive free credits to test the API." Once those run out, you pay per token. New organizations can also start on an Evaluation tier with lower rate limits until they build usage history, per the rate limits docs. Our guide to Claude API rate limits explains the tiers.

Which Claude API key type to choose

When you create a key, you choose its type. From the authentication docs:

Key type Acts as Stops working when
Personal key You, with your roles and permissions You lose access to the organization
Service account key A service account The service account is archived
Workspace key (legacy) No one. It belongs to the workspace It expires, is disabled or deleted, or its workspace is archived

Use a personal key for your own development and scripts. Use a service account key for anything shared, such as a CI pipeline or a production service. The docs call workspace keys legacy and recommend the other two, because identity-backed keys stop working on their own when the person or workload leaves.

Workspaces

A key scoped to one workspace only works there, and requests can skip the workspace ID. A key that isn't scoped to a workspace must send the anthropic-workspace-id header on every request, or the API returns a 400 error. You'll find each workspace's ID on the Workspaces page.

Expiration

You set an expiration when you create the key: 3 hours, 1 day, 7 days, 30 days, a custom length, or Never. You can't change it later. Anthropic emails the key's creator 7 days before expiry for keys with a lifetime of at least 14 days, and 1 day before for keys of at least 7 days. After a key expires, requests return a 401 authentication_error and you need a new key.

Set ANTHROPIC_API_KEY as an environment variable

The client SDKs read ANTHROPIC_API_KEY automatically, so you never paste the key into your code.

For the current terminal on macOS or Linux:

Terminal
export ANTHROPIC_API_KEY="sk-ant-api03-..."

To keep it across sessions in zsh (the CLI authentication docs show the same for bash with ~/.bashrc):

Terminal
echo 'export ANTHROPIC_API_KEY=sk-ant-api03-...' >> ~/.zshrc
source ~/.zshrc

On Windows, this saves it for future terminals. Open a new terminal afterwards:

PowerShell
setx ANTHROPIC_API_KEY "sk-ant-api03-..."

Then make a test request. This cURL call comes from the authentication docs:

Terminal
curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2026-10-21" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello, Claude"}]
  }'

The docs say direct HTTP requests can send the key as Authorization: Bearer <key>, with x-api-key still accepted as the legacy header. For the full first-call walkthrough in Python and TypeScript, see our Claude API tutorial.

Using a Claude Code API key

Claude Code can run on a subscription login or on an API key, and they bill differently.

  • Subscription login. Run claude, and it opens a browser to sign in with your Pro, Max, Ultra, Team or Enterprise account. Usage counts against your plan's limits.
  • API key. Set ANTHROPIC_API_KEY before you start Claude Code. In interactive mode, Claude Code asks once whether to use the key and remembers your answer. You can change that later with the "Use custom API key" toggle in /config. In non-interactive mode (claude -p), the key is always used when it's present. Usage bills to your Console organization at API rates.

The trap is having both. The Claude Code docs say that if you have a subscription but also have ANTHROPIC_API_KEY set, Claude Code uses the key once you approve it. To fall back to your plan:

Terminal
unset ANTHROPIC_API_KEY

Then run /status inside Claude Code to confirm which method is active. When a login and a key are both set up, /status marks the one that isn't in use.

You can also sign in at /login with a Claude Console account. Since v2.1.242, Claude Code offers a recommended option that signs in without creating an API key at all, alongside a legacy option that creates one for you.

For CI jobs on a subscription instead of a key, claude setup-token generates a one-year OAuth token you set as CLAUDE_CODE_OAUTH_TOKEN. If you want to compare what a key costs against a plan, try the Claude Code cost calculator.

How to keep your Claude API key safe

The authentication docs give these rules:

  • Store keys in a secrets manager, not in code or a shared doc.
  • Rotate them periodically.
  • Set an expiration so a leaked key stops working on its own.
  • Turn on IP allowlisting to limit where a leaked key works.
  • Never put a key in browser code. Anyone using a web page can read a key in its code. Call the API from your own back end.

Two habits of our own: add .env to your .gitignore before you create the file, and never share a terminal screenshot with the key visible.

How to rotate or revoke a key

On the API keys page you have two options:

  • Disable is reversible. Re-enable brings the key back.
  • Delete is permanent. The key is archived.

Expired keys can only be deleted. To rotate a key without downtime, the docs' replacement steps boil down to this: create the new key, put it wherever your app reads ANTHROPIC_API_KEY or your secrets manager entry, confirm requests succeed, then delete the old key. If you suspect a leak, disable the key first and investigate after.

Learn to build with Claude Code

The Stride AI Coding course teaches Claude Code and Codex for building client tools, written for non-programmers, with weekly live calls to review your builds. Many of the free guides cover Claude Code setups, and the Claude Code community is where members compare theirs.

FAQ

How do I get a Claude API key?

Sign in to the Claude Console at platform.claude.com, open the API keys page, click Create key, name it and choose an expiration. Copy the key straight away, because the Console only shows it once.

Is the Claude API key free?

New users get free credits to test the API, per Anthropic's pricing docs. After that, you pay per token through the Console's Billing page. A Claude chat subscription doesn't include API usage, though Max, Ultra and Team plans come with monthly API credits.

Can I use my Claude Pro subscription instead of an API key?

In Claude Code, yes. Log in with your claude.ai account and usage counts against your plan. For your own apps calling the API, no. The API is billed separately through the Claude Console.

Why is Claude Code charging my API key instead of my plan?

If ANTHROPIC_API_KEY is set and you approved it, Claude Code uses the key ahead of your subscription. Run unset ANTHROPIC_API_KEY, then check /status to confirm Claude Code is back on your plan.

What do I do if my Claude API key leaks?

Disable or delete it on the API keys page in the Console right away, then create a new key and swap it in. Disable is reversible and Delete is permanent. Setting expirations and IP allowlisting limits the damage next time.

Where do I find a lost Claude API key?

You can't. The Console shows the full key only at creation, and the Admin API only returns a redacted hint. Create a new key and replace the old one.

Build it with us in Stride AI Academy

Four courses, weekly live calls and a community of people building and selling AI. Or start with 320+ free guides.

Keep reading

Free AI Coding guides