---
name: hackrouter
description: Integrates hackrouter, an OpenAI- and Anthropic-compatible API for many language and image models paid from one prepaid balance. Use when the user wants to call models through hackrouter, switch an app's OpenAI or Anthropic client to hackrouter, pick a hackrouter model, or debug a hackrouter error, rate limit or charge.
---

# hackrouter

hackrouter serves many models through the APIs the user's code already uses. Only the base URL and the key change.

## Connect

| Client | Base URL |
|---|---|
| OpenAI SDKs and OpenAI-compatible tools | `https://api.hackrouter.ai/v1` |
| Anthropic SDKs | `https://api.hackrouter.ai` (the SDK adds `/v1`) |

- Read the key from the `HACKROUTER_KEY` environment variable. Never write a key into code or commit it. Keys start with `hr-` and are created in the console at https://hackrouter.ai/console/keys.
- Send it as `Authorization: Bearer $HACKROUTER_KEY` or `x-api-key: $HACKROUTER_KEY`.

## Pick a model

Do not guess model IDs. List them, with prices and the APIs each can be called through; no key is needed:

```bash
curl https://api.hackrouter.ai/v1/models
```

Each model's `endpoints` says which APIs it accepts: `openai_chat` (`POST /v1/chat/completions`), `openai_responses` (`POST /v1/responses`), `anthropic_messages` (`POST /v1/messages`), `openai_images` (`POST /v1/images/generations`). `prices` are US dollars per million tokens.

## Call it

OpenAI SDK, Python:

```python
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.hackrouter.ai/v1", api_key=os.environ["HACKROUTER_KEY"])
answer = client.chat.completions.create(
    model="MODEL_ID",  # from GET /v1/models
    messages=[{"role": "user", "content": "Hello"}],
)
```

OpenAI SDK, TypeScript:

```ts
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.hackrouter.ai/v1", apiKey: process.env.HACKROUTER_KEY });
```

Anthropic SDK, Python:

```python
import os
from anthropic import Anthropic

client = Anthropic(base_url="https://api.hackrouter.ai", api_key=os.environ["HACKROUTER_KEY"])
```

Claude Code: set `ANTHROPIC_BASE_URL=https://api.hackrouter.ai`, `ANTHROPIC_AUTH_TOKEN=$HACKROUTER_KEY` and `ANTHROPIC_MODEL=<model id>`.

## What differs from OpenAI and Anthropic

- Chat Completions and Messages are interchangeable: a model is translated when it does not speak the client's format. Features that cannot be translated are refused with `400 rejected_by_model`: `n` above 1, `logit_bias` and `logprobs` from Chat to Messages; structured outputs and built-in tools from Messages to Chat.
- Provider-run tools are refused: Anthropic server tools such as web search, OpenAI built-in tools, `web_search_options`.
- Responses are not stored: leave `previous_response_id`, `conversation` and `prompt` unset and `background` false.
- Request bodies are at most 20 MiB. Send large media with `POST /v1/files`, which returns an upload link and a URL to put in the request.

## Errors

Act on `error.code`, never on the message. None of these are charged.

| Code | Status | What to do |
|---|---|---|
| `invalid_request`, `body_not_json` | 400 | Fix the request |
| `rejected_by_model` | 400, 413, 422 | Change the request or the model |
| `invalid_api_key` | 401 | Check `HACKROUTER_KEY` |
| `insufficient_balance` | 402 | The user must top up at https://hackrouter.ai/console/billing |
| `key_limit_reached` | 402 | The key's monthly limit is reached |
| `model_not_found` | 404 | Use an ID from `GET /v1/models` that lists this API |
| `rate_limited` | 429 | Wait `retry-after` seconds, then retry |
| `internal_error`, `model_unavailable` | 500, 503 | Retry with backoff, or try another model |

Rate limits are per account; `x-ratelimit-remaining-requests` tells how many can be sent now.

## Costs

Every response has an `x-request-id`. `GET /v1/requests/{id}` returns its usage and charge a minute or two later. `POST /v1/estimates` prices a usage before sending anything. `GET /v1/account` returns the balance.

## More

The whole documentation as one file: https://hackrouter.ai/llms-full.txt. Index: https://hackrouter.ai/llms.txt.
