← All articles

Claude Code API Key: How to Get One and Configure Billing

Learn how to get a Claude Code API key in the Claude Console, when to sign in without one, how CLI auth priority and monthly API credits work, and how to avoid billing conflicts.

UniRoute ·

When looking up how to get a Claude Code API key, start by choosing how you want to authenticate and pay for usage. If you already subscribe to Claude Pro, Max, Team, or Enterprise, run /login inside Claude Code—no static key is needed. For direct pay-as-you-go API billing or headless scripts, create Claude API keys on the Claude Console API keys page (Get your Claude API key, verified 2026-10-23). In supported CLI releases, Console users can also sign in via /login OAuth without generating a static key (Authentication, 2026-10-23).

Disclosure: UniRoute publishes this guide and operates an independent third-party API gateway. It is not affiliated with or endorsed by Anthropic. If you use an official Claude subscription or a direct Claude Console key, you do not need UniRoute.

Comparing Credentials and Billing Paths

Match your credential to the account that will pay for your requests:

PathActive CredentialWhere Usage Is BilledBest For
1. Official Claude Subscription/login claude.ai OAuth (or CLAUDE_CODE_OAUTH_TOKEN from claude setup-token)Claude Pro, Max, Team, or Enterprise plan limitsInteractive coding under your subscription
2. Official Claude Console APIANTHROPIC_API_KEY (X-Api-Key) or Console OAuth sign-inConsole organization balance or eligible linked API credits (Separate billing, 2026-10-23)Direct Anthropic API billing and CI automation
3. Third-Party Gateway (e.g., UniRoute)ANTHROPIC_BASE_URL + gateway credential (ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY)Gateway account balance (e.g., UniRoute USD wallet by token usage)Multi-provider routing and separate API budgets

When Max and Team Monthly API Credits Apply

For Monthly API credits for Max and Team plans (verified 2026-10-23), eligible plans claim monthly credits and link an organization from their claude.ai plan billing settings, and view the credit balance in the Claude Console:

  • Interactive sessions are not covered: Interactive Claude Code usage (terminal, IDE, desktop, or web) and extra usage after hitting plan limits do not draw from monthly API credits.
  • When claude -p is covered: Non-interactive claude -p (and the Claude Agent SDK) is covered only when you run it yourself using an API key from your linked Console organization—not while signed in under your subscription.
  • Actions, IDEs, and Desktop are excluded: Runs triggered by the Claude Code GitHub Action, IDE extensions, or Claude Desktop are not covered, even with -p.

How to Get a Claude Code API Key in the Console

To create a static key for direct Anthropic billing, follow Get your Claude API key (verified 2026-10-23):

  1. Open the Console: Sign in at platform.claude.com/settings/keys (in the developer Console, not claude.ai settings).
  2. Choose a key type: Select a personal key for individual coding or a service account key for shared CI and backend pipelines. Both deactivate automatically if their linked account leaves the organization, unlike legacy workspace keys.
  3. Create and scope: Click Create key, name it, set an expiration date, link the account, and optionally scope it to a workspace.
  4. Save the secret immediately: The full sk-ant-... secret is shown only once. Save it in a secrets manager right away; lost keys cannot be recovered and must be replaced.
  5. If creation is disabled: Ask an organization admin for key access or a service account key, or use /login with your Console account if your organization allows Console OAuth sign-in without a static key.

CLI Setup and Authentication Priority

For direct Console API billing in a local terminal, export your key before launching Claude Code:

# Placeholder only. Use only when you want direct Console API billing.
export ANTHROPIC_API_KEY="YOUR_CLAUDE_CONSOLE_API_KEY"
claude

Never paste real keys into chats, Git repos, or screenshots, and never send an official Console key to a third-party ANTHROPIC_BASE_URL. Do not run claude -p in automated config checks, as -p sends a live model request that incurs charges.

