Thanks to visit codestin.com
Credit goes to runapi.ai

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 for Mac, Linux, and Windows.

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

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

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. Use ANTHROPIC_API_KEY (not CLAUDE_API_KEY) — that is the variable Claude Code actually reads.

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. On Windows, set them in PowerShell and open a new terminal window for the change to take effect.

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 — variables set in one session do not automatically carry over.

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

Fixing common errors

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

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 (.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://api.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.

RELATED

Explore More

Point Claude Code at RunAPI in two commands.

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