Get a free API key from AgentRouter and point Claude Code (CLI, desktop app, or IDE extension) at it — no Anthropic subscription needed.
setup.js does the work: it detects Node.js, npm, and the Claude CLI, verifies your key against
the live API, writes your config, and wires up the environment variables. Zero dependencies —
nothing to npm install.
What AgentRouter is: a community-run ("公益站" / non-profit) gateway that speaks the Anthropic API. Requests are routed through their servers, which means your prompts and code pass through a third party. Use it for hobby projects and experiments, not for confidential work. It offers no uptime guarantee and models come and go.
- Step 1 — Create an account (GitHub sign-in)
- Step 2 — Get free credit
- Step 3 — Create an API key
- Step 4 — Automatic setup
- Manual setup
- Claude Desktop and IDE extensions
- Checking your balance
- Configuration reference
- Troubleshooting
- Security notes
Register here: https://agentrouter.org/register
AgentRouter has no email/password signup. The only ways in are OAuth:
- GitHub (recommended)
- Linux.do community account
Tip
Refresh the page if only the form appears
- Open the registration link.
- Click Sign in with GitHub (
使用 GitHub 登录). - GitHub shows an Authorize screen listing what the app can read — normally your public profile and email. Click Authorize.
- You land on the AgentRouter console, already logged in. There is no email confirmation step.
AgentRouter runs an anti-abuse check on the GitHub account you sign in with. Brand-new throwaway accounts get rejected or receive no credit — this is standard for NewAPI-based gateways, which typically gate on account age (and sometimes public activity).
Two sources of credit:
- Signup bonus. New accounts get a starting balance. Third-party write-ups quote figures
from $100 to $200, Check your actual balance in the console, or run
node setup.js --balanceafter setup. Whatever a promo claims, the per-key spending cap is what actually governs you. - Daily check-in (
签到). The site announcements advertise $25 credit for checking in. Log into the console and click the check-in button — doing this daily is the main way to keep a free balance topped up.
Credit is denominated in USD. The gateway bills internally in CNY and converts.
- In the console sidebar open Tokens (
令牌). - Click Add Token (
添加令牌). - Give it a name. Leave the quota unlimited unless you want a per-key cap.
- Save, then Copy the key. It looks like
sk-followed by ~48 characters.
Copy it now — some builds only show the full key once.
Requires Node.js 18+.
git clone https://github.com/cmalf/Free-Claude-Code-API-Credit.git
cd Free-Claude-Code-API-Credit
node setup.js
It will:
- Check your environment — Node.js version, npm, Claude Code CLI, git.
- Ask for your API key, base URL, and default model (pressing Enter keeps the shown value).
- Verify the key by fetching your real balance.
- Write
config.json(chmod 600) andagentrouter.env.sh. - Merge the variables into
~/.claude/settings.json— your existing settings are kept and a timestamped.bak-*backup is made first. - Optionally add one
sourceline to your~/.zshrc/~/.bashrc.
Then start using it:
source ./agentrouter.env.sh
claudeAsk Claude to "reply with just OK". A reply means routing works.
node setup.js --check # diagnostics only, writes nothing
node setup.js --balance # credit balance
node setup.js --env # print the export lines — eval "$(node setup.js --env)"
node setup.js --yes # non-interactive, uses config.json as-is
node setup.js --helpIf the Claude CLI is missing, the script tells you the install command rather than installing a global npm package behind your back:
npm install -g @anthropic-ai/claude-code@latestIf you would rather not run the script.
node --version # must be v18 or newer
npm install -g @anthropic-ai/claude-code@latest
claude --versionmacOS / Linux (zsh, bash):
export ANTHROPIC_BASE_URL="https://agentrouter.org"
export ANTHROPIC_AUTH_TOKEN="sk-*****"
export ANTHROPIC_MODEL="claude-opus-5"
# If ANTHROPIC_API_KEY is set from something else, clear it — see the note below.
unset ANTHROPIC_API_KEYAlternate domain — use this if agentrouter.org is unreachable from your network (it is the
mirror they publish for users in mainland China, functionally identical):
export ANTHROPIC_BASE_URL="https://ps.air-outer.com"Windows PowerShell:
$env:ANTHROPIC_BASE_URL="https://agentrouter.org"
$env:ANTHROPIC_AUTH_TOKEN="sk-*****"
$env:ANTHROPIC_MODEL="claude-opus-5"
Remove-Item Env:\ANTHROPIC_API_KEY -ErrorAction SilentlyContinueTo make it permanent, put the export lines in ~/.zshrc or ~/.bashrc; on Windows use
setx ANTHROPIC_BASE_URL "https://agentrouter.org".
ANTHROPIC_AUTH_TOKENvsANTHROPIC_API_KEY— AgentRouter's own docs useANTHROPIC_AUTH_TOKEN(sent asAuthorization: Bearer). Some tools readANTHROPIC_API_KEYinstead (sent asx-api-key). AgentRouter accepts either, so setting both is the safe default — that is whatauthVariable: "both"inconfig.jsondoes.
Do not add
/v1to the base URL. Claude Code appends the Anthropic path itself. Thehttps://agentrouter.org/v1form is the OpenAI-compatible endpoint, for Codex-style clients.
Environment variables are per-shell. To make it apply everywhere, put them in
~/.claude/settings.json:
{
"env": {
"ANTHROPIC_BASE_URL": "https://agentrouter.org",
"ANTHROPIC_AUTH_TOKEN": "sk-*****",
"ANTHROPIC_MODEL": "claude-opus-5"
}
}Use .claude/settings.json inside a project to scope it to that project only.
Precedence: real shell variables override settings.json. If you set both and they disagree,
the shell wins.
claudeAt the first prompt, Claude Code asks you to confirm it is using an API key from the environment.
Accept, then send a trivial message. Check node setup.js --balance — the used amount should
have gone up.
The Claude Desktop chat signs in with your Anthropic account, not an API key, so a third-party gateway cannot be plugged into the chat UI.
What does work: Claude Code inside the desktop app reads ~/.claude/settings.json. Run
node setup.js, let it write that file, then fully quit and reopen the desktop app — the Claude
Code panel routes through AgentRouter.
Same for the VS Code / JetBrains extensions: they read ~/.claude/settings.json. Restart the
editor after setup.
node setup.js --balance ✓ Credit $-- left of $-- — used $-- (-.--%), valid until no expiry
The number comes from the gateway's live billing endpoints, so it reflects real usage, not a cached figure.
Copy config.example.json to config.json and edit it, or just run node setup.js.
{
"baseUrl": "https://agentrouter.org",
"fallbackUrl": "https://ps.air-outer.com",
"apiKey": "sk-REPLACE_WITH_YOUR_AGENTROUTER_KEY",
"model": "claude-opus-5",
"smallFastModel": "",
"authVariable": "ANTHROPIC_AUTH_TOKEN",
"targets": {
"claudeCode": true
},
"writeShellProfile": false
}| Key | Purpose |
|---|---|
baseUrl |
Primary endpoint. No /v1 suffix. |
fallbackUrl |
Tried automatically if baseUrl is unreachable; on success it is saved as the new baseUrl. |
apiKey |
Your sk-... token. |
model |
Value for ANTHROPIC_MODEL. |
smallFastModel |
Optional ANTHROPIC_SMALL_FAST_MODEL, used for cheap background tasks. Empty = omit. |
authVariable |
"both", "ANTHROPIC_AUTH_TOKEN", or "ANTHROPIC_API_KEY". |
targets.claudeCode |
Merge into ~/.claude/settings.json. |
writeShellProfile |
Add one source line to ~/.zshrc / ~/.bashrc, between # >>> agentrouter >>> markers. Re-running replaces the block instead of duplicating it. |
Model availability changes often on a free gateway. Query the live list:
curl -s https://agentrouter.org/v1/models \
-H "Authorization: Bearer $ANTHROPIC_API_KEY" \
-H "User-Agent: claude-cli/2.0.0 (external, cli)" | jq -r '.data[].id'The User-Agent header is required — see troubleshooting below. If your configured model
disappears from that list, pick another one from it.
unauthorized client detected / HTTP 401 on a working key
AgentRouter fingerprints the client User-Agent and rejects anything it does not recognise as a
supported CLI. Your key is fine; your curl is the problem. Add:
-H "User-Agent: claude-cli/2.0.0 (external, cli)"Claude Code sends an accepted UA on its own, so this only bites manual curl calls. Note this is
a client allowlist, not a security feature — don't read it as permission to hammer the API from
scripts; the operators ban for bulk automated use.
无效的令牌 / "invalid token" — the key is wrong, revoked, or missing its sk- prefix.
Recreate it in the console.
Response is HTML instead of JSON — you hit a front-end route rather than an API path, or a captive portal intercepted the request.
Claude Code still uses my Pro/Max subscription — these variables override a subscription login, but only in shells where they are set. Conversely, to get your subscription back:
unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_API_KEY ANTHROPIC_MODELand remove the env block from ~/.claude/settings.json. Restart claude.
Nothing changed after running setup — a shell variable is shadowing settings.json. The
script warns when it detects this. Run node setup.js --check, then either unset the old ones
or re-source ./agentrouter.env.sh.
Network unreachable — try the alternate domain https://ps.air-outer.com. The script does
this automatically.
404s, timeouts, model gone — the gateway is a free service with real downtime; announcements have mentioned upstream failures and Claude models being pulled. Check their Discord before debugging your own setup.
- Everything you send through the gateway is visible to whoever operates it. Do not point it at a private or client codebase.
setup.jsbacks up~/.claude/settings.jsonand your shell profile before editing (.bak-<timestamp>), and never installs anything globally without telling you.
- AgentRouter registration
- AgentRouter docs — Claude Code
- AgentRouter docs
- Claude Code settings reference
The balance endpoints and the User-Agent requirement were verified against the live API. Credit
amounts and the GitHub account-age threshold are not officially documented — verify in the
console.
MIT. Not affiliated with Anthropic or AgentRouter.