socialcrawl/
← All recipes

Poll often without paying for it

Cache hits cost nothing, so a tight polling loop is cheap. Force fresh data only when it matters.

TypeScript
const KEY = process.env.SOCIALCRAWL_API_KEY!;

async function poll(fresh = false) {
  const r = await fetch("http://datatrend.nl/v1/hackernews/top", {
    headers: {
      "x-api-key": KEY,
      // omit this and repeat calls are served from cache at 0 credits
      ...(fresh ? { "Cache-Control": "no-cache" } : {}),
    },
  });
  const body = await r.json();
  console.log(body.cached ? "cache hit — 0 credits" : `fresh — ${body.credits_used} credit`);
  return body.data.items;
}

await poll();       // fresh, costs 1
await poll();       // cached, costs 0
await poll(true);   // forced fresh, costs 1
You'll need a key. It takes ten seconds and starts with 100 credits. Get a key