Claude Code API Key Setup: Windows, macOS, VS Code

Set up a Claude Code API key on Windows, macOS or VS Code: ANTHROPIC_BASE_URL, settings.json, tested fixes for 401 and 404 errors, and real per-turn costs.

GoiAPI · · 16 min read

Ask AI and more
View as Markdown

Diagram of Claude Code with ANTHROPIC_BASE_URL set to api.goiapi.com, sending requests to Claude Opus 5.5 and Claude Haiku 5.5, labeled Windows, macOS and VS Code

To run Claude Code on an API key instead of a Claude subscription, install it, then set two environment variables before you type claude: ANTHROPIC_BASE_URL (the API address; for GoiAPI it is https://api.goiapi.com, with no /v1) and ANTHROPIC_AUTH_TOKEN (your key). Add ANTHROPIC_MODEL to pick the model, for example claude-haiku-5-5. This guide covers every step on Windows, macOS, Linux and the VS Code extension, plus the errors people actually hit and what a single turn costs.

How a Claude Code API key works

Claude Code is Anthropic's coding agent. It runs in your terminal and in editors such as VS Code, reads your project, edits files, runs commands and reports back. Every step is a request to a Claude model, so you pay either through a plan or per token.

According to Anthropic's setup page (checked on December 5, 2026), Claude Code requires a Pro, Max, Ultra, Team, Enterprise or Console account; a claude.ai account without a paid plan doesn't include it. The other route is an API key, from the Anthropic Console or from an API gateway such as GoiAPI, where you pay only for the tokens you use.

What changes when you switch to a key, per Anthropic's LLM gateway docs (checked on December 5, 2026):

  • While a gateway credential is active, requests carry that credential instead of your claude.ai login, and your subscription's usage limits don't apply to them.
  • That traffic is billed per token to whoever owns the key.
  • Setting only ANTHROPIC_BASE_URL without a credential does not replace your subscription login.

If you already pay for Pro or Max and don't use up your limits, logging in with your account is the simpler choice. A key makes sense when you want pay-as-you-go billing, no monthly plan, or one key for several models. For a month-by-month cost comparison, read Claude Code Max vs API. If your company needs data terms signed directly with Anthropic, use an Anthropic Console key or a Team/Enterprise plan instead of a gateway.

Requirements

From Anthropic's setup page, checked on December 5, 2026:

ItemRequirement
macOS13.0 or later
WindowsWindows 10 1809+ or Windows Server 2019+
LinuxUbuntu 20.04+, Debian 10+, Alpine Linux 3.19+
Hardware4 GB+ RAM, x64 or ARM64 processor
ShellBash, Zsh, PowerShell or CMD
Node.jsNot needed for the native installer; the npm package needs Node.js 22+

Step 1: Install Claude Code

Windows (PowerShell, CMD or WinGet)

Open PowerShell and run:

POWERSHELL
irm https://claude.ai/install.ps1 | iex

From CMD, use this instead:

BAT
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd

Or install with WinGet (WinGet installs don't auto-update; run winget upgrade Anthropic.ClaudeCode from time to time):

POWERSHELL
winget install Anthropic.ClaudeCode

You don't need Administrator rights. Git for Windows is optional but recommended: with it, Claude Code uses Git Bash for its Bash tool; without it, Claude Code runs shell commands through PowerShell.

macOS, Linux and WSL

Open a terminal and run:

BASH
curl -fsSL https://claude.ai/install.sh | bash

On macOS you can use Homebrew instead (claude-code tracks the stable channel; Homebrew installs don't auto-update):

BASH
brew install --cask claude-code

npm (any OS)

Only if you need it. This route requires Node.js 22 or later. Don't use sudo:

BASH
npm install -g @anthropic-ai/claude-code@latest

Open a new terminal and check the install with claude --version. If something looks off, claude doctor prints installation and settings diagnostics without starting a session.

Step 2: Get an API key

With GoiAPI, one key calls Claude and the other models on the Models page:

  1. Sign up with email or Google and verify your email.
  2. Open Top up in the dashboard and pay with USDT (TRC20 or BEP20) or Binance Pay. No credit card and no monthly fee. Top-ups are final and non-refundable, so add only what you plan to use.
  3. Open API Keys and create a key. It looks like sk-... and is shown only once, so copy it somewhere safe right away.

When you create the key you can restrict it to certain models or IP addresses, and set a spending limit. Set a daily limit from the start: a long Claude Code session can send a lot of requests. If you'd rather not use a card at all, Claude API without a credit card walks through paying with USDT.

Step 3: Set ANTHROPIC_BASE_URL and your key

ANTHROPIC_BASE_URL tells Claude Code where to send requests instead of Anthropic's default endpoint. For GoiAPI, use https://api.goiapi.com. Do not add /v1: Claude Code appends /v1/messages by itself. The short version of this setup also lives in the Claude Code integration docs.

Diagram: ANTHROPIC_BASE_URL set to https://api.goiapi.com plus the appended /v1/messages gives the correct path; adding /v1 produces /v1/v1/messages and a 404 error
Claude Code appends /v1/messages, so ANTHROPIC_BASE_URL must not end in /v1.

macOS and Linux (bash or zsh)

Good for a quick test; the variables disappear when you close the terminal. Haiku is used here because it costs less, which is plenty for a test run:

BASH
export ANTHROPIC_BASE_URL="https://api.goiapi.com"
export ANTHROPIC_AUTH_TOKEN="sk-..."
export ANTHROPIC_MODEL="claude-haiku-5-5"
claude

Windows PowerShell

POWERSHELL
$env:ANTHROPIC_BASE_URL = "https://api.goiapi.com"
$env:ANTHROPIC_AUTH_TOKEN = "sk-..."
$env:ANTHROPIC_MODEL = "claude-haiku-5-5"
claude

To keep shell variables across new terminals, add the same lines to your shell profile (~/.zshrc, ~/.bashrc or your PowerShell $PROFILE). The settings file below is usually the better choice, though.

ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?

The two variables send your key in different headers. From Anthropic's environment variables reference and gateway connection guide (checked on December 5, 2026):

VariableHeader sentBehavior
ANTHROPIC_AUTH_TOKENAuthorization: Bearer <key>Takes precedence over a saved claude.ai login immediately
ANTHROPIC_API_KEYx-api-key: <key>In interactive mode you approve it once; in -p mode it's always used when present

GoiAPI accepts both headers (Authentication), and ANTHROPIC_AUTH_TOKEN is what we tested. If an old ANTHROPIC_API_KEY is already set on your machine (an Anthropic Console key, for example), remove it for this session so only one credential is active:

BASH
unset ANTHROPIC_API_KEY
POWERSHELL
Remove-Item Env:ANTHROPIC_API_KEY

Make it permanent with settings.json

To avoid retyping, and so the VS Code extension sees the same configuration, put the variables in the env block of your user settings file:

  • macOS and Linux: ~/.claude/settings.json
  • Windows: %USERPROFILE%\.claude\settings.json

This example uses Opus for real work:

JSON
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.goiapi.com",
    "ANTHROPIC_AUTH_TOKEN": "sk-...",
    "ANTHROPIC_MODEL": "claude-opus-5-5"
  }
}

Three things to know:

  • When a shell export and a settings-file env block set the same variable, the settings-file value wins (per Anthropic's gateway guide). If you exported a different model earlier, remove that export so you aren't guessing which one is active.
  • Settings files are strict JSON: a trailing comma or a // comment breaks the file.
  • Never put your key in a project's .claude/settings.json. That file is meant to be committed and shared with everyone who clones the repo. Keep the key in your user file.

Our own test (below) used shell variables, not settings.json. After saving the file, run the checks in the next step to confirm requests go through GoiAPI.

Step 4: Check that Claude Code uses your key

  1. Start claude and type /status. On the Status tab, look for an Anthropic base URL line showing https://api.goiapi.com (the line only appears when a custom base URL is set) and an Auth token line naming ANTHROPIC_AUTH_TOKEN. A Login method line naming a claude.ai account means your key isn't active.
  2. Send a short prompt, such as "List the files in this folder".
  3. Open Request history in the GoiAPI dashboard. Each request shows up with the model, token counts, cost and response time. If it's there, Claude Code is going through your key.

What we tested

On December 3, 2026 we ran Claude Code 2.1.325 on Windows 10 Pro, in a Bash shell, in an empty folder, in non-interactive mode, with a separate config folder so existing settings weren't touched and with ANTHROPIC_API_KEY removed:

BASH
env -u ANTHROPIC_API_KEY \
  ANTHROPIC_BASE_URL=https://api.goiapi.com \
  ANTHROPIC_AUTH_TOKEN=sk-... \
  ANTHROPIC_MODEL=claude-haiku-5-5 \
  claude -p "<a one-line question>" --output-format json

Claude Code answered correctly through https://api.goiapi.com. The modelUsage field in its JSON output listed only the model we set: no background calls to other model names and no 404s. That was -p mode; we did not test an interactive session or switching aliases with /model.

Use Claude Code in VS Code with an API key

  1. In VS Code 1.94.0 or later, open Extensions, search for Claude Code by Anthropic (ID anthropic.claude-code) and install it.
  2. Open Settings, search for Disable Login Prompt (claudeCode.disableLoginPrompt) and check it, so the extension doesn't ask you to sign in to a Claude account.
  3. Put the variables in the env block of ~/.claude/settings.json as shown above. Anthropic's VS Code page says variables there are shared by the extension and the CLI.

If you want the variables to apply to the extension only, Anthropic's gateway guide also documents the claudeCode.environmentVariables setting (open Preferences: Open User Settings (JSON)):

JSON
{
  "claudeCode.environmentVariables": [
    { "name": "ANTHROPIC_BASE_URL", "value": "https://api.goiapi.com" },
    { "name": "ANTHROPIC_AUTH_TOKEN", "value": "sk-..." }
  ]
}

These steps come from Anthropic's docs (checked on December 5, 2026); we have not tested the VS Code extension with GoiAPI ourselves. After setup, send one short message in the extension's chat panel and check Request history. If nothing shows up, you can still run claude in VS Code's integrated terminal with the configuration above.

Choose a model and estimate the cost

GoiAPI offers two Claude models. Input tokens are what you send (your prompt, code and Claude Code's own instructions); output tokens are what the model writes. Prices per 1M tokens, from the GoiAPI pricing page, checked on December 5, 2026:

ModelModel nameInputOutputCache readCache write
Claude Opus 5.5claude-opus-5-5$0.442$2.21$0.0221$0.8775
Claude Haiku 5.5claude-haiku-5-5$0.0221$0.1105$0.00221$0.043875

Prices can change; the pricing page always has the current numbers. Claude Code uses prompt caching automatically: tokens written to the cache are billed at the cache-write rate (higher than input), and tokens read back from it at the much lower cache-read rate. Each request that returns content also has a small minimum charge, which only matters for very short requests. For a deeper look at Claude per-token rates, see Claude API pricing.

How much one Claude Code turn costs

Claude Code sends a large system prompt and tool definitions with every request, so even a tiny question isn't cheap in tokens. In our December 3 test with Haiku, a one-line question used 10,679 input tokens, 15,574 cache-write tokens and 3 output tokens. Almost all of that is Claude Code's own instructions, not your question.

Token typeTokensHaiku 5.5 rate per 1MCost
Input10,679$0.0221$0.000236
Cache write15,574$0.043875$0.000683
Output3$0.1105under $0.000001
Totalabout $0.00092

If the same token counts were billed at Opus 5.5 rates, the turn would come to about $0.018. That's our own arithmetic from the token counts and the price table, not a measured Opus run, and real turns that read and edit files use more tokens. Two notes:

  • The total_cost_usd value Claude Code prints is its own estimate at its own price list, not your GoiAPI bill. Use Request history for the real cost of each request.
  • Later turns in the same session read much of that prompt back from the cache at the lower cache-read rate.

To size a full month, plug your expected usage into the cost calculator.

Model aliases and the /model command

Switch models mid-session with /model. Claude Code has three aliases, opus, sonnet and haiku; the model behind each is set by ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL and ANTHROPIC_DEFAULT_HAIKU_MODEL. Per Anthropic's environment variables reference, the Haiku one is also used for background tasks.

GoiAPI has no Sonnet model. In our -p test, setting only ANTHROPIC_MODEL was enough. If you get a 404 when switching aliases or in interactive sessions, map all three in your env block (we have not tested this mapping on GoiAPI):

JSON
{
  "env": {
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-5-5",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-opus-5-5",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-5-5"
  }
}

Can Claude Code use GPT, DeepSeek or GLM?

Anthropic states it doesn't support using Claude Code with non-Claude models through any gateway. In our December 3 test, the same claude -p command did answer through https://api.goiapi.com with glm-5.3, deepseek-v4-pro-0813 and gpt-6.1-sol, using 14,684 to 16,507 input tokens per turn (about $0.0019 to $0.0024 per turn by our own arithmetic at those models' GoiAPI prices). Claude Code printed a [claude-code:unrecognized_model] warning to stderr each time, and we did not test long tasks that read and edit files. The trade-offs are covered in Claude Code Router.

Troubleshooting Claude Code API key setup

For Invalid API key or a request to run /login, go to Claude Code invalid API key. For connection errors, dropped streams and timeouts, see Claude Code API error codes.

ANTHROPIC_BASE_URL not working

If Claude Code still asks you to log in, reports a model error, or Request history shows nothing new, check in this order:

  1. ANTHROPIC_BASE_URL is exactly https://api.goiapi.com, with no /v1 at the end.
  2. Any old ANTHROPIC_API_KEY is removed, so the key in ANTHROPIC_AUTH_TOKEN is the one in use.
  3. The variables sit in the env block of your user settings.json, at the path above, and the file is valid JSON.
  4. /status shows the Anthropic base URL line. If it's missing, the variable didn't reach the session.

"There's an issue with the selected model" (base URL ends in /v1)

We reproduced this on December 3, 2026 by setting ANTHROPIC_BASE_URL=https://api.goiapi.com/v1 and running the same claude -p command with claude-haiku-5-5. Claude Code exited with code 1 and printed:

TEXT
There's an issue with the selected model (claude-haiku-5-5). It may not exist or you may not have access to it. Run --model to pick a different model.

Underneath, the status was 404 (api_error_status: 404). With that base URL the path becomes /v1/v1/messages, which doesn't exist; GoiAPI answers that path with The path POST /v1/v1/messages does not exist. The message talks about the model, not the URL, so if you're sure the model name is right, check for /v1 first.

'irm' is not recognized

You ran the PowerShell command in CMD. Open PowerShell and run it again, or use the CMD install command.

The token '&&' is not a valid statement separator

The reverse: you ran the CMD command in PowerShell. Use irm https://claude.ai/install.ps1 | iex.

Claude Code can't find Git Bash on Windows

Set CLAUDE_CODE_GIT_BASH_PATH in the env block. Backslashes must be doubled in JSON:

JSON
{
  "env": {
    "CLAUDE_CODE_GIT_BASH_PATH": "C:\\Program Files\\Git\\bin\\bash.exe"
  }
}

401, 402, 403, 404, 429 and 503 errors

The GoiAPI messages below are the English ones from the error codes docs; messages follow your account's language.

StatusExample messageWhat to do
401The API key is invalid or has been revoked.Check ANTHROPIC_AUTH_TOKEN; create a new key if it was revoked
402Your balance is too low for this request (about ${amount} needs to be held). Please top up your balance.Top up. Funds are held based on max_tokens before the call, then only actual usage is charged, so a very low balance can fail on a big request
403This API key is not allowed to use the model {model}.Check the key's model and IP restrictions in the dashboard
404The model {model} does not exist. See the list at /v1/models.Fix the model name, and make sure the base URL has no /v1
429This API key has reached its limit of {limit} requests per minute. Please try again in {seconds} s.Wait for the seconds in Retry-After, then retry. Our test key on December 3 returned x-ratelimit-limit-requests: 60; other keys may differ
503The model is temporarily not responding. Please try again later. Support ID: {requestId}Retry later; check the status page

A 429 can also mean you hit the daily or monthly spending limit you set on the key, or the model is overloaded. GoiAPI never returns 529; overload shows up as 429 or 503, and Claude Code retries both automatically. A failed request that returns no content isn't charged (unless you cancel it yourself). Every response carries an x-request-id header; send it to support if you need a lookup.

Remote Control doesn't work

As of Claude Code v2.1.196, Remote Control is disabled when ANTHROPIC_BASE_URL points at a host other than api.anthropic.com, and it also needs a claude.ai identity rather than a key. That's a Claude Code limit, not a gateway one. If you depend on Remote Control or voice dictation, an Anthropic plan suits you better.

Long requests time out

API_TIMEOUT_MS is how long each request attempt has to receive its response headers; the default is 600000 (10 minutes). Each GoiAPI request can run for up to 15 minutes (Limits and balance). If very long non-streaming requests get cut off, raise API_TIMEOUT_MS in your env block, up to 900000 to match that limit.

FAQ

Can I use Claude Code without a subscription?

Yes. Set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN (or use an Anthropic Console key) and Claude Code bills per token to the key's owner instead of using a plan. A claude.ai account without a paid plan doesn't include Claude Code.

Do I need Node.js to install Claude Code?

No, not with the native installer (irm ... | iex on Windows, curl ... | bash on macOS and Linux). Only the npm package needs Node.js 22 or later.

What should ANTHROPIC_BASE_URL be, and do I add /v1?

It's the address Claude Code sends requests to instead of Anthropic's default. For GoiAPI, set https://api.goiapi.com without /v1, because Claude Code appends /v1/messages itself. With /v1 added you get a misleading "issue with the selected model" error.

Should I use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?

For GoiAPI, put your key in ANTHROPIC_AUTH_TOKEN; it's sent as Authorization: Bearer and takes over from a saved login without a prompt. Remove any old ANTHROPIC_API_KEY so two credentials don't compete.

Where is Claude Code's settings.json on Windows and Mac?

The user file is ~/.claude/settings.json on macOS and Linux and %USERPROFILE%\.claude\settings.json on Windows. Installing Claude Code doesn't create it, so create the file yourself if it's missing.

If I use an API key, does my Pro or Max usage limit still apply?

No. Requests sent with a key are billed per token to the key's owner and don't count against your plan's limits. Your plan still exists; log back in with your Claude account (and unset the key variables) to use it.

Does the VS Code extension share settings with the terminal?

Yes, the env block in ~/.claude/settings.json is read by both, per Anthropic's VS Code docs. Disable Login Prompt is a VS Code setting, so turn it on in VS Code itself.

How do I check which key Claude Code is using?

Run /status inside Claude Code. It shows the base URL and whether an auth token, API key or claude.ai login is active.

Get started with GoiAPI

  1. Create an account and verify your email.
  2. Top up with USDT (TRC20 or BEP20) or Binance Pay. No card, no monthly fee; top-ups are final, so add only what you'll use.
  3. Create an API key, set ANTHROPIC_BASE_URL=https://api.goiapi.com and ANTHROPIC_AUTH_TOKEN as shown above, and run claude.

The quickstart has the same steps plus curl tests for the OpenAI format (https://api.goiapi.com/v1) and the Anthropic format.

Disclosure: this article is written by GoiAPI, which offers paid access to Claude Opus 5.5 and Claude Haiku 5.5 mentioned above.

Related posts