Claude Code

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.

Updated June 18, 2026 RunAPI Editorial
At a glance

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 them to RunAPI values and every request routes through RunAPI at 50% off. The cards below cover the essentials before the full walkthrough.

Two variables

ANTHROPIC_BASE_URL sets the endpoint. ANTHROPIC_API_KEY sets the key. That is the whole config.

Endpoint value

Set ANTHROPIC_BASE_URL to https://api.runapi.ai so Claude Code routes through RunAPI.

Persist it

Add both exports to your shell profile or .claude.json so you do not re-set them each session.

50% off

Every Claude model bills at half the official Anthropic rate. No subscription, no cap.

Before you start

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. The list below covers everything required before the first command.

Claude Code installed

Install Claude Code from the official source and confirm it runs with the claude command in your terminal.

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.

Terminal access

You need a shell to set environment variables. macOS, Linux, and WSL on Windows all work the same way.

Step by step

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.

1

Set the base URL

This tells Claude Code to send requests to RunAPI instead of the default Anthropic endpoint.

export ANTHROPIC_BASE_URL=https://api.runapi.ai
2

Set the API key

Paste your RunAPI key in place of the placeholder. Claude Code uses it to authenticate every request.

export ANTHROPIC_API_KEY=sk-your-runapi-key
3

Persist both variables

Append both exports to ~/.zshrc (or ~/.bashrc) so they survive new terminal sessions. Reload with source ~/.zshrc.

echo 'export ANTHROPIC_BASE_URL=https://api.runapi.ai' >> ~/.zshrc
4

Verify the config

Confirm the variable prints the RunAPI URL. If it is empty, reload your shell profile or open a new terminal.

echo $ANTHROPIC_BASE_URL
5

Run Claude Code

Launch Claude Code in your project. It now routes through RunAPI at 50% off the official rate.

claude
Environment variables

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.

Variable Purpose Example value
ANTHROPIC_BASE_URL Custom endpoint Claude Code sends requests to https://api.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

You can also store these in a .claude.json config file in your home or project directory instead of exporting them each session.

Choosing a model

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.

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. Billing follows the model you actually call, all at 50% off.

Fixing common errors

Why is my Claude Code API key not working?

Most setup problems come from an unset variable, a stale shell, 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://api.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
Quick version

Claude Code API key setup in four steps

1

Set the base URL

Export ANTHROPIC_BASE_URL=https://api.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 so they survive new sessions.

4

Run Claude Code

Launch claude in your project. Requests now route through RunAPI at 50% off the official rate.

Frequently asked questions

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://api.runapi.ai and ANTHROPIC_API_KEY to your RunAPI key. Append both to your shell profile so they persist. 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, since 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://api.runapi.ai routes every request through RunAPI instead, which bills the same models at 50% of the official rate.

Why does Claude Code still bill at the full rate?

The base URL is probably not set in your current shell. Run echo $ANTHROPIC_BASE_URL — if it is empty, the export did not persist. Append it to ~/.zshrc or ~/.bashrc, reload with source, then open a new terminal and check again.

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 handier 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 choose a cheaper model like Haiku 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 the default setup. You only change where requests go and how they are billed — the coding experience stays the same.

Which model should I set as default?

Most developers set ANTHROPIC_MODEL to claude-sonnet-4-6 for daily coding. Use claude-haiku-4-5 for fast cheap edits or claude-opus-4-7 for hard refactors. You can switch models inside Claude Code at any time, and billing follows the model you call.

Point Claude Code at RunAPI in two commands.

Create a free RunAPI account, set ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY, and run every Claude model in Claude Code at 50% off official Anthropic pricing.