socialcrawl/
← All recipes

Sync a batch safely, retries and all

An Idempotency-Key makes a retry free: the first response replays and no credits move.

TypeScript
const KEY = process.env.SOCIALCRAWL_API_KEY!;

async function fetchOnce(handle: string, runId: string) {
  for (let attempt = 1; attempt <= 3; attempt++) {
    const r = await fetch(`http://datatrend.nl/v1/github/user?handle=${handle}`, {
      headers: {
        "x-api-key": KEY,
        // same key on every retry -> charged exactly once
        "Idempotency-Key": `${runId}:${handle}`,
      },
    });
    if (r.ok) return r.json();
    if (r.status === 429) { await new Promise((s) => setTimeout(s, 1000 * attempt)); continue; }
    if (r.status >= 500) continue; // credits were refunded automatically
    throw new Error(`${r.status}`);
  }
}

const runId = crypto.randomUUID();
const users = await Promise.all(["torvalds", "rasmus"].map((h) => fetchOnce(h, runId)));
console.log(users.map((u: any) => u.data.followers));
You'll need a key. It takes ten seconds and starts with 100 credits. Get a key