# Limits, caching and terms

> The request limits as numbers, the headers on every answer, what to do on a 429, and what to cache for how long.

Reading needs no key. Limits keep the catalog fast for everyone; they are
counted per address without a token, and per member with one.

## The limits

|                   | Without a token                      | Free API token                    | Starter plan            |
| ----------------- | ------------------------------------ | --------------------------------- | ----------------------- |
| Requests a minute | 60                | 90              | 1,200      |
| Requests a day    | 2,000                   | 3,000                 | no daily limit          |
| Requests a week   | 5,000                  | 7,500                | 200,000        |
| Games read a week | 500           | 750         | 40,000 |
| Counted per       | address (an IPv6 address by its /64) | member, all their tokens together | member                  |

"Games read" counts `GET /games/{id}` and the routes under it. Days are UTC
days; weeks start on Monday at 00:00 UTC.

- **Free API token**: a member whose email address is confirmed creates up
  to 2 on their account page at thebgdb.org, and sends
  one as `Authorization: Bearer bgdb_…`.
- **Starter plan**: for commercial use, €49.99 a month, from
  [thebgdb.org/developers](https://thebgdb.org/developers).
- **More**: larger volumes, data dumps and service levels on request at
  contact@thebgdb.org.

The same numbers, as JSON, with your own limits when you send a token:

```sh
curl "https://api.thebgdb.org/v1/limits"
```

## The headers

Every counted answer carries:

| Header                                                                                 | Meaning                                                                     |
| -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| `X-RateLimit-Limit`                                                                    | Your daily limit (the weekly one on a paid plan, which has no daily limit). |
| `X-RateLimit-Remaining`                                                                | What is left of it.                                                         |
| `X-RateLimit-Reset`                                                                    | When it starts again, in seconds since 1970.                                |
| `x-ratelimit-weekly-limit`, `x-ratelimit-weekly-remaining`, `x-ratelimit-weekly-reset` | The same for the week.                                                      |
| `x-ratelimit-weekly-detail-*`                                                          | The same for the games read this week, on `GET /games/{id}`.                |

## When you are refused

Beyond a limit the API answers `429` with a `retry-after` header (seconds to
wait) and the code `rate_limited` (a minute's or a day's limit) or
`weekly_limit` (a week's):

```ts
async function get(url: string): Promise<Response> {
for (let attempt = 0; attempt < 3; attempt++) {
const response = await fetch(url);
if (response.status !== 429) return response;
const wait = Number(response.headers.get("retry-after") ?? 60);
const { code } = await response.json();
if (code === "weekly_limit" || wait > 120) return response; // Not worth waiting for.
await new Promise((resolve) => setTimeout(resolve, wait * 1000));
}
return fetch(url);
}
```

```python

def get(url, **params):
    for _ in range(3):
        response = requests.get(url, params=params, timeout=10)
        if response.status_code != 429:
            return response
        wait = int(response.headers.get("retry-after", "60"))
        if response.json()["code"] == "weekly_limit" or wait > 120:
            return response
        time.sleep(wait)
    return requests.get(url, params=params, timeout=10)
```

## Caching

- **Catalog answers** are cached by us for up to five minutes, so a change
  can take that long to show. Search and suggestion answers are cached for
  everyone until the catalog changes, and a cached answer is not counted
  against your limits.
- **Images** never change at a given address: keep them as long as you like.
- **On your side**, keep a game you show for a day before reading it again;
  keep search results for a few minutes; keep the `id` of a game for good.
- **Do not** walk the whole catalog game by game: `GET /games` lists games
  a page at a time with their summaries, and `GET /index/games` gives names
  and slugs by the hundred.

## Terms

Using the API and what it returns is subject to the
[terms](https://thebgdb.org/terms#api): the licence of the data and texts
is CC BY-NC 4.0 ([details](https://thebgdb.org/terms#license)): credit us as
**Data from thebgdb.org** with a link to https://thebgdb.org (or to the
game's page). Commercial use (a product, a service, an app or a site that
earns money) needs the Starter plan, or for larger volumes an agreement on
request.
