# Claude Code API key — ANTHROPIC_API_KEY: how to get it, set it up, and when to use claude setup-token instead Claude Code needs an API key to function. Whether you are setting up Claude Code for the first time or migrating from a direct Anthropic account to a flat-rate gateway, the process involves generating a key, configuring two environment variables, and verifying the connection. ## Claude Code API key: the short version A Claude Code API key is an ordinary Anthropic-format key (`sk-...`) that Claude Code reads from `ANTHROPIC_API_KEY`. Get one in the Claude Console (Settings → API keys, shown once) or in this gateway's dashboard (visible any time), export it, and Claude Code starts without the browser login. With a gateway key you also set `ANTHROPIC_BASE_URL` to the gateway host. That is the whole setup; the rest of this page is the detail — the difference between an API key, the OAuth token from `/login` and a claude.ai session key, the per-OS steps, and the errors people hit. ## What Is a Claude Code API Key A Claude Code API key is the authentication credential that lets the Claude Code CLI make requests to a Claude model provider. Without it, every command you run in Claude Code returns an authentication error. The key is a string that you store in an environment variable — either ANTHROPIC_API_KEY (sent as x-api-key header) or ANTHROPIC_AUTH_TOKEN (sent as Bearer token). Both work with AI Prime Tech. When you use a gateway like AI Prime Tech instead of Anthropic directly, the key comes from the gateway dashboard rather than the Anthropic Console. The key format may differ, but the setup process in Claude Code is identical: set the key in your environment, point the base URL at the gateway, and Claude Code handles the rest. Your key is tied to your gateway account balance or subscription, not to Anthropic billing. One key can be used across multiple machines and Claude Code sessions simultaneously. However, if you share a key across a team, all usage counts toward the same account's rate limits and balance. For team setups, generate a separate key per developer from the dashboard so usage tracking and rate limiting work correctly per person. ## API key vs OAuth token — and what a "session key" is Claude Code accepts two kinds of credential. An API key (ANTHROPIC_API_KEY, or ANTHROPIC_AUTH_TOKEN sent as a Bearer token) bills per token against an API balance — this is what you use with a gateway. An OAuth token comes from a Claude Pro or Max subscription: `/login` inside Claude Code signs you in, and `claude setup-token` mints a long-lived token you can put in CI or a headless machine so it draws on the subscription instead of an API balance. A "Claude session key" is neither: it is the browser session cookie from claude.ai, which some unofficial clients scrape to drive the chat interface. It is not an API credential, it violates the consumer terms, it expires, and it can get the account suspended. If you want programmatic access, use an API key — from Anthropic's console or from a gateway like this one. ## Step 1 — Get the key: Claude Console or gateway dashboard Two places issue a key that Claude Code accepts. In the Claude Console (the Claude platform at platform.claude.com) open Settings → API keys, click Create Key, name it, and copy the key secret — the Console shows it once and only lists the name and prefix afterwards, so key management there means creating a new key rather than retrieving an old one. A regular key is what Claude Code needs; an admin API key is a different credential for managing an organisation and will not authenticate requests. Then `export ANTHROPIC_API_KEY=sk-ant-...` in your shell and Claude Code uses it without /login. Anthropic bills per token and needs a payment method on the account first. The faster route is a gateway key: register at aiprimetech.io, open API Keys in the dashboard, click Create, and the key is visible in the dashboard at any time — copy it again, rename it or revoke it whenever you like. It is the same `sk-` shape and works in the same variable; the only extra step is ANTHROPIC_BASE_URL, covered in Step 2. At this point you have a key but no balance. Add a small top-up before testing — the models page shows what a dollar buys per model — or pick an unlimited plan on /plans/ if you already know you will run Claude Code all day. ## Step 2 — Set Environment Variables Claude Code reads two environment variables for gateway configuration: ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY. Set ANTHROPIC_BASE_URL to the root host of the gateway without any path suffix. The Anthropic SDK appends /v1/messages automatically — if you include /v1 in the base URL, requests will hit /v1/v1/messages and return 404 errors. On Linux or macOS, add these lines to your shell profile (~/.bashrc, ~/.zshrc, or ~/.profile): export ANTHROPIC_BASE_URL="https://aiprimetech.io" and export ANTHROPIC_API_KEY="your-key-here". On Windows, set them as system environment variables through Settings > System > Advanced system settings > Environment Variables, or add them to your PowerShell $PROFILE. For per-project configuration, create a .env file in your project root with the same variables. Claude Code loads .env files from the working directory, and these take precedence over system-level variables. This is useful if you use different API providers for different projects. Make sure to add .env to your .gitignore to avoid accidentally committing your key to version control. ## Step 3 — Verify the Connection After setting the environment variables, open a new terminal session (so the variables are loaded) and run Claude Code. Type a simple message like 'Hello, confirm you can respond.' If you get a response from Claude, your setup is working end-to-end. The key is valid, the base URL is correct, and the gateway is routing your request to Claude successfully. If verification fails, the error message tells you what went wrong. A 401 or 403 error means the API key is invalid or expired — copy it fresh from the dashboard. A 404 error almost always means the base URL is wrong — check for accidental /v1 suffix or trailing slashes. A connection refused error means the gateway is unreachable — check your network, VPN, or firewall settings. You can also verify programmatically by hitting the gateway's status endpoint with curl: curl -H 'Authorization: Bearer YOUR_KEY' https://aiprimetech.io/v1/models — this returns the list of available models if your key is valid. If this works but Claude Code does not, the issue is in your environment variable loading, not in the key or gateway itself. ## Step 4 — Configure Model Preferences By default, Claude Code uses its built-in model selections for each tier (Sonnet, Opus, Haiku). You can override these with additional environment variables: ANTHROPIC_DEFAULT_SONNET_MODEL, ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_HAIKU_MODEL, and ANTHROPIC_DEFAULT_FABLE_MODEL. Set these to the exact model identifiers the gateway supports. For most developers, setting ANTHROPIC_DEFAULT_SONNET_MODEL=claude-sonnet-4-5 and ANTHROPIC_DEFAULT_OPUS_MODEL=claude-opus-4-6 is sufficient. Claude Code defaults to Sonnet for most interactions and uses Opus when you select it via the /model command or when plan mode escalates to a stronger model. The gateway supports all current Claude model versions. Enable gateway model discovery by setting CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1. This tells Claude Code to query the gateway for available models instead of using a hardcoded list. Also set CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 to prevent telemetry requests from going to the gateway — this reduces noise and slightly improves request latency. ## Troubleshooting Common Auth Errors The most frequent issue is environment variables not being loaded. After setting them in your profile, you must open a new terminal — existing terminals use the old environment. On Windows, you may need to restart your terminal application entirely, not just open a new tab. Verify with echo $ANTHROPIC_BASE_URL (bash/zsh) or echo $env:ANTHROPIC_BASE_URL (PowerShell). If you get 'model not found' errors despite a valid connection, the model identifier does not match what the gateway offers. Model IDs are case-sensitive and version-specific. Check the dashboard or /v1/models endpoint for exact supported identifiers. Common mistakes include using 'claude-3-opus' instead of 'claude-opus-4-6' or adding extra whitespace in the environment variable value. Check your dashboard balance. To continue without interruption, either add pay-as-you-go balance or upgrade to an unlimited plan — 1 day ($9) or 1 week ($39) for flat-rate access to all models without per-token billing. ## Example ``` # Add to ~/.bashrc or ~/.zshrc: export ANTHROPIC_BASE_URL="https://aiprimetech.io" export ANTHROPIC_API_KEY="your-key-from-dashboard" export ANTHROPIC_DEFAULT_SONNET_MODEL="claude-sonnet-4-5" export ANTHROPIC_DEFAULT_OPUS_MODEL="claude-opus-4-6" export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 # Verify connection: # $ source ~/.bashrc && claude # Or test the gateway directly: # $ curl -H "x-api-key: $ANTHROPIC_API_KEY" https://aiprimetech.io/v1/models ``` ## FAQ ### ANTHROPIC_API_KEY — how to get it? Either create a key in the Claude Console (Settings → API keys → Create Key; copy the key secret once) or create one in the gateway dashboard here, where it stays visible. Export it as ANTHROPIC_API_KEY; with a gateway key also set ANTHROPIC_BASE_URL. ### Do I need a separate key for Claude Code vs other tools? No. The same API key works for Claude Code, Cursor, Cline, Aider, and any other tool that uses the Anthropic Messages API or OpenAI-compatible endpoint. One key, multiple tools. ### Is a Claude session key the same as an API key? No. A session key is the claude.ai browser cookie used by unofficial clients; an API key is the credential the API and Claude Code expect. Only the API key is supported, billed and stable. ### How do I get an ANTHROPIC_API_KEY? From Anthropic's console (billed at list price to your Anthropic account) or from this gateway's dashboard (billed against credits bought below face value, or an unlimited plan). Either goes in the ANTHROPIC_API_KEY environment variable. ### What does `claude setup-token` do? It creates a long-lived OAuth token for a Claude subscription so Claude Code can run in CI or headless without interactive login. It uses subscription usage, not API credits; with a gateway you use an API key instead. ### Should ANTHROPIC_BASE_URL include /v1? No. Set it to the root host only: https://aiprimetech.io — the Anthropic SDK appends /v1/messages automatically. Including /v1 causes a doubled path that returns 404 errors. ### Can I use the same key on multiple machines? Yes. One key works across all your devices simultaneously. All usage counts toward the same account's balance and rate limits. For teams, generate separate keys per developer for independent tracking. --- Source: https://aiprimetech.io/learn/claude-code-api-key-setup/ Provider: AI Prime Tech — independent Anthropic-compatible API gateway (not affiliated with Anthropic).