Home / Learn / Claude Code API Key — How to Get and Set Up
Claude Code API Key — How to Get and Set Up — Setup Guide

Claude Code API Key — How to Get and Set Up

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.

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 Unlimited.

When you use a gateway like AI Prime Tech Unlimited 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.

Step 1 — Register and Generate Your Key

Go to unlimited.aiprimetech.io and create an account. The registration requires an email address and password. After confirming your email, log in to the dashboard. Navigate to the API Keys section and click Generate New Key. Give it a descriptive name like 'claude-code-laptop' or 'work-desktop' so you can identify it later if you need to revoke it.

Copy the key immediately after generation — most dashboards only show it once for security. Store it in a password manager or secure note. If you lose the key, you can always generate a new one from the dashboard and update your environment variables, but the old key will need to be revoked.

At this point you have a key but no balance. This is enough for approximately 1 million Sonnet tokens or 200,000 Opus tokens — plenty to verify your setup and run several real coding sessions before deciding on a plan.

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://unlimited.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://unlimited.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.

# 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

Frequently asked questions

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.

Should ANTHROPIC_BASE_URL include /v1?
No. Set it to the root host only: https://unlimited.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.

Start using Claude in minutes

Get an API key — no Anthropic account or waitlist required.

Get your API key

AI Prime Tech is an independent API gateway. It is not affiliated with, endorsed by, or a reseller of Anthropic. Claude and related model names are trademarks of their respective owners.