# Find a game despite typos

> Search games by name in any language, with misspellings corrected, and offer suggestions while someone types.

People type game names badly, in their own language, and with the
punctuation left out. Two endpoints cover it:

|         | `GET /search`                                                  | `GET /suggest`                           |
| ------- | -------------------------------------------------------------- | ---------------------------------------- |
| For     | a results page                                                 | a search box, at every key press         |
| Returns | games (and, with `type=all`, people and publishers in `terms`) | games and up to 3 people or publishers   |
| Paging  | `page` or `cursor`                                             | none: one short list                     |
| Answer  | full `GameSummary` objects                                     | a slimmer `Suggestion`, with `thumbnail` |

## Search

```sh
curl "https://api.thebgdb.org/v1/search?q=brass%20birmigham"
```

- **Every word, any order, any name.** A game matches when one of its names
  (the main one or an alternate one, in any language) contains every word of
  `q`. Case, accents and punctuation are ignored: `flugelschlag` finds
  Flügelschlag, `catan!` finds Catan.
- **Misspellings.** When fewer than `limit` games match as written, games
  with a close name are added after them, each marked `approximate: true`.
  `cattan` finds Catan, `wingspn` finds Wingspan. Show these as "Did you
  mean…" if your interface tells them apart.
- **Which name matched.** `matchedName` says which alternate name was found
  when it was not the main one, so you can show "Die Siedler von Catan (Catan)".
- **Order.** A name equal to the query first, then names starting with it,
  then a word starting with it, then names containing it; within each, the
  better-known games first.

### Names in the reader's language

Send `lang` (a BCP 47 code such as `fr`, `ja` or `pt-BR`):

```sh
curl "https://api.thebgdb.org/v1/search?q=aventuriers&lang=fr"
```

A game with a name in that language comes back with it as `name`, and its
main name as `originalName`. A regional code falls back on its language
(`fr-CA` on `fr`).

### Pages

`limit` sets the page size. Go on with the `nextCursor` of the previous
answer as `cursor`, or ask for a numbered `page`:

```sh
curl "https://api.thebgdb.org/v1/search?q=catan&limit=24&page=2"
```

Send one of `page` or `cursor`, not both (`invalid_page`). A page past the
last is refused with `page_out_of_range`.

### People and publishers

`type=people` finds designers and artists, `type=publishers` publishers,
and `type=all` games plus up to 10 people and publishers in `terms` on the
first page:

```sh
curl "https://api.thebgdb.org/v1/search?q=feld&type=people"
```

## Suggestions for a search box

`GET /suggest` answers in a few milliseconds from the search index, with the
same matching and order:

```sh
curl "https://api.thebgdb.org/v1/suggest?q=wings&lang=en"
```

Each game carries its `thumbnail` (our small copy of the cover), so the list
can show covers at once. Designers, artists and publishers come back with
their `kind`. Call it as the person types, after a short pause (150 to 250
ms) so a fast typist does not send a request per letter.

## In code

```ts
async function findGames(query: string, lang = "en") {
const url = new URL("https://api.thebgdb.org/v1/search");
url.searchParams.set("q", query);
url.searchParams.set("lang", lang);
const response = await fetch(url);
if (!response.ok) throw new Error((await response.json()).code);
const { data } = await response.json();
return data.map((game) => ({
slug: game.slug,
name: game.name,
guessed: game.approximate === true,
}));
}
```

```python

def find_games(query, lang="en"):
    response = requests.get(
        "https://api.thebgdb.org/v1/search",
        params={"q": query, "lang": lang},
        timeout=10,
    )
    body = response.json()
    if not response.ok:
        raise RuntimeError(body["code"])
    return [(g["slug"], g["name"], g.get("approximate", False)) for g in body["data"]]
```

:::tip[What to cache, and for how long]
Search and suggestion answers are cached by us for everyone until the
catalog changes, and a cached answer does not count against your limits.
On your side, keep a query's results for a few minutes; keep the `id` of the game someone picked for good: `GET /games/{id}` also
accepts a slug the game had before, or the id of an entry merged into it,
and answers with the game under its current address.
:::
