# Identify an edition by its barcode

> Turn the barcode on a box into the exact edition and its game, one at a time or a hundred at once, and handle a barcode we do not know yet.

The barcode on the back of a box names an edition: its language, its
publisher, its print run. Scan it, and the API tells you which game it is
and which edition of it.

## One barcode

```sh
curl "https://api.thebgdb.org/v1/editions?ean=4002051694104"
```

`ean` takes the 13 digits of an EAN-13, or the 12 of a UPC-A (boxes sold in
North America). Spaces and dashes are ignored; the check digit must be
right, so a misread scan is refused (`invalid_ean`, 400) instead of
matching another box.

The answer gives the barcode as EAN-13 and every edition it is printed on,
each with its game:

```json
{
  "ean": "4002051694104",
  "data": [
    {
      "edition": {
        "id": "…",
        "slug": "de-kosmos-2015-k3x9q2",
        "name": null,
        "original": false,
        "language": "de",
        "publisher": { "slug": "kosmos", "name": "Kosmos" },
        "year": 2015,
        "ean": "4002051694104",
        "cover": { "image": "/v1/media/covers/….jpg", "thumbnail": "/v1/media/covers/…-w250.jpg" }
      },
      "game": { "id": "…", "slug": "catan", "name": "Catan" }
    }
  ]
}
```

The answer is shortened here; `game` is a full game summary, the same as in
search results. There is usually one result. A publisher sometimes reuses
a barcode for a reprint, and then you get each edition it is printed on.

A barcode can match in three ways: an edition's own barcode, the game's
barcode (which is its original edition's), or a barcode a source gave the
game. Each call counts as one game read against the weekly
[limits](https://docs.thebgdb.org/guides/limits-and-caching/).

### A barcode we do not know yet

`barcode_not_found` (404) means no edition in the catalog carries it yet.
Offer the person a name search instead ([Find a game despite
typos](https://docs.thebgdb.org/guides/find-a-game/)), and keep the barcode: the catalog grows
every day.

```ts
async function fromBarcode(scanned: string) {
const url = new URL("https://api.thebgdb.org/v1/editions");
url.searchParams.set("ean", scanned);
const response = await fetch(url);
const body = await response.json();
if (response.ok) return body.data; // [{ edition, game }, …]
if (body.code === "barcode_not_found") return []; // Ask for the name instead.
if (body.code === "invalid_ean") throw new Error("Scan it again");
throw new Error(body.code);
}
```

```python

def from_barcode(scanned):
    response = requests.get(
        "https://api.thebgdb.org/v1/editions", params={"ean": scanned}, timeout=10
    )
    body = response.json()
    if response.ok:
        return body["data"]  # [{"edition": ..., "game": ...}, ...]
    if body["code"] == "barcode_not_found":
        return []  # Ask for the name instead.
    raise RuntimeError(body["code"])
```

## The edition's own page

Each edition has a `slug`, its address under its game. Read everything we
hold about it (the front and back of its box, the languages of its rules,
its rulebooks, what differs from the original edition):

```sh
curl "https://api.thebgdb.org/v1/games/catan/editions/de-kosmos-2015-k3x9q2"
```

The same edition is shown to people at
`https://thebgdb.org/games/catan/editions/de-kosmos-2015-k3x9q2`.

## A hundred at once

On a paid plan, `POST /lookup` takes up to 100 barcodes and ids in one
request, for a shop's catalog or a collection import:

```sh
curl -X POST "https://api.thebgdb.org/v1/lookup" \
  -H "Authorization: Bearer bgdb_…" \
  -H "Content-Type: application/json" \
  -d '{ "eans": ["4002051694104", "0841333100667"], "ids": ["catan"] }'
```

Each result names what was sent (`ean` or `id`) and what it found
(`found`, a list of editions with their games); a barcode that cannot be
read has an `error`. Every item counts as a request and a game read. Without
a paid plan the answer is `paid_plan_required` (403): look barcodes up one
at a time with `GET /editions?ean=`.

:::tip[What to cache, and for how long]
A barcode keeps naming the same edition: keep a match for a month. Ask
again for a `barcode_not_found` after a week or so, when the person comes
back.
:::
