2 min read
Timeout every call
async function fetchWithTimeout(url, init = {}, ms = 8000) {
const ac = new AbortController();
const t = setTimeout(() => ac.abort(), ms);
try {
return await fetch(url, { ...init, signal: ac.signal });
} finally {
clearTimeout(t);
}
}
Retry only what's safe
async function resilient(url, init) {
for (let attempt = 0; attempt < 4; attempt++) {
try {
const res = await fetchWithTimeout(url, init);
if (res.status < 500 && res.status !== 429) return res;
} catch (e) {
if (e.name !== 'AbortError' && attempt === 3) throw e;
}
await new Promise(r => setTimeout(r, 2 ** attempt * 200 + Math.random() * 200));
}
throw new Error('exhausted retries');
}Retry 429 and 5xx and network aborts; never retry a 4xx other than 429 — a 400 will fail identically no matter how many times you send it. Pair retries with an idempotency key so a retried POST is safe.
Frequently asked questions
What timeout should I set?
Public reads are fast; 5–8 seconds is generous. Set it low enough that a hung request fails fast and your own request cycle stays responsive.
Is it safe to retry a POST?
Only with an idempotency key. Without one, a retried POST could create a duplicate. With one, the API returns the original result.
Related reading

Quickstart: use idempotency keys on write requests
An Idempotency-Key header makes a POST safe to retry. Generate one UUID per logical operation, send it with…
Read →
Quickstart: handle rate limits on the public API
The public ring is rate-limited, and a 429 tells you exactly when to try again. Read the RateLimit-Remaining…
Read →
Quickstart: handle Credicorp API error responses
Every Credicorp API error uses the same envelope: { error: { type, code, message, request_id } }. Branch on…
Read →
Quickstart: call the Credicorp public API from Node.js
Node 18+ ships a global fetch, so you can call the Credicorp public API with zero dependencies. This…
Read →Funding for UK limited companies
Credicorp lends to your company, not to you personally — short-term working capital with no personal guarantee. See what your business could access.