JavaScript
// sync.js: runs on a server (Node 18 or later)
const SITE = process.env.COREDNA_URL; // https://www.example.com
const KEY = process.env.COREDNA_API_KEY;
const LIMIT = 250;
async function getJson(url) {
for (let attempt = 1; ; attempt++) {
const response = await fetch(url, { headers: { Authorization: 'Bearer ' + KEY } });
if (response.ok) return response.json();
if (attempt === 3 || response.status < 500) {
throw new Error(url + ': HTTP ' + response.status + ' ' + (response.headers.get('x-reason') || ''));
}
await new Promise(r => setTimeout(r, 2000 * attempt));
}
}
// Every product variant with an id above sinceId, LIMIT at a time
export async function* variantsSince(sinceId = 0) {
let lastId = sinceId;
for (;;) {
const url = new URL(SITE + '/cdna-api/products/product-variants');
url.searchParams.set('properties', '{ id, code, name, finalPrice, availableQuantity }');
url.searchParams.set('orderBy', 'id:ASC');
url.searchParams.set('limit', LIMIT);
url.searchParams.set('>id', lastId);
const page = await getJson(url);
for (const variant of page.data) {
yield variant;
lastId = variant.id;
}
if (page.data.length < LIMIT) return;
}
}
// usage: resume from the id stored by the previous run
let highest = Number(process.env.LAST_SYNCED_ID || 0);
for await (const variant of variantsSince(highest)) {
await upsertIntoWarehouse(variant); // your code
highest = variant.id;
}
console.log('synced up to id', highest);
The request each loop sends, readable:
HTTP
GET /cdna-api/products/product-variants?properties={ id, code, name, finalPrice, availableQuantity }&orderBy=id:ASC&limit=250&%3Eid=4180
Authorization: Bearer YOUR_ACCESS_TOKEN