# Read a Barcode Price History run through the Apify API

Start with the barcode quick start in the Actor, then copy the ID of your completed run. Set `APIFY_RUN_ID` to that ID and `APIFY_TOKEN` to your own Apify API token in your local environment. These examples read the completed run and its dataset. They do not start a report or trigger another report event. Apify can meter dataset reads.

The examples return up to 80 observations from the run. Each observation keeps its exact barcode, retailer domain, date, currency, price and Open Prices attribution. For a coverage check, also read the run's `OUTPUT` record. Check whether each requested barcode was read successfully and whether the report capped the results.

## Python

```python
import json
import os
from urllib.parse import quote
from urllib.request import Request, urlopen

base = "https://api.apify.com/v2"
headers = {"Authorization": f"Bearer {os.environ['APIFY_TOKEN']}"}

def get_json(path):
    with urlopen(Request(f"{base}/{path}", headers=headers), timeout=15) as response:
        return json.load(response)

run_id = quote(os.environ["APIFY_RUN_ID"], safe="")
run = get_json(f"actor-runs/{run_id}")["data"]
if run["status"] != "SUCCEEDED":
    raise SystemExit(f"Run status is {run['status']}. Review its log or coverage.")
dataset_id = quote(run["defaultDatasetId"], safe="")
rows = get_json(f"datasets/{dataset_id}/items?format=json&clean=true&limit=80")
print(json.dumps({"observations": len(rows), "firstBarcode": rows[0]["gtin"] if rows else None}))
```

## JavaScript

Save as an ES module and run it using a Node.js release with built-in `fetch`.

```javascript
const base = "https://api.apify.com/v2";
const { APIFY_TOKEN, APIFY_RUN_ID } = process.env;
if (!APIFY_TOKEN || !APIFY_RUN_ID) throw new Error("Set APIFY_TOKEN and APIFY_RUN_ID.");

async function getJson(path) {
    const response = await fetch(`${base}/${path}`, {
        headers: { Authorization: `Bearer ${APIFY_TOKEN}` },
        signal: AbortSignal.timeout(15000),
    });
    if (!response.ok) throw new Error(`Apify returned HTTP ${response.status}.`);
    return response.json();
}

const { data: run } = await getJson(`actor-runs/${encodeURIComponent(APIFY_RUN_ID)}`);
if (run.status !== "SUCCEEDED") throw new Error(`Run status is ${run.status}. Review its log or coverage.`);
const id = encodeURIComponent(run.defaultDatasetId);
const rows = await getJson(`datasets/${id}/items?format=json&clean=true&limit=80`);
console.log(JSON.stringify({ observations: rows.length, firstBarcode: rows[0]?.gtin ?? null }));
```

## cURL

In a POSIX-compatible shell, read the run metadata first. Use the returned `defaultDatasetId` to request the dataset items in the same way as the Python and JavaScript examples. The bearer header is passed to cURL on standard input.

```sh
printf 'header = "Authorization: Bearer %s"\n' "$APIFY_TOKEN" |
  curl --config - --fail-with-body --silent --show-error --max-time 15 \
  "https://api.apify.com/v2/actor-runs/$APIFY_RUN_ID"
```

## Postman

Import the accompanying collection and set `apifyToken` and `runId` in a private environment. Run the two requests in order. The first request records the dataset ID for the second request. Keep the token out of shared workspaces and collection exports.

If Apify returns HTTP 401 or 403, check that the token belongs to an account with access to that run. If it returns HTTP 404, check the run ID and whether the results are still retained. An empty dataset has zero observations. Read the coverage record to see whether the source was read successfully.

- [Apify run endpoint](https://docs.apify.com/api/v2/actor-run-get)
- [Apify dataset exports](https://docs.apify.com/storage/dataset)
