# API errors and rate limits

What a failed hematite API request answers: the error codes, validation errors per field, rate limits per endpoint, their headers, and plan limits.

A request that fails answers a status of 400 or above, with JSON in the same shape as a success: a `code` to check in code, a `message` to read, and where there's more to say, `data` or `issues`.

```json title="404 Not Found"
{
  "code": "API_ERROR",
  "message": "Not Found"
}
```

## The codes

| Status | `code`                     | When                                                                                                                      |
| ------ | -------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| 400    | `VALIDATION_ERROR`         | The request doesn't match the endpoint's schema: a field missing, or of the wrong type                                    |
| 400    | `API_ERROR`                | The request is well formed but can't be done; the message says why ("Invalid email or password")                          |
| 401    | `UNAUTHORIZED`             | No token, or one that expired or was removed ([authentication](https://hematite.gg/docs/developers/authentication))                               |
| 401    | `REAUTH_REQUIRED`          | A change that asks you to confirm it's you first ([two-step sign-in](https://hematite.gg/docs/account/two-step-sign-in)); `data.methods` says how |
| 402    | `API_ERROR`                | Your plan's limit is reached: "Storage limit reached" on an upload                                                        |
| 403    | `API_ERROR`                | The token may not do this: someone else's image, or an upload token on anything but an upload                             |
| 403    | `OAUTH_INSUFFICIENT_SCOPE` | An app's token wasn't allowed the scope this needs; it's in `data.scope` ([Sign in with hematite](https://hematite.gg/docs/developers/oauth))     |
| 403    | `OAUTH_ROUTE_DENIED`       | No app's token may call this endpoint, whatever its scopes                                                                |
| 403    | `OAUTH_FIELD_DENIED`       | An app's token sent a field no app may send here (a profile's `email` or privacy settings); they're in `data.fields`      |
| 403    | `KEY_INSUFFICIENT_SCOPE`   | An API key wasn't made with the scope this needs; it's in `data.scope` ([API keys](https://hematite.gg/docs/developers/api-keys))                 |
| 403    | `KEY_ROUTE_DENIED`         | No API key may call this endpoint, whatever its scopes                                                                    |
| 403    | `KEY_FIELD_DENIED`         | An API key sent a field no key may send here; they're in `data.fields`                                                    |
| 404    | `API_ERROR`                | It doesn't exist                                                                                                          |
| 409    | `API_ERROR`                | It conflicts with what's there: a fourth upload token, a domain that's taken                                              |
| 429    | `RATE_LIMITED`             | Too many requests: wait and try again                                                                                     |
| 429    | `KEY_RATE_LIMITED`         | An API key past its own rate a minute; `Retry-After` says when it may go on                                               |
| 429    | `TOO_MANY_SIGN_INS`        | Ten wrong passwords for an address within 15 minutes; `data.retryAt` says when it can be tried again                      |
| 500    | `INTERNAL_ERROR`           | Something broke on hematite's side                                                                                        |

Where an endpoint has errors of its own with data, its reference entry lists them.

## Validation errors

A `VALIDATION_ERROR` says what was wrong, per part of the request, in `data`: `body`, `query`, `params`, `headers` or `files`, each a list of issues with the field's `path` and a `message`.

```json title="400 Bad Request"
{
  "code": "VALIDATION_ERROR",
  "message": "Request validation failed.",
  "data": {
    "body": [
      {
        "code": "invalid_type",
        "expected": "string",
        "path": ["title"],
        "message": "Invalid input: expected string, received number"
      }
    ]
  }
}
```

An upload that's too large or of a type hematite doesn't take is a validation error too, under `files`.

## Rate limits

Each endpoint counts requests per address in a fixed window: 120 a minute unless its reference entry says otherwise. Endpoints that count something, like downloads, allow fewer, and uploading takes 60 a minute. To send more, wait for the next window when a `429` says to, as hematite's own upload page does.

Every answer says where you stand, in three headers:

| Header                | What it says                                           |
| --------------------- | ------------------------------------------------------ |
| `RateLimit-Limit`     | How many requests the window allows                    |
| `RateLimit-Remaining` | How many are left in it                                |
| `RateLimit-Reset`     | When it starts over, in seconds since 1970 (Unix time) |

Past the limit, the answer is `429` with the code `RATE_LIMITED` and a `Retry-After` header: the seconds to wait. A page on another site can read all four headers too: the API exposes them to browsers.

```json title="429 Too Many Requests"
{
  "code": "RATE_LIMITED",
  "message": "Too many requests. Please slow down and try again."
}
```

## Plan limits

How much you can store and how many folders you can have depend on your plan: see [pricing](https://hematite.gg/pricing). An upload past your storage answers `402` with how much you use and how much you have, in `data.storageUsed` and `data.storageLimit`, in bytes.
