# Errors

> Draft

Errors share one format, and the `X-Request-Id` header is always set:

```json
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_parameter",
    "message": "references 要是非空的字符串数组",
    "param": "references",
    "request_id": "req_6a2f90d4e1b7c3a85d02f9e4"
  }
}
```

- `type` (string, required) — Error category. See below
- `code` (string, required) — The specific reason. Branch on this
- `message` (string, required) — Human-readable and may be in Chinese. Don’t branch on it
- `param` (string | null, required) — The parameter at fault, or `null`
- `request_id` (string, required) — Request ID, same as the `X-Request-Id` header

## Error category

| type | Status | Meaning |
| --- | --- | --- |
| `invalid_request_error` | `400` `404` `405` `413` | Something is wrong with the request |
| `authentication_error` | `401` | The key is missing, unrecognized or revoked |
| `permission_error` | `403` | The account is disabled, or the web origin isn’t allowed (MCP) |
| `billing_error` | `402` | Not enough allowance |
| `rate_limit_error` | `429` | Too many requests |
| `api_error` | `500` `502` | An error on our side or upstream |
| `unavailable` | `503` | Temporarily unavailable |

## Request and authentication

| Status | code | Cause | What to do |
| --- | --- | --- | --- |
| `400` | `missing_parameter` | A required parameter is missing. `param` names it. | Add the parameter. |
| `400` | `invalid_parameter` | A parameter has the wrong type or value. `param` names it. | Fix it to match the type and range in the reference. |
| `400` | `unknown_parameter` | The request has a query parameter or body field the endpoint doesn’t accept. | Remove it or check the spelling. |
| `400` | `invalid_json` | The request body isn’t a JSON object. | Send a single JSON object with `Content-Type: application/json`. |
| `400` | `url_blocked` | The URL can’t be fetched (for example, a private or local address). | Use a DOI, arXiv ID or title instead. |
| `404` | `not_found` | The endpoint or resource doesn’t exist. | Check the path and the `/v1` prefix. The MCP address is `/mcp`. |
| `405` | `method_not_allowed` | Wrong HTTP method. The `Allow` header lists the supported ones. | Use a method listed in `Allow`. |
| `413` | `too_many_references` | `references` has more than 20 items. | Split into batches of up to 20. |
| `413` | `body_too_large` | The request body is too large. | Send fewer or shorter items. |
| `401` | `missing_api_key` | The `Authorization` header is missing or isn’t `Bearer <key>`. | Add `Authorization: Bearer <API key>`. |
| `401` | `invalid_api_key` | The key is malformed or doesn’t exist. | Make sure you copied the whole key with no extra spaces. |
| `401` | `revoked_api_key` | The key has been revoked | Create a new key in your account. |
| `403` | `account_disabled` | The account is disabled. | Contact us with the `request_id`. |
| `403` | `origin_not_allowed` | An MCP request from a web page came from an origin that isn’t allowed. | Call from a CLI, a desktop client or your server. |
| `400` | `unsupported_protocol_version` | The `MCP-Protocol-Version` header isn’t a supported version | Update the client |

## Allowance and rate limits

| Status | code | Cause | What to do |
| --- | --- | --- | --- |
| `402` | `insufficient_balance` | This month’s trial allowance doesn’t cover the units this request needs. | Check `/v1/usage`, send fewer items or wait for next month’s reset. Word packs go on sale at launch. |
| `429` | `rate_limit_exceeded` | A rate limit was hit (per IP, per key or per account). `message` says which. | Wait the number of seconds in `Retry-After`, then retry. Queue requests if several processes share one key. |

## Server

| Status | code | Cause | What to do |
| --- | --- | --- | --- |
| `500` | `internal_error` | Something went wrong on our side. | Retry with backoff. If it keeps happening, contact us with the `request_id`. |
| `502` | `upstream_unavailable` | Crossref, OpenAlex and arXiv all failed to respond. | Retry later. You aren’t charged for this request. |
| `503` | `coming_soon` | The API isn’t open yet. Every `/v1` and `/mcp` call returns this for now. | [Join the waitlist](https://paperpurr.com/#api). We’ll announce the launch on Discord. |
| `503` | `maintenance` | Under maintenance. | Retry later. |
| `503` | `not_configured` | The service is temporarily unavailable. | Retry later. |
| `503` | `busy` | The service is busy. Billable calls also return this when usage can’t be recorded, and nothing is charged. | Retry with backoff. |

## Rewrite only

Available when rewriting opens:

| Status | code | Cause | What to do |
| --- | --- | --- | --- |
| `409` | `idempotency_conflict` | The same `Idempotency-Key` was sent with a different body. | Use a new key for each new job. |
| `413` | `text_too_long` | The text is over the per-request limit. | Split the text into parts. |
| `422` | `unprocessable_content` | The content can’t be processed. | Check that `text` isn’t blank or garbled. |

## What to retry

| Status | Retry? | How |
| --- | --- | --- |
| `429` | Yes | Wait `Retry-After` seconds |
| `500` `502` `503` (`busy`, `maintenance`, `not_configured`) | Yes | Exponential backoff: 1, 2, 4, 8 seconds, up to 5 tries |
| `503 coming_soon` | No | Not launched; retrying won’t help |
| `400` `401` `402` `403` `404` `413` | No | Fix the request or key first |

```python
import time
import requests

RETRY = {429, 500, 502, 503}


def call(method, url, **kw):
    for attempt in range(5):
        r = requests.request(method, url, timeout=60, **kw)
        if r.ok:
            return r.json()
        err = r.json().get("error", {})
        if r.status_code not in RETRY or err.get("code") == "coming_soon":
            raise RuntimeError(f"{err.get('code')}: {err.get('message')} ({err.get('request_id')})")
        time.sleep(int(r.headers.get("Retry-After", 2 ** attempt)))
    raise RuntimeError(f"still failing after retries ({err.get('request_id')})")
```

## Common mistakes

### `400`: `references` must be an array

Incorrect:

```
{ "references": "Smith, J. (2020). Title. Journal, 12(3), 45-67." }
```

Correct:

```
{ "references": ["Smith, J. (2020). Title. Journal, 12(3), 45-67."] }
```

### `401`: put `Bearer` before the key

Incorrect:

```
Authorization: pp_live_...
```

Correct:

```
Authorization: Bearer pp_live_...
```

### `413`: split batches over 20

Incorrect:

```
requests.post(url, headers=headers, json={"references": refs})
```

Correct:

```
for i in range(0, len(refs), 20):
    requests.post(url, headers=headers, json={"references": refs[i:i + 20]})
```

### `429`: wait for `Retry-After` before retrying

Incorrect:

```
while r.status_code == 429:
    r = requests.post(url, headers=headers, json=body)
```

Correct:

```
while r.status_code == 429:
    time.sleep(int(r.headers.get("Retry-After", "60")))
    r = requests.post(url, headers=headers, json=body)
```

## Contact us

If you keep getting `500` or results look wrong, find us on Discord with the `request_id`.

Next: [Rate limits and metering](https://paperpurr.com/en/docs/limits.md)
