Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

axios gets 429 Too Many Requests from a React app

ErrorRequest failed with status code 429

Short 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.

  • Applies to axios 1, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"]);
      }
    });
  2. 2Match what you see, then jump to its fix

    1. 1429 after a few quick calls, Retry-After presentThe API simply has a low rate limitFix
    2. 2The network tab shows dozens of identical calls, one per renderYour own code sends requests in a loopFix
    3. 3Exactly two identical calls on mount, only in devStrict Mode runs the effect twice in developmentFix
    4. 4Retry-After is null in the browser but present in curlThe browser cannot read Retry-After from another originFix

What does the error look like?

  • Request failed with status code 429
  • 429 (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/items

2Fix

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/items

Confirmed by developer.mozilla.org

Did this fix it?

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 render

2Fix

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 path

Confirmed by react.dev

Did this fix it?

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 StrictMode

2Fix

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 preview

3Verify

A production build makes one call on mount, not two.

// network tab: filter by the API path

Confirmed by react.dev

Did this fix it?

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/items

2Fix

On the API server, add the header to the exposed list.

Access-Control-Expose-Headers: Retry-After

3Verify

error.response.headers['retry-after'] now holds the value.

console.log(error.response.headers['retry-after']);

Confirmed by developer.mozilla.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.