# Anthropic

> Point Anthropic Messages clients at AnyRouter — change the base URL and key, keep the anthropic-version header.


# Migrate from Anthropic

Repoint your Anthropic Messages client at AnyRouter by changing the base URL and API key. Your `messages.create` calls keep the native Anthropic body shape, and the `anthropic-version` header stays required. This guide is for teams already on the Anthropic API who want unified billing and access to every Claude model through one key.

## Before you start

- An AnyRouter API key (prefixed `sk-ar-`). Create one in the [dashboard](https://anyrouter.dev/dashboard/keys).
- An existing Anthropic Messages client (`@anthropic-ai/sdk` or raw HTTP).

:::warning
The SDK appends `/v1/messages` to the base URL, so the AnyRouter base URL must stop at `/api` — not `/api/v1`. Setting it to `/api/v1` produces a double `/v1` and a 404.
:::

## What changes

| Setting        | Before (Anthropic)              | After (AnyRouter)                                  |
| -------------- | ------------------------------- | -------------------------------------------------- |
| Base URL       | `https://api.anthropic.com`     | `https://anyrouter.dev/api`                        |
| API key        | `ANTHROPIC_API_KEY`             | Your AnyRouter key, prefixed `sk-ar-`              |
| Version header | `anthropic-version: 2023-06-01` | Same. Required.                                    |
| Model          | `claude-sonnet-4-5`             | `anthropic/claude-sonnet-4-6` or any `anthropic/*` |

## Before and after

Only the base URL, key, and model id change — the request body is native Anthropic.

::::tabs

:::tab{label="Before (Anthropic)"}
```typescript
import Anthropic from "@anthropic-ai/sdk"

const client = new Anthropic({
  baseURL: "https://api.anthropic.com",
  apiKey: process.env.ANTHROPIC_API_KEY,
})

const message = await client.messages.create({
  model: "claude-sonnet-4-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Review this incident summary." }],
})
```
:::

:::tab{label="After (AnyRouter)"}
```typescript
import Anthropic from "@anthropic-ai/sdk"

const client = new Anthropic({
  baseURL: "https://anyrouter.dev/api",
  apiKey: process.env.ANYROUTER_API_KEY,
})

const message = await client.messages.create({
  model: "anthropic/claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Review this incident summary." }],
})
```
:::

::::

## Migrate

::::steps

:::step{title="Point the SDK at AnyRouter"}
Set the base URL to `https://anyrouter.dev/api` (stopping at `/api`) and the API key to your AnyRouter key.
:::

:::step{title="Keep the version header"}
The `anthropic-version: 2023-06-01` header is still required. Leave it as-is.
:::

:::step{title="Update model ids to the anthropic/ form"}
Rewrite your model ids to include the owner:

- `claude-sonnet-4-5` → `anthropic/claude-sonnet-4-6`
- `claude-haiku` → `anthropic/claude-haiku-4-5`
:::

::::

## Verify

Send one request against the Messages endpoint with your new key:

```bash
curl https://anyrouter.dev/api/v1/messages \
  -H "x-api-key: sk-ar-your-key" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-haiku-4-5",
    "max_tokens": 512,
    "messages": [{"role": "user", "content": "Say hello in one sentence."}]
  }'
```

The Messages endpoint accepts native Anthropic request bodies — same shape, same fields. For cross-provider routing instead, use [Chat Completions](/api-reference/chat-completions) or [Responses](/api-reference/responses).

## Migrate with an agent prompt

Paste this into a coding agent (Claude Code, Cursor, Codex, …) to repoint the Anthropic client across the project:

```text
Migrate this project from the Anthropic API to AnyRouter.

1. Change the Anthropic SDK base URL to https://anyrouter.dev/api
   (the SDK appends /v1/messages — the base URL must stop at /api).
2. Swap ANTHROPIC_API_KEY for ANYROUTER_API_KEY (prefix sk-ar-). Never hardcode it.
3. Keep the `anthropic-version: 2023-06-01` header — it is still required.
4. Rewrite model ids to the anthropic/ form:
     claude-sonnet-4-5 → anthropic/claude-sonnet-4-6
     claude-haiku      → anthropic/claude-haiku-4-5
5. messages.create calls stay unchanged — native Anthropic body shape.
6. Keep streaming and error handling exactly as they are.
7. Add a smoke test that sends one short prompt and prints the reply.

Only swap the provider config — do not change UI or business logic.
```

## Troubleshooting

:::details{summary="I get a 404 on every request"}
The base URL must stop at `/api`, not `/api/v1`. The SDK appends `/v1/messages` itself, so `/api/v1` becomes `/api/v1/v1/messages`. Set `baseURL` to `https://anyrouter.dev/api`.
:::

:::details{summary="I get a 401 Unauthorized"}
Anthropic clients send the key as `x-api-key`. Confirm your AnyRouter key (prefixed `sk-ar-`) is set there, and that the `anthropic-version` header is present.
:::

## Related

- [Messages API](/api-reference/messages) — native Anthropic endpoint reference.
- [Claude Code](/guides/claude-code) — route the Claude Code CLI through AnyRouter.
- [Models API](/api-reference/models) — list every available model.