While Anthropic's environment variable guide (2026-05-05) notes that environment keys override subscriptions in standard local shells, current Authentication precedence (verified 2026-10-23) depends on three practical conditions:

  • Interactive approval vs. -p: In standard CLI precedence, cloud-provider flags and ANTHROPIC_AUTH_TOKEN rank ahead of ANTHROPIC_API_KEY. When ANTHROPIC_API_KEY is reached in interactive mode, Claude Code prompts once to approve or decline it and remembers your choice (if declined earlier, enable Use custom API key in /config). Non-interactive mode (claude -p) skips that approval prompt so ANTHROPIC_API_KEY can be used without confirmation, but -p does not change precedence—higher-priority credentials such as cloud-provider flags or ANTHROPIC_AUTH_TOKEN still take priority.
  • Corporate Claude apps gateway and managed policies: A signed-in corporate Claude apps gateway session (not a regular third-party bearer gateway) takes precedence over cloud-provider flags, bearer tokens, API keys, and /login sessions. Organization-managed authentication policies can also restrict or block local environment credentials depending on the specific policy configured. See the official precedence documentation for enterprise policy details.
  • Cloud and Desktop ignore CLI shell variables: Cloud sessions always use subscription credentials, and Claude Desktop does not read terminal shell variables (see Claude Code Terminal vs Desktop).

Safe SET/UNSET Check (Without Printing Secrets)

Avoid echo $ANTHROPIC_API_KEY, env, or blind sed -i edits. Check variable presence in your current shell without printing secrets:

python3 - <<'PY'
import os
names = (
    "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL",
    "CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_PROFILE",
    "CLAUDE_CODE_USE_BEDROCK", "CLAUDE_CODE_USE_VERTEX", "CLAUDE_CODE_USE_FOUNDRY",
)
for name in names:
    print(f"{name}: {'SET' if os.environ.get(name) else 'UNSET'}")
PY

This script checks only the current shell process—not key validity, billing state, ~/.claude/settings.json, .claude/settings.local.json, managed policies, saved gateway sessions, or CLAUDE_CONFIG_DIR. Run /status inside Claude Code to inspect the active credential and Anthropic base URL. To return to your subscription, back up ~/.claude/settings.json, remove only the overrides you added, restart claude, and verify /status.

Optional: Routing Claude Code Through UniRoute

Per Connect Claude Code to an LLM gateway (verified 2026-10-23), gateways pair ANTHROPIC_BASE_URL with ANTHROPIC_AUTH_TOKEN (Authorization: Bearer) or ANTHROPIC_API_KEY (x-api-key) per gateway specification.

If you choose UniRoute, configure https://api.uniroute.app (without /v1 or /v1/messages) and ANTHROPIC_AUTH_TOKEN in ~/.claude/settings.json:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.uniroute.app",
    "ANTHROPIC_AUTH_TOKEN": "YOUR_UNIROUTE_API_KEY"
  }
}

UniRoute and Anthropic Console keys are separate credentials and cannot be swapped. Before setup, review model pricing, create a UniRoute API key, and read our Claude Code CLI setup guide, endpoint rules, wallet separation, and Claude Code API pricing. When ready to send live requests—which incur usage charges—check wallet top-ups.

Frequently Asked Questions

Do I need an API key if I already have Claude Pro or Max?

No. Run claude and sign in with /login to use your subscription limits.

Can I view my Claude Console API key again if I lose it?

No. The sk-ant-... secret appears only once at creation. Create a replacement key in platform.claude.com/settings/keys.

Why am I billed on the API after signing in with my subscription?

When no signed-in corporate Claude apps gateway session is active and no managed policy blocks local credentials, cloud-provider flags, ANTHROPIC_AUTH_TOKEN, or an approved ANTHROPIC_API_KEY (or ANTHROPIC_API_KEY in -p, which skips approval without changing precedence) rank ahead of /login. Check /status to see which credential is active.

Why did I get a 401 error after setting a key?

The key may be invalid or revoked, paired with the wrong ANTHROPIC_BASE_URL, or placed in the wrong header variable (ANTHROPIC_AUTH_TOKEN vs. ANTHROPIC_API_KEY).

Why didn't unset ANTHROPIC_API_KEY switch me back to my subscription?

Another override—such as ANTHROPIC_AUTH_TOKEN, apiKeyHelper, ANTHROPIC_PROFILE, or an env entry in ~/.claude/settings.json—may still be active. Check /status and remove only the override you added.

Do Max and Team monthly API credits cover Claude Code?

They do not cover interactive Claude Code sessions or extra usage after hitting plan limits. They apply to claude -p and the Agent SDK only when run by yourself with an API key from your linked Console organization—not under subscription login, GitHub Actions, IDE extensions, or Claude Desktop.