# Claude Code

> Connect Claude Code to MirAPI over the Anthropic protocol with ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, and ANTHROPIC_MODEL.

Claude Code is Anthropic's terminal coding agent. Because MirAPI implements the Anthropic Messages API (`POST /v1/messages`), you can point Claude Code at MirAPI with three environment variables — no proxy or patch required.

## What you need

| Element | Value |
|---|---|
| Base URL | `https://api.mirapi.ai` — the bare origin, **without `/v1`** |
| API key | Your MirAPI key (`sk-...`), passed through `ANTHROPIC_AUTH_TOKEN` |
| Model | `deepseek-chat` via `ANTHROPIC_MODEL` (or the `model` setting) |

## Configure

1. Create an API key in https://console.mirapi.ai → **API Keys** and store it:

```bash
export MIRAPI_API_KEY=sk-...
```

2. Export the connection variables. Add them to your shell profile (`~/.zshrc`, `~/.bashrc`, or `~/.config/fish/config.fish`) so they persist:

```bash
export ANTHROPIC_BASE_URL="https://api.mirapi.ai"
export ANTHROPIC_AUTH_TOKEN="$MIRAPI_API_KEY"
export ANTHROPIC_MODEL="deepseek-chat"
```

Define `MIRAPI_API_KEY` **before** `ANTHROPIC_AUTH_TOKEN` — the token variable expands when the profile is sourced, so an empty value here causes auth errors.

3. Reload your shell and start Claude Code:

```bash
source ~/.zshrc   # or ~/.bashrc, or open a new terminal
claude
```

:::note
**Project-level configuration.** To scope the settings to one project instead of your whole shell, put them in `.claude/settings.local.json` in the project root:

```json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.mirapi.ai",
    "ANTHROPIC_AUTH_TOKEN": "sk-...",
    "ANTHROPIC_MODEL": "deepseek-chat"
  }
}
```
:::

## Verify with a message

Run `/status` inside Claude Code and confirm the auth token and base URL point at MirAPI:

```text
/status
```

Then send a quick test message:

```text
Say "connected to MirAPI" and nothing else.
```

The reply should contain `connected to MirAPI`. The first request draws a small amount from your prepaid balance; confirm it later with `GET /api/usage/token` or in the console billing log.

## Credential variables

Claude Code sends the two credential variables as different headers, so which one you set matters:

| Variable | Header sent | Use with MirAPI |
|---|---|---|
| `ANTHROPIC_AUTH_TOKEN` | `Authorization: Bearer <token>` | Recommended for a gateway |
| `ANTHROPIC_API_KEY` | `x-api-key: <key>` | Direct-Anthropic credential |

MirAPI accepts both `x-api-key` and `Authorization: Bearer` on `/v1/messages`, so either works — but `ANTHROPIC_AUTH_TOKEN` is the credential variable Anthropic documents for bearer-authenticated gateways. If `ANTHROPIC_API_KEY` still holds a real Anthropic key from an earlier setup, Claude Code may prompt or fall back to Anthropic; set it to an empty string to avoid conflicts.

## Switching models

Change `ANTHROPIC_MODEL` to any model name from `GET /v1/models`:

```bash
export ANTHROPIC_MODEL="deepseek-chat"
```

- `GET /v1/models` returns the exact `id`/`name` to request, plus context length and pricing.
- Your key must be whitelisted for the model you choose; otherwise the request fails with **403**.
- Claude Code is optimized for Anthropic-style clients, so pick a model that supports tool calling for the best experience.

## Billing

- Conversations are billed per token (input / output / cache read, per 1M tokens USD). Reasoning and thinking tokens count as **output**.
- Claude Code draws from a prepaid balance: no subscription, no minimum, and the balance never expires.
- When the balance runs out, requests fail with **403**; topping up restores the same key immediately.
- Reconcile usage in the console billing log, `GET /api/usage/token` (account-level totals), or `GET /api/log/token` (per-request detail).

## Troubleshooting

- **Do not append `/v1`.** Anthropic clients build the full path themselves; `https://api.mirapi.ai/v1/messages` will not resolve.
- **`/v1/messages/count_tokens` is not implemented** and returns 404. Claude Code may call it during startup; the error is harmless and does not block requests.
- **401** means the key is missing, invalid, or unknown. Check that `ANTHROPIC_AUTH_TOKEN` is set and non-empty.
- **403** means the key is valid but the request was refused: balance exhausted, model not whitelisted, quota exceeded, or IP not allowed. Fix the cause and retry.
- **429** means rate limited. Retry with jittered exponential backoff (1s → 2s → 4s, capped around 30s); the gateway does not retry for you and sends no `Retry-After` header.
- Every error message ends with a request ID — include it when you open a support ticket.

## Related links

- [Quickstart](/docs/quickstart)
- [Models & pricing](/docs/models)
- [Billing & top-ups](/docs/billing)
- [Anthropic SDK](/docs/integrations/anthropic-sdk)
- [Claude Code advanced](/docs/tutorials/claude-code-advanced)
- [Authentication](/docs/api-reference/authentication)