axios gets 429 Too Many Requests from a React app
Request failed with status code 429Short answer
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window. Read the Retry-After header, slow or batch your calls, and remove accidental loops, such as a useEffect that fires on every render. Do not retry in a tight loop.
Fix it fast
1Find out which cause you have
Catch the error and print the status and the Retry-After header. A number of seconds tells you the server's limit window, and a count of calls in the network tab tells you whether your own app is the source.
axios.get(url).catch((error) => { if (error.response) { console.log(error.response.status, error.response.headers["retry-after"]); } });2Match what you see, then jump to its fix
- 1
429 after a few quick calls, Retry-After presentThe API simply has a low rate limitFix - 2
The network tab shows dozens of identical calls, one per renderYour own code sends requests in a loopFix - 3
Exactly two identical calls on mount, only in devStrict Mode runs the effect twice in developmentFix - 4
Retry-After is null in the browser but present in curlThe browser cannot read Retry-After from another originFix
- 1
What does the error look like?
Request failed with status code 429429 (Too Many Requests)AxiosError: Request failed with status code 429
Cause 1 of 4
The API simply has a low rate limit
1Check
A 429 means the client sent too many requests in a given time. Limits are often server-wide or per resource, and usually tied to the client IP or a key. Read the API's own rate limit page for the number.
curl -i https://api.example.com/items2Fix
Wait as long as Retry-After says, then try again. Spread calls out, batch ids into one request if the API allows it, and cache results you already have.
const seconds = Number(error.response.headers["retry-after"] ?? 1);
await new Promise((r) => setTimeout(r, seconds * 1000));
const retry = await axios.get(url);3Verify
The retried call returns 200.
curl -i https://api.example.com/itemsConfirmed by developer.mozilla.org
Cause 2 of 4
Your own code sends requests in a loop
1Check
Open the network tab and count the calls for one page load. A useEffect without a dependency array, or one that sets state it depends on, runs again after every render.
useEffect(() => {
axios.get("/api/items").then((r) => setItems(r.data));
}); // no dependency array: runs on every render2Fix
Give the effect a dependency array and put only the values it truly reads in it.
useEffect(() => {
axios.get("/api/items").then((r) => setItems(r.data));
}, []);3Verify
One call per page load in the network tab.
// network tab: filter by the API pathConfirmed by react.dev
Cause 3 of 4
Strict Mode runs the effect twice in development
1Check
In development, Strict Mode runs one extra setup and cleanup cycle for every effect. A fetch inside the effect therefore fires twice. It does not happen in a production build.
// count the calls in the network tab, with and without StrictMode2Fix
Leave StrictMode on. Count calls in a production build, where the extra cycle does not run, and keep development traffic under the API's limit, for example by pointing development at a sandbox key or saved responses.
npm run build
npm run preview3Verify
A production build makes one call on mount, not two.
// network tab: filter by the API pathConfirmed by react.dev
Cause 4 of 4
The browser cannot read Retry-After from another origin
1Check
Only a short list of response headers is visible to cross-origin scripts. Retry-After is not on it, so the server must expose it.
curl -i -H "Origin: https://app.example.com" https://api.example.com/items2Fix
On the API server, add the header to the exposed list.
Access-Control-Expose-Headers: Retry-After3Verify
error.response.headers['retry-after'] now holds the value.
console.log(error.response.headers['retry-after']);Confirmed by developer.mozilla.org
How do I stop it coming back?
- Wait for Retry-After and use growing delays between retries, with a maximum number of attempts.
- Debounce search boxes and cache responses with a query library.
- Count requests per page load in the network tab before you ship.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fix