# Claude Code API Key Setup

Claude Code reads two environment variables to route requests anywhere: ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY. Point them at RunAPI and run every Claude model at half the official rate. This guide shows the exact commands for Mac, Linux, and Windows.

*Updated June 18, 2026 · RunAPI Editorial*

## How do you set a custom API key in Claude Code?

Claude Code uses two environment variables to decide where requests go and how they authenticate. Set ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY to RunAPI values and every request routes through RunAPI at 50% off. Want to use a different provider entirely? RunAPI also works as a drop-in gateway so you can run Claude models without a direct Anthropic account.

- **Two variables**: ANTHROPIC_BASE_URL sets the endpoint. ANTHROPIC_API_KEY sets the key. That is the whole config — two variables, no other changes.
- **Endpoint value**: Set ANTHROPIC_BASE_URL to https://runapi.ai so Claude Code routes through RunAPI.
- **Persist it**: Add both exports to your shell profile (.zshrc or .bashrc) so you do not re-set them each session. Windows users set them in PowerShell and open a new window.
- **50% off**: Every Claude model bills at half the official Anthropic rate. No subscription, no cap.

## What do you need before configuring Claude Code?

The setup takes a few minutes. You need Claude Code installed, a RunAPI account, and a terminal. Run claude --version first to confirm Claude Code is working before touching any API key config.

### Claude Code installed

Install Claude Code from the official source and confirm it runs with claude --version in your terminal. If that command errors, fix the install before setting any API keys.

### A RunAPI account

Sign up at runapi.ai. The free tier includes credits to test any Claude model before you fund a balance.

### An API key

Create a key from Dashboard → API Keys. Copy it once — it is shown in full only at creation time. Do not paste it into public repos or share it in screenshots.

### Terminal access

You need a shell to set environment variables. macOS and Linux use .zshrc or .bashrc. Windows users can use PowerShell — set the variable, then open a new terminal window for it to take effect.


## How to set up a Claude Code API key with RunAPI

Run these commands in order. They set the endpoint and key, persist them to your shell profile, and confirm Claude Code picks them up. Replace the placeholder with your real RunAPI key. Use ANTHROPIC_API_KEY (not CLAUDE_API_KEY) — that is the variable Claude Code actually reads.

## Which environment variables does Claude Code read?

Claude Code reads a small set of variables to control endpoint, auth, model, and output length. The table lists the ones that matter for a custom endpoint setup and what each does. Note: use ANTHROPIC_API_KEY (not CLAUDE_API_KEY) — that is the variable Claude Code actually reads.

| Variable | Purpose | Example value |
|---|---|---|
| ANTHROPIC_BASE_URL | Custom endpoint Claude Code sends requests to | https://runapi.ai |
| ANTHROPIC_API_KEY | Key used to authenticate requests | sk-your-runapi-key |
| ANTHROPIC_MODEL | Default model for new sessions | claude-sonnet-4-6 |
| CLAUDE_CODE_MAX_OUTPUT_TOKENS | Cap on tokens returned per response | 8192 |

## Which Claude model should you run in Claude Code?

Claude Code accepts any Claude model ID through RunAPI. Pick by task: a fast cheap model for routine edits, a balanced one for daily coding, or the strongest for hard problems. Set ANTHROPIC_MODEL to change the default, or switch models inside Claude Code — billing follows whichever model you actually call.

### claude-haiku-4-5

Fastest and cheapest at $1/M input and $5/M output through RunAPI. Best for routine edits and quick lookups.

### claude-sonnet-4-6

Balanced default for daily coding at $3/M input and $15/M output through RunAPI. Most developers run this.

### claude-opus-4-7

Strongest for hard problems at $5/M input and $25/M output through RunAPI. Use it for complex refactors and reviews.

### Switch any time

Set ANTHROPIC_MODEL or change the model inside Claude Code any time. Billing follows the model you actually call, all at 50% off.


## Why is my Claude Code API key not working?

Most setup problems come from one of three things: the wrong env var name (use ANTHROPIC_API_KEY, not CLAUDE_API_KEY), a stale shell session that has not picked up your profile changes, or a typo in the key. The table lists the common errors and the fix for each.

| Error or symptom | Likely cause | Fix |
|---|---|---|
| 401 Unauthorized | Wrong or expired API key | Recheck ANTHROPIC_API_KEY and create a new key in the RunAPI dashboard if needed |
| Requests still hit Anthropic | ANTHROPIC_BASE_URL not set in this shell | Run echo $ANTHROPIC_BASE_URL; if empty, reload your profile or open a new terminal |
| 404 Not Found | Trailing slash or wrong path in base URL | Set the base URL to exactly https://runapi.ai with no trailing path |
| Model not found | Invalid or misspelled model ID | Use a valid ID like claude-sonnet-4-6 or claude-opus-4-7 |
| Variables lost on restart | Exports not written to shell profile | Append both exports to ~/.zshrc or ~/.bashrc and reload |

## How to set up a Claude Code API key with RunAPI

1. **Set the base URL** — Export ANTHROPIC_BASE_URL=https://runapi.ai to route requests through RunAPI.
2. **Set the API key** — Export ANTHROPIC_API_KEY with your RunAPI key from Dashboard → API Keys.
3. **Persist both variables** — Append both exports to ~/.zshrc or ~/.bashrc and reload with source ~/.zshrc so they survive new sessions. On Windows, set them in PowerShell and open a new terminal window.
4. **Run Claude Code** — Run claude --version to confirm install, then launch claude in your project. Requests now route through RunAPI at 50% off the official rate.

## Claude Code API Key FAQ

### How do I add an API key to Claude Code?

Set two environment variables. Export ANTHROPIC_BASE_URL to https://runapi.ai and ANTHROPIC_API_KEY to your RunAPI key. Append both to your shell profile (.zshrc or .bashrc) so they persist across sessions. Claude Code reads them on startup and routes every request through RunAPI.

### Where do I get a Claude Code API key?

Create one at runapi.ai. Sign up, open Dashboard → API Keys, and generate a key. Copy it immediately — the full key is shown only at creation. The free tier includes credits to test any Claude model before you fund a balance.

### What is ANTHROPIC_BASE_URL for?

It tells Claude Code which endpoint to send requests to. By default Claude Code calls Anthropic directly. Setting it to https://runapi.ai routes every request through RunAPI instead, which bills the same models at 50% of the official rate.

### Can I use Claude Code with other API providers like OpenAI?

Is there a way to use Claude Code with other API providers like OpenAI? Yes — Claude Code supports any OpenAI-compatible endpoint via ANTHROPIC_BASE_URL. RunAPI exposes a compatible gateway so you can swap providers without changing your code, using one key across Claude, GPT, and other models.

### Why does Claude Code still bill at the full rate?

Run echo $ANTHROPIC_BASE_URL in your terminal. If it prints nothing, the export did not persist. Append both exports to ~/.zshrc or ~/.bashrc, run source ~/.zshrc, then open a new terminal and check again. Windows users need to open a new PowerShell window after setting env vars.

### Can I store the key in a config file instead?

Yes. Place the values in a .claude.json file in your home or project directory rather than exporting them each session. Both approaches work — the file is handy for per-project keys, while exports are simpler for a single global setup.

### How do I monitor cost after setup?

Open the RunAPI dashboard. It shows spend per key and per request in real time. To cap output cost, set CLAUDE_CODE_MAX_OUTPUT_TOKENS so each response is limited, and use claude-haiku-4-5 for routine edits.

### Does this change how Claude Code behaves?

No. RunAPI proxies requests directly to the underlying Claude models. Output, tool use, and behavior are identical to calling Anthropic directly. You only change where requests go and how they are billed — the coding experience stays the same.

