Edited Sep 19, 10:20 AM by Jonas Weber
Status
Owner
Tags
Every API key has a request budget per minute. When you exceed it the API returns 429 Too Many Requests with a Retry-After header.
with-retry.tsts
1export async function withRetry<T>(fn: () => Promise<Response>, attempts = 4): Promise<Response> {2 for (let i = 0; i < attempts; i++) {3 const res = await fn()4 if (res.status !== 429) return res5 const wait = Number(res.headers.get("Retry-After") ?? 2 ** i)6 await new Promise((r) => setTimeout(r, wait * 1000))7 }8 throw new Error("Rate limit: retries exhausted")9}Batch endpoints count as one request regardless of how many records they carry.
Comments