Read the response headers
Retry-Aftertells you how long to wait, either as a number of seconds (Retry-After: 30) or as a date. Always respect it when it's there.- Many APIs also send the limit and what's left of it, under names such as
X-RateLimit-Limit,X-RateLimit-RemainingandX-RateLimit-Reset, or the newerRateLimitandRateLimit-Policyheaders. Check the API's documentation for which ones it uses.
Retry with exponential backoff
Wait longer after each failed attempt, and add random jitter so many clients don't retry at the same moment:
async function fetchWithRetry(url, options = {}, attempts = 5) {
for (let attempt = 0; ; attempt++) {
const response = await fetch(url, options);
if (response.status !== 429 || attempt >= attempts - 1) return response;
const retryAfter = Number(response.headers.get("Retry-After"));
const backoff = 2 ** attempt * 1000 + Math.random() * 1000;
const wait = Number.isFinite(retryAfter) && retryAfter > 0 ? retryAfter * 1000 : backoff;
await new Promise((resolve) => setTimeout(resolve, wait));
}
}Send fewer requests
- Cache responses that don't change often instead of fetching them again.
- Use the API's batch or bulk endpoints when it has them.
- Limit how many requests run in parallel, for example 5 at a time, rather than starting hundreds with
Promise.all. - Debounce requests triggered by typing, such as search-as-you-type.
- Use webhooks instead of polling if the API offers them.
Returning 429 from your own API
Send a Retry-After header with the 429 so clients know when to come back, and include the limit in the response body or headers. Rate limit per API key or user where possible, rather than per IP address, which many users can share.
Frequently asked questions
How long should I wait after a 429?
As long as Retry-After says. Without it, start at about a second and double the wait after each further 429, up to a limit.
Why do I get 429 when I'm barely sending requests?
The limit may be shared, for example across all users behind the same IP address or across all the apps using one API key. Some limits are also counted per day, not per minute.
Is 429 the same as 503?
No. 429 says you in particular are sending too much. 503 Service Unavailable says the server is overloaded or down for everyone.
More http status code lookup guides
- 502 Bad Gateway: What It Means and How to Fix It
- 504 Gateway Timeout: Causes and Fixes
- 403 Forbidden: Meaning, Causes and How to Fix It
- 401 Unauthorized: What It Means and How to Fix It
- 500 Internal Server Error: Causes and How to Fix It
- 503 Service Unavailable: Causes and Fixes
- 400 Bad Request: Causes and Fixes (Including Cookie Too Large)
- 404 Not Found: What It Means and How to Fix It
- 301 Moved Permanently: Meaning, SEO and Examples
- 302 Found: Temporary Redirects Explained
- 408 Request Timeout: Causes and How to Fix It