# Errors

> Every error the check endpoint can return, what causes it, and what your code should do about it.

## Error format

An error answers with a `4xx` or `5xx` status and a JSON body with one `error` object:

```json
{
  "error": {
    "code": "rate_limited",
    "message": "Too many requests this second for this key. Retry in a second."
  }
}
```

- `code` is stable. Branch on it in code.
- `message` is written for the developer reading a log. It can change, and it is not meant for your users.

## Error codes

| Status | Code                    | Cause                                                                                                                                 | What to do                                                                                |
| ------ | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `400`  | `invalid_request`       | Neither `ip` nor `email` was sent, a value is not a string or is longer than 320 characters, or the `POST` body is not a JSON object. | Fix the request.                                                                          |
| `400`  | `invalid_ip`            | `ip` is not a single IPv4 or IPv6 address. Ports, CIDR ranges and zone ids are refused.                                               | Send a bare address, or leave `ip` out.                                                   |
| `400`  | `invalid_email`         | `email` has no `@`, or no domain with a dot after it.                                                                                 | Validate the address before you call.                                                     |
| `401`  | `missing_api_key`       | No key in `Authorization: Bearer` or `x-api-key`.                                                                                     | Send the key. See [Authentication](https://heimdal.codebrewerz.com/docs/authentication/).                                     |
| `401`  | `invalid_api_key`       | The key does not exist or was revoked.                                                                                                | Check the key, or create a new one.                                                       |
| `402`  | `subscription_required` | The account has no active subscription.                                                                                               | Start or renew the subscription in the dashboard.                                         |
| `413`  | `payload_too_large`     | The request body is larger than 16 KB.                                                                                                | Send only `ip` and `email`.                                                               |
| `429`  | `rate_limited`          | The key went over its per-second limit, or this address sent too many requests with a missing or invalid key.                         | Wait for the time in `Retry-After` and retry. See [Rate limits](https://heimdal.codebrewerz.com/docs/reference/rate-limits/). |
| `503`  | `lists_unavailable`     | Heimdal has just started and has not loaded its lists yet.                                                                            | Retry shortly. Let the signup through in the meantime.                                    |
| `503`  | `service_unavailable`   | Something failed on Heimdal's side, or it is restarting.                                                                              | Retry shortly (after `Retry-After`, when sent). Let the signup through in the meantime.   |

### Messages

These are the messages the API sends today. They read like this in your logs:

| Code                    | Message                                                                             |
| ----------------------- | ----------------------------------------------------------------------------------- |
| `invalid_request`       | Send `ip`, `email`, or both, as strings of at most 320 characters.                  |
| `invalid_ip`            | `ip` must be one IPv4 or IPv6 address, without a port or zone.                      |
| `invalid_email`         | `email` must be an address with a domain, like `name@example.com`.                  |
| `missing_api_key`       | Send your API key in the Authorization header: Bearer hd\_…                         |
| `invalid_api_key`       | This API key is not valid.                                                          |
| `subscription_required` | This account has no active subscription. Start or renew one in the dashboard.       |
| `rate_limited`          | Too many requests this second for this key. Retry in a second.                      |
| `rate_limited`          | Too many requests with a missing or invalid API key from this address. Retry later. |
| `payload_too_large`     | Request body too large.                                                             |
| `lists_unavailable`     | The lists are still loading. Retry shortly.                                         |
| `service_unavailable`   | The service is temporarily unavailable. Retry shortly.                              |

### Order of checks

A request is checked in this order, and the first failure is the answer:

1. API key (`401`, or `429` after too many bad keys from one address)
2. Subscription (`402`)
3. Rate limit for the key (`429`)
4. Input (`400`)
5. Lists loaded (`503`)

So a request with a bad key and a bad body answers `401`, and a request refused with `400` has already counted toward the rate limit.

### Handling errors

Do not make a signup depend on Heimdal being reachable. On a timeout, a network error, a `429` or any `5xx`, let the signup through and log what happened. A `401` or a `402` means your integration needs attention: let the signup through as well, and alert yourself. [Best practices](https://heimdal.codebrewerz.com/docs/guides/best-practices/#fail-open) shows the pattern.

## Limits

Request rates per key and per address are on [Rate limits](https://heimdal.codebrewerz.com/docs/reference/rate-limits/).

| Limit                         | Value                                                     |
| ----------------------------- | --------------------------------------------------------- |
| Monthly quota                 | None                                                      |
| Length of `ip`, `email`       | 320 characters                                            |
| Request body                  | 16 KB                                                     |
| Key and subscription changes  | Take effect within 60 seconds          |
| Access after a failed renewal | 3 days                                       |
| Custom lists                  | 20 lists, 50,000 entries per account |

### Subscription and grace period

The API answers while your subscription is active, the trial included. If a renewal payment fails, access continues for 3 days. After that every call answers `402 subscription_required` until the subscription is active again.

A change to your subscription or your keys can take up to 60 seconds to reach the API. That covers a new subscription, a cancellation that takes effect, and a revoked key.

## Degraded answers

Every check also reads your custom lists. If that read fails, Heimdal still answers `200` with every other signal and adds this header:

```http
Heimdal-Degraded: custom_lists
```

`custom.matched` is then empty and `custom_list` is missing from `reasons`, whether or not an entry would have matched. If your custom lists are essential to a decision, check for the header and treat the answer as incomplete.

## Caching

Every response carries `Cache-Control: no-store`. The Tor and proxy lists change every hour, and your custom lists change whenever you edit them, so do not keep an answer to reuse later. Check once, at the moment of the signup.
