Docs menu

Docs Tutorials / JavaScript: look up a barcode in recall notices

JavaScript: look up a barcode in recall notices

Send a UPC, EAN or GTIN and get the recall notices that list it, with the lot codes and best-by dates written beside it.

You need a free key (get one; it takes about a minute) in the environment variable RECALLDB_KEY. Keep the key on a server or your own machine, never in a web page.

The code

Node 18 or newer (built-in fetch). The same code runs in Deno and Bun.

barcode.mjs
// Look up a product barcode (UPC, EAN or GTIN) in official recall notices.
//   RECALLDB_KEY=rdb_... node barcode.mjs 036000291452      (Node 18 or newer)
const API = process.env.RECALLDB_API ?? 'https://recalldb.app'
const code = process.argv[2] ?? '036000291452'

const res = await fetch(`${API}/v1/barcode/${encodeURIComponent(code)}`, {
  headers: { Authorization: `Bearer ${process.env.RECALLDB_KEY}` },
})
const body = await res.json()
if (!res.ok) {
  // For example 400 bad_barcode (wrong check digit) or 429 (a limit was reached; the message says which).
  console.error(`${res.status} ${body.error.code}: ${body.error.message}`)
  process.exit(1)
}

if (body.data.length === 0) {
  console.log('No recall notice in RecallDB lists this barcode.')
  console.log('That is not a clean bill of health: notices often name products without barcodes. Check the brand and the agency too.')
}
for (const r of body.data) {
  console.log(`${r.date}  ${r.title}  (${r.issued_by})`)
  console.log(`  As written in the notice: ${r.official_text}`)
  // Lot codes, best-by dates and serials are the notice's own words. Compare them with your product yourself.
  if (r.lot_codes.length) console.log(`  Lot codes: ${r.lot_codes.join(', ')}`)
  if (r.best_by_dates.length) console.log(`  Best-by dates: ${r.best_by_dates.join(', ')}`)
  console.log(`  Official notice: ${r.source.official_url}`)
}
console.log(`${body.attribution.text} (${body.attribution.url})`)

Run it

Terminal
RECALLDB_KEY=rdb_your_key node barcode.mjs 036000291452

Things to get right

  • A number with a wrong check digit is refused with 400 bad_barcode: it is not a real barcode.
  • Lot codes, best-by dates and serials are copied from the notice. RecallDB never decides whether your item matches: show them and let the person compare.
  • No match is not a clean bill of health. Many notices name products without listing barcodes.

Reference: GET /v1/barcode/{code}.