const baseUrl = (process.env.PUBLIC_API_BASE_URL || "https://api.compudime.com").replace(/\/$/, ""); const apiKey = process.env.PUBLIC_API_KEY; if (!apiKey) { throw new Error("Set PUBLIC_API_KEY to before running this example."); } async function request(path, requestId) { const response = await fetch(`${baseUrl}${path}`, { headers: { Authorization: `Bearer ${apiKey}`, "X-Request-Id": requestId, }, }); if (!response.ok) { const problem = await response.json(); throw new Error(`Public API problem JSON ${problem.code} (${problem.requestId}): ${problem.detail}`); } return response.json(); } const fields = encodeURIComponent("productCode,description,price"); const firstPage = await request(`/v1/products?fields=${fields}&limit=50`, "js-products-page-1"); console.table(firstPage.products); if (firstPage.nextCursor) { const cursor = encodeURIComponent(firstPage.nextCursor); const secondPage = await request(`/v1/products?fields=${fields}&limit=50&cursor=${cursor}`, "js-products-page-2"); console.log(`Fetched ${secondPage.products.length} products from the nextCursor page.`); } const lookup = await request("/v1/products/SKU-100?fields=productCode,description,price", "js-product-lookup"); console.log(lookup.product ?? lookup);