-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1.5 KB
/
index.js
File metadata and controls
43 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// retailerapi quickstart — Node 20+
//
// Look up a product, print title + cheapest price across retailers.
// Run: RETAILERAPI_KEY=rk_live_… node index.js
const KEY = process.env.RETAILERAPI_KEY;
if (!KEY) {
console.error('RETAILERAPI_KEY env var required. Get one at https://app.retailerapi.com/signup');
process.exit(1);
}
const id = process.argv[2] ?? '19667262713';
async function main() {
const r = await fetch(`https://api.retailerapi.com/v1/products/${id}?include_cross_retailer=true`, {
headers: { Authorization: `Bearer ${KEY}` },
});
if (!r.ok) {
const body = await r.text();
throw new Error(`${r.status}: ${body}`);
}
const p = await r.json();
console.log(`${p.title}`);
console.log(` brand: ${p.brand ?? '—'}`);
console.log(` walmart: $${p.current_price?.toFixed(2) ?? '—'} (${p.walmart_url})`);
// Cheapest non-Walmart cell where status === 'ok'
const others = (p.cross_retailer ?? []).filter((c) => c.status === 'ok' && typeof c.price === 'number');
if (others.length) {
const cheapest = others.reduce((min, c) => (c.price < min.price ? c : min), { price: Infinity });
console.log(` cheapest off-Walmart: $${cheapest.price.toFixed(2)} at ${cheapest.retailer}`);
} else {
console.log(` cross-retailer: indexing — call again in a few seconds`);
}
console.log(`\ntokens_consumed: ${p.tokens_consumed} · tokens_remaining: ${p.tokens_remaining}`);
}
main().catch((e) => {
console.error('ERR:', e.message);
process.exit(1);
});