---
title: Claude Code API Key: Two-Variable Setup
description: "Get a Claude Code API key without an Anthropic subscription: set ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY and run every Claude model at a flat 50% off."
url: https://apitoken.sale/docs/learn/claude-code-api-key
language: en
---

# Run Claude Code on an apiToken.sale API key

Claude Code takes its endpoint and credential from two environment variables, so a Claude Code API key from apiToken.sale is a drop-in replacement for subscription billing: set ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY once and the CLI runs unchanged against your prepaid balance. Below is the exact setup, which model to run per session, and fixes for the three errors everyone hits first.

## Set ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY

Run one command. The setup asks for the provider and key, fetches every model enabled for the key, writes a private launcher, and removes the competing auth variable. It works on macOS, Linux, PowerShell, and Windows CMD.

1. Create a free account and generate a customer key in the dashboard.
2. Run curl -fsSL https://apitoken.sale/setup/connect.sh | bash. On Windows PowerShell run irm https://apitoken.sale/setup/connect.ps1 | iex; from CMD run powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://apitoken.sale/setup/connect.ps1 | iex".
3. Start the private launcher printed by the setup and send a small prompt.

```
curl -fsSL https://apitoken.sale/setup/connect.sh | bash

# Windows PowerShell
irm https://apitoken.sale/setup/connect.ps1 | iex
```

## Keep the variables across terminals

An export only lives for the current shell session. Close the terminal and Claude Code loses its credentials, which is the most common reason a working setup 'stops working' the next day. Put both lines in your shell startup file — ~/.zshrc on macOS, ~/.bashrc on most Linux setups — and they load automatically.

```
The setup stores the key in ~/.config/apitoken/key with private permissions. Use the generated launcher in new terminals.
```

> The variables must be exported, not just assigned, because Claude Code runs as a child process of your shell and only inherits exported variables. If you edited the startup file by hand, open a new terminal or run source on the file before launching claude.

## Pick the model per session, not per month

Model choice is the single biggest cost lever in Claude Code, and a prepaid key makes it a per-session decision instead of a plan decision. One key serves the whole supported line, so you can default to a mid-tier model and escalate only when the task earns it.

| Model ID | Use it for |
| --- | --- |
| claude-sonnet-5 | Everyday coding: features, tests, small fixes. The sensible default for most sessions. |
| claude-opus-4-8 | Hard refactors, multi-file reasoning, and long agentic sessions where mistakes are expensive. |
| claude-haiku-4-5 | Quick questions, cheap experimentation, and high-volume steps where speed matters more than depth. |

Switch mid-session with the /model command, or set the model at launch: claude --model claude-opus-4-8. A practical pattern is to start on claude-sonnet-5 and only move up to Opus when Sonnet stalls on the same problem twice.

[Per-model prices and context windows](/models)

## What changes and what stays the same

Bringing your own key changes billing and nothing else. Claude Code is the same binary talking to the same Anthropic Messages API, so the features you use daily keep working.

- Agentic editing, tool use, and streaming behave identically — only the billing endpoint changed.
- Model IDs are unchanged: claude-opus-4-8, claude-sonnet-5, claude-haiku-4-5.
- The same key also works in Cursor, Cline, Continue, Aider and the official Anthropic SDKs, so one balance covers your whole toolchain.
- The balance is prepaid and never expires; it is spent only when requests actually run, at a flat 50% off official spend for B2C accounts.

## See what each session costs

The dashboard shows per-request token usage, so a long Claude Code session is no longer a black box — you can see exactly which prompts and models consumed the balance. Top up any whole-dollar amount by bank card or cryptocurrency; there is no fixed product catalog and no monthly commitment.

[Estimate a month of Claude Code usage in the free calculator](/tools/claude-api-cost-calculator)

## Fix the three errors everyone hits first

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Authentication or 401 error | Typo in a variable, or the variable was never exported | Re-check both ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY, then restart the shell so they are exported. |
| Claude Code ignores the key and uses a subscription | An old subscription login is still active | Run /logout (or check /status) inside Claude Code, then relaunch so the environment key is used. |
| 429 rate limit responses | Too much burst concurrency | Honor Retry-After, back off, and reduce parallelism; contact support for sustained higher throughput. |

> Support is available in English and Russian over Telegram if an error persists after the fixes above.

## Give Claude Code its own key

Because one apiToken.sale account can issue multiple named keys, give Claude Code a dedicated key instead of sharing one across every tool. If a key ever leaks from a shell history file or a committed dotfile, you revoke that one key and the rest of your setup keeps running.

- Set a lifetime spending limit on the Claude Code key to cap the blast radius of a runaway session.
- Add an expiration date if the key is for a short-lived project or a contractor machine.
- Keep the key in the environment or a secret manager — never in git, dotfiles you publish, or chat messages.

## Frequently asked questions

### How do I get an API key for Claude Code?

Create a free apiToken.sale account, generate a key in the dashboard (it looks like sk-pool-…), then set ANTHROPIC_BASE_URL to https://router.apitoken.sale and ANTHROPIC_API_KEY to the key. Run claude and you are done.

### Can I use Claude Code without an Anthropic subscription?

Yes. Claude Code accepts a plain API key, and with an apiToken.sale key it bills prepaid balance at a flat 50% off official spend. The balance never expires, so light users stop paying for idle months.

### Which model should I set in Claude Code?

Default to claude-sonnet-5 for everyday coding, switch to claude-opus-4-8 with /model for hard refactors and long sessions, and use claude-haiku-4-5 for cheap, high-volume steps.

### Why does Claude Code say my API key is invalid?

Almost always a typo or a variable that was set but not exported. Re-check both environment variables, restart your shell, and if Claude Code still uses a subscription login, run /logout first.

### How do I track what Claude Code costs on a prepaid key?

The apiToken.sale dashboard shows per-request token usage for the key. Set a lifetime spending limit on the key if you want a hard ceiling on what a session can burn.

---
Get a key: https://apitoken.sale/register
More guides: https://apitoken.sale/docs/learn
