> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aggrefine.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Compare prices

> Show the cheapest shop for each product in a list, and how much it saves.

Say you have a "best dog foods" page with ten products. For each one you want the lowest price,
the shop that has it, and how much cheaper it is than the most expensive shop.

## Fetch each product

Look up every GTIN on the page. The calls are independent, so run them in parallel:

```js theme={null}
const headers = { Authorization: `Bearer ${process.env.AGGREFINE_API_KEY}` };

async function getProduct(gtin) {
  const res = await fetch(
    `https://api.aggrefine.com/v0/channels/${channelId}/products/${gtin}`,
    { headers },
  );
  return res.ok ? res.json() : null; // 404: no approved shop sells it
}

const products = (await Promise.all(gtins.map(getProduct))).filter(Boolean);
```

## Pick the cheapest offer

Compare only offers in the same currency and in stock:

```js theme={null}
const rows = products.map((p) => {
  const offers = p.offers
    .filter((o) => o.availability === "in_stock" && o.currency === "SEK")
    .sort((a, b) => Number(a.price) - Number(b.price));
  if (offers.length === 0) return null;

  const cheapest = offers[0];
  const highest = offers.at(-1);
  return {
    title: p.title,
    image: p.images[0]?.url,
    shop: cheapest.merchant.name,
    price: cheapest.price,
    link: cheapest.affiliate_url,
    shops: offers.length,
    saving: (Number(highest.price) - Number(cheapest.price)).toFixed(2),
  };
}).filter(Boolean);
```

Show `shops` as "from 1 279 kr at Nordic Pet, 3 shops" and `saving` as "save up to 70 kr".

## Keep it fast

* Cache each product for five minutes; prices don't change faster than the merchants' feeds.
* For a large list, render from your own cache and refresh it in the background, rather than
  calling the API on every page view.
* Prices are decimal strings. `Number()` is fine for sorting and display; use a decimal library if
  you add them up.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.