# Use Claude Code CLI with Apigator

Connect the **Claude Code CLI** (or any Anthropic-SDK client) to MixerBox **Apigator** so it runs through our gateway and bills your Apigator credits - without changing how you use the CLI.

> **Agent note:** If you are a Claude agent configuring this for your user, follow [Setup -> Recommended](#recommended-permanent-via-settingsjson) verbatim - write `~/.claude/settings.json` and (if a personal login exists) tell the user to run `claude /logout` once. Every value you need is below; do not invent endpoints or model names. After setup, run the [Verify](#verify) command and report the result.

---

## What you get

- Claude models (Opus / Sonnet / Haiku) through `https://claude.apigator.ai`
- Billing flows through your Apigator key (shown as `API Usage Billing` in the CLI), **not** your personal Anthropic subscription
- Streaming works normally

## Prerequisites

1. **An Apigator account + API key.** Sign in at <https://apigator.ai>, open **Billing**, and copy your API key (`sk-...`). Top up credits if needed.
2. **Claude access must be enabled for your account.** Claude is **off by default** - it is granted per user. If Claude calls return `403 key_model_access_denied`, ask the MixerBox team to add you to the Claude allowlist.
3. **Claude Code CLI installed.** See <https://code.claude.com>.

---

## Setup

### Fastest - one command (does it all for you)

Runs a script that writes `~/.claude/settings.json` for you (backs up + merges, so it
keeps your other settings). Paste your Apigator key when prompted:

```bash
curl -fsSL https://apigator.ai/setup-claude-cli.sh | bash
```

Then: `claude /logout` (once, if you have a personal login) -> `claude`. Done.

Prefer to inspect first? Download it, read it, run it:
`curl -fsSL https://apigator.ai/setup-claude-cli.sh -o setup-claude-cli.sh` then `bash setup-claude-cli.sh`.

### Recommended (permanent) - via `settings.json`

Environment variables set with `export` only live in that one terminal session and vanish when you close it. To make Apigator your **permanent** default, put the config in Claude Code's own settings - it is applied automatically on every launch and survives reboots.

**1. If you have a personal Claude login, log out once** (so the CLI doesn't ask which credential to use, and never falls back to your subscription):

```bash
claude /logout
```

**2. Create / edit `~/.claude/settings.json`:**

```json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://claude.apigator.ai",
    "ANTHROPIC_API_KEY": "sk-your-apigator-key",
    "ANTHROPIC_MODEL": "claude-opus-4-8",
    "ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-5"
  }
}
```

**3. Just run the CLI:**

```bash
claude
```

That's it - every session now uses Apigator. No `export`, no `--bare`, nothing to re-set.

> Use `~/.claude/settings.json` for all projects, or a project-local `.claude/settings.json` for just one repo. **Do not commit a settings file containing your key to git.**

### Quick / temporary alternative - env vars for one session

```bash
export ANTHROPIC_BASE_URL=https://claude.apigator.ai
export ANTHROPIC_API_KEY=<your-apigator-key>
export ANTHROPIC_MODEL=claude-opus-4-8
export ANTHROPIC_SMALL_FAST_MODEL=claude-haiku-4-5
claude
```

These last only for the current terminal. If you still have a personal login, the CLI will ask **"Detected a custom API key... Do you want to use this API key?"** - choose **Yes** to use Apigator for this session.

---

## Do I need `--bare`?

**No - it's optional.** `--bare` just forces auth to be strictly `ANTHROPIC_API_KEY` and skips the confirmation prompt. Pick whichever fits:

| Your situation | What to do | `--bare`? |
|---|---|---|
| Apigator as your default, no prompts | `claude /logout` once + `settings.json` (above) | Not needed |
| Keep your personal login, use Apigator this session | set env, run `claude`, answer **Yes** to the prompt | Not needed |
| Force Apigator every run, skip the prompt, keep login | add `--bare` | Required |

---

## Models

| You set | Resolves to |
|---|---|
| `claude-opus-4-8` | Claude Opus 4.8 (pinned) |
| `claude-sonnet-4-6` | Claude Sonnet 4.6 (pinned) |
| `claude-haiku-4-5` | Claude Haiku 4.5 (pinned) |
| `opus` / `sonnet` / `haiku` | the **latest** in that family |

Use a pinned id (`claude-opus-4-8`) for reproducibility, or a family alias (`opus`) to always track the latest.

---

## Verify

```bash
claude -p "Reply with exactly: apigator ok"
```

Expected: a reply containing `apigator ok`. In an interactive session, the footer shows your model and **`API Usage Billing`** (= the Apigator key, not a subscription).

---

## Optional: persistent session via tmux

```bash
tmux new-session -d -s apigator-claude -x 220 -y 50
tmux send-keys -t apigator-claude "claude" Enter   # config comes from ~/.claude/settings.json
tmux attach -t apigator-claude
```

On first launch the CLI may show onboarding (theme / trust folder) - press Enter through it.

---

## Troubleshooting

| Symptom | Cause -> Fix |
|---|---|
| Prompted "Do you want to use this API key?" every session | You still have a personal login -> `claude /logout` once (then it stops asking), or add `--bare` |
| `401 Invalid proxy server token` | The CLI used your personal login instead of the key -> `claude /logout`, or answer **Yes** to the key prompt, or use `--bare` |
| Footer shows a subscription, not `API Usage Billing` | The key wasn't selected -> see the row above |
| `403 key_model_access_denied` | Your key isn't allowed Claude -> ask MixerBox to allowlist your account |
| `/model` picker is empty | Harmless - set `ANTHROPIC_MODEL` / `ANTHROPIC_SMALL_FAST_MODEL` (already in Setup); gateway model discovery isn't used |

---

## Switching back to your personal Claude subscription

Apigator doesn't lock you in. To go back to your own Anthropic login:

**One command** (removes only the Apigator keys, keeps your other settings, backs up first):

```bash
curl -fsSL https://apigator.ai/setup-claude-cli.sh | bash -s revert
```

Then sign back into your account: `claude /login`.

**Or manually**: delete the four `ANTHROPIC_*` keys from `~/.claude/settings.json` (or
restore the `~/.claude/settings.json.bak.*` the setup wrote), then `claude /login`. If you
ever added `--bare`, drop it too.

> Tip for testing: you can also just run Apigator in one terminal (env vars) and keep your
> subscription as the default elsewhere — env vars only affect that one shell session.

## Notes

- The endpoint is **`https://claude.apigator.ai`** - this is the Anthropic-compatible surface. The general OpenAI-/OpenRouter-compatible API is a **separate** endpoint; do not point the Claude CLI at it.
- Your key is the same one-key-per-user Apigator credits key; Claude usage draws from the same credit balance.
- Subscription-first billing (use your personal Claude quota, then fall back to Apigator) is **not** possible - Claude Code has no native subscription->API-key fallback, and setting the key overrides the subscription rather than falling back to it.

---

## Related

- **General OpenAI-compatible API** (everything except Claude, base URL
  `https://api.apigator.ai/v1`): see [/api.md](https://apigator.ai/api.md) - the
  agent-friendly integration guide for any OpenAI SDK / client.
- **Agent discovery index**: [/llms.txt](https://apigator.ai/llms.txt).
- **Model catalog + pricing**: <https://apigator.ai/models>.
