跳到正文
DEV Community· ByteHarvester·· 3 小時前AI 評分43

用 20 行 JavaScript 比較 Tesco、Sainsbury's、Asda 和 Morrisons 的價格(另包括 Coles、Woolworths 和 ALDI)

Compare Tesco, Sainsbury's, Asda and Morrisons prices in 20 lines of JavaScript (plus Coles, Woolworths and ALDI)

AI 導讀

作者把 8 間超市、分佈於 3 個國家的商品價格整合至 API,並示範用約 20 行 JavaScript 比較 Tesco、Sainsbury's、Asda 和 Morrisons 的商品價格。API 提供免費 Basic 計劃,每月 50 次請求;亦可篩選特價商品,建立價格提示。

正文

Disclosure: I built the APIs in this post. Written with AI assistance.

Grocery prices are the prices people check most often. They are also among the hardest to get as data. Every chain has its own website, its own search, its own way of showing a multi-buy offer, and its own habit of changing all of it without warning. If you have ever started a price-comparison side project, you know where the time goes: not into your app, but into keeping a scraper per supermarket alive.

So I put 8 supermarkets in 3 countries behind one API, with one JSON shape:

Country Stores Extras
UK (GBP) Tesco, Sainsbury's, Asda, Morrisons Sainsbury's: barcode, availability, Nectar prices with start and end dates
Australia (AUD) Coles, Woolworths, ALDI barcode, ingredients and nutrition where the store publishes them
New Zealand (NZD) Woolworths NZ (formerly Countdown) barcode, club price, stock level

Every product comes back with name, price, was-price, offer text, unit price, pack size, category, image and link. Add onlySpecials=true and you get only the products on offer.

Try the UK Supermarket Prices API on RapidAPI: the free Basic plan gives you 50 requests a month, enough to build and test everything below.

1. One request

Subscribe to the free plan, copy your RapidAPI key, then:

curl "https://uk-supermarket-prices-tesco-sainsburys-asda-morrisons.p.rapidapi.com/grocery-uk/search?store=sainsburys&query=baked%20beans&limit=5" \
  -H "x-rapidapi-host: uk-supermarket-prices-tesco-sainsburys-asda-morrisons.p.rapidapi.com" \
  -H "x-rapidapi-key: YOUR_RAPIDAPI_KEY"

A real answer from 28 October 2026, shortened:

{
  "site": "sainsburys",
  "count": 2,
  "results": [
    {
      "name": "Heinz Baked Beans in a Rich Tomato Sauce 4 x 415g",
      "price": 3,
      "wasPrice": 3.9,
      "isOnSpecial": true,
      "isClubPrice": true,
      "promoEndDate": "2026-11-03T00:00:00Z",
      "unitPriceText": "£1.81/kg",
      "barcode": "5000157024886",
      "currency": "GBP",
      "url": "https://www.sainsburys.co.uk/gol-ui/product/heinz-baked-beans-in-a-rich-tomato-sauce-4-x-415g"
    }
  ]
}

Price, the old price, the Nectar flag, the date the offer ends, the unit price and the barcode, already parsed. No HTML, no selectors, no headless browser on your side.

2. Twenty lines: the cheapest shop for one product

Save this as compare.mjs and run it with Node 18 or newer:

const HOST = 'uk-supermarket-prices-tesco-sainsburys-asda-morrisons.p.rapidapi.com';
const KEY = process.env.RAPIDAPI_KEY;
const STORES = ['tesco', 'sainsburys', 'asda', 'morrisons'];

async function search(store, query) {
  const url = `https://${HOST}/grocery-uk/search?store=${store}&query=${encodeURIComponent(query)}&limit=5`;
  const res = await fetch(url, { headers: { 'x-rapidapi-host': HOST, 'x-rapidapi-key': KEY } });
  const { results = [] } = await res.json();
  return results
    .filter((p) => p.status === 'OK')
    .map((p) => ({ store, name: p.name, price: p.price, unit: p.unitPriceText ?? '', offer: p.isOnSpecial ? 'yes' : '' }));
}

const query = process.argv[2] ?? 'heinz baked beans';
const rows = (await Promise.all(STORES.map((s) => search(s, query)))).flat();
console.table(rows.sort((a, b) => a.price - b.price));
RAPIDAPI_KEY=your_key node compare.mjs "heinz baked beans 4 x 415g"

You get one table with every match from all four chains, cheapest first. This is what it printed on 28 October 2026 (first rows, about 8 seconds for all four chains):

store       name                                                price  unit      offer
asda        Heinz Beanz Baked Beans 415g                        1.38
tesco       Heinz Beanz Baked Beans 415g                        1.40
morrisons   Heinz Tinned Baked Beans 415g                       1.40   £3.37/kg  yes
sainsburys  Heinz Baked Beans in a Rich Tomato Sauce 4 x 415g   3.00   £1.81/kg  yes
morrisons   Heinz Baked Beans 4x415g                            3.00   £2.35/kg  yes

Same tin, four shops, one call each. One comparison is four requests, so the free plan covers about a dozen comparisons a month while you build. Tip: naming the brand and the pack size ("heinz baked beans 4 x 415g") gives the tightest matches, because supermarkets list dozens of near-identical products.

3. A deal alert in one parameter

Add &onlySpecials=true and only discounted products come back. Run that on a schedule (cron, a GitHub Action, a serverless function), keep the products you have not seen yet, and post them to Discord, Slack or e-mail. That is a working "price drop" bot for the products your users care about.

Australia and New Zealand: the same code

Change the host and the path, and the same loop works for Coles, Woolworths and ALDI (/grocery-au/search?store=coles|woolworths|aldi) or Woolworths New Zealand (/grocery-nz/search?store=woolworths-nz). The fields have the same names, so your table, your alerts and your database schema stay the same. ALDI answers come from the full ALDI catalog, refreshed daily, so they come back instantly, and each answer says when its prices were read.

Australian Supermarket Prices API (Coles, Woolworths, ALDI) · New Zealand Supermarket Prices API (Woolworths NZ)

What people build with it

  • Price comparison pages and apps: "where is my basket cheapest this week?"
  • Basket trackers and inflation dashboards: the same 30 products every day, a chart per chain.
  • Deal and price-drop alerts for a list of favourite products.
  • Recipe and meal-plan costing: the real price of a recipe at each chain.
  • AI shopping assistants: give your agent a tool that returns real, current shelf prices instead of guesses.

Good to know

  • Prices are the stores' online prices.
  • Tesco and Asda prices come from each chain's own Google Shopping listings (resellers are removed), so they have no barcode or stock flag. Sainsbury's, Morrisons, Coles, Woolworths and ALDI come with the richer fields listed above.
  • A search that finds nothing tells you so in a problems list, so your code can tell "no product" apart from "no answer".

Plans

The same plans on all three APIs:

  • Basic: free, 50 requests a month
  • Pro: $9.99 a month for 1,000 requests
  • Ultra: $29.99 a month for 3,000 requests
  • Mega: $99.99 a month for 10,000 requests

Start before the Christmas rush

Shoppers compare grocery prices hardest in the weeks before Christmas, and a price chart is only as good as the history behind it. If you want your tracker, dashboard or alert bot to show real trends in December, start collecting this week. The free plan is enough to wire everything up today.

Get your free key for the UK Supermarket Prices API: Tesco, Sainsbury's, Asda and Morrisons in one call, in about five minutes from now.

來源:DEV Community · dev.to