> ## 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.

# Find deals

> Page through your channel's catalog and list the products on sale.

An offer is on sale when the merchant gives an `old_price` above `price`. To build a deals page,
page through every product of the channel and keep those offers.

## Page through the catalog

`GET /v0/channels/{channelId}/products` returns up to `limit` products (at most 200) ordered by
GTIN. Pass `next_cursor` from a response as `cursor` to get the next page; it is `null` on the last one.

```bash theme={null}
curl "https://api.aggrefine.com/v0/channels/$CHANNEL_ID/products?limit=200" \
  -H "Authorization: Bearer $AGGREFINE_API_KEY"
```

```json theme={null}
{
  "items": [{ "gtin": "00012345678905", "title": "…", "offers": [ … ] }],
  "next_cursor": "00012345678905"
}
```

## Collect the discounts

```js theme={null}
const headers = { Authorization: `Bearer ${process.env.AGGREFINE_API_KEY}` };
const deals = [];
let cursor = null;

do {
  const url = new URL(`https://api.aggrefine.com/v0/channels/${channelId}/products`);
  url.searchParams.set("limit", "200");
  if (cursor) url.searchParams.set("cursor", cursor);

  const page = await (await fetch(url, { headers })).json();
  for (const p of page.items) {
    for (const o of p.offers) {
      const price = Number(o.price);
      const old = Number(o.old_price);
      if (o.availability === "in_stock" && old > price) {
        deals.push({
          title: p.title,
          shop: o.merchant.name,
          price: o.price,
          old_price: o.old_price,
          discount: Math.round((1 - price / old) * 100),
          link: o.affiliate_url,
        });
      }
    }
  }
  cursor = page.next_cursor;
} while (cursor);

deals.sort((a, b) => b.discount - a.discount);
```

Show the top of `deals` as "−25% at Nordic Pet", linking to `link`.

## Run it on a schedule

Walking the whole catalog takes one request per 200 products. Run it from a scheduled job (every
hour is plenty), store the result, and serve the deals page from that, not from the API on every
view.


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