Developer guide · Python 3.10+

Compare supermarket prices with Python.

Start with a free CSV, then use the same approach with a barcode lookup. This example uses Python’s standard library and shows the date behind every price.

By Joe Keene · Published 2 December 2026 · Example code tested with Python 3.

1. Get the runnable example

Open the example repository on GitHub, or download compare_prices.py. No package installation is required.

git clone https://github.com/jpekeene/grocerystats-examples.git
cd grocerystats-examples
python3 compare_prices.py --sample

This downloads our dated 50-pack sample and groups rows by barcode. It uses no account or API credits. You can instead download the CSV and pass its filename:

python3 compare_prices.py --sample-file uk-supermarket-price-sample-2026-12-02.csv

2. Compare an exact pack through the API

Create a key in your Grocery Stats account. Set GROCERY_API_KEY in your environment using your shell or secret manager. Keep it on your server, outside source control and browser code.

python3 compare_prices.py --barcode 5740900402751

A successful lookup costs one credit across all returned shops. This example barcode identifies the 750 g Lurpak Slightly Salted Spreadable pack. Your response may contain one shop or several, with a separate observation date for each listing.

3. Check before ranking prices

The API returns pack_size_check. The example refuses to rank conflict or unknown results. It accepts agree, and displays a single_shop result without calling it a cross-shop comparison.

from decimal import Decimal

if body["pack_size_check"] not in ("agree", "single_shop"):
    raise ValueError("Review the pack sizes before comparing")

rows = sorted(body["data"],
              key=lambda row: Decimal(str(row["price_gbp"])))
for row in rows:
    print(row["retailer"], row["price_gbp"], row["observed_on"])

Keep GTINs as strings so leading zeroes survive. Use Decimal for money. Check dates rather than assuming every shop was observed at the same time. The script warns when observation dates differ.

4. Keep offers separate

The output ranks recorded shelf prices. It does not substitute a membership or multi-buy price. The API can return a separate offer price and available promotion details; check eligibility, dates and quantity conditions before calculating an alternative total.

Errors and credit use

  • 401: the API key was rejected.
  • 402: credits are exhausted.
  • 404: no active, priced barcode match was found.
  • 409: a retailer-scoped barcode is ambiguous.
  • 429: wait for the returned Retry-After interval. The example does not retry or spend more credits automatically.

Failed lookups use no credits. A successful search page, including an empty result, costs one credit. This CLI does not implement retailer-scoped barcode lookup; use the documented retailer parameter directly if you need it.

Where to go next

API documentation explains name search, listing-ID comparisons and available food labels. Coverage and methodology explains gaps and matching. For an AI assistant, use the MCP connection guide.

Prices are dated observations, not live quotes or local stock guarantees. The example source is MIT licensed; the free dataset has separate reuse terms.