AI summary

What to do in n8n when an API call fails: timeouts, 401 and 403, 429 with Retry-After, empty results, and safely retrying requests that change server data.

  • Retry timeouts on reads, stop on 401 and 403, wait on 429, and accept an empty result as an answer.
  • Retry On Fail waits at most 5 seconds between tries and ignores Retry-After, so read that header yourself.
  • A request that changes server data may have worked even if it timed out. Use an idempotency key, look it up first, or send it to a person.

A 429, a 403 and an empty list can all look like “the API call didn’t work” from inside an n8n workflow. They need different handling.

In n8n, each step of a workflow is a node. Every node has a setting called Retry On Fail that repeats a failed call. Turn it on for everything and it gets most of these cases wrong.

I built n8n workflows between about 10 university library systems and an AI chatbot. Each library had its own API, authentication and response format, so the same failure looked different in each one. That’s why I decide by failure type, not by API. Pick a failure here:

Choose the recovery pathAn illustrative API scenario. Change the failure to see the recovery action.

Bounded retries with a delay. For writes, check whether repeating the operation is safe.

Timeouts: retry reads a limited number of times

A timeout doesn’t tell you whether the server did the work. For a GET that doesn’t matter, so retry a few times with a delay and then give up.

A write, a request that creates or changes data, is different. For that, skip to the last section.

401 and 403: stop

A 401 means the credentials were refused. If the token you send to prove who you are is no longer valid, try getting a new one without signing in again, if the API allows it. Do that once, then retry the request once. RFC 9110 allows repeating the request with a new or replaced Authorization header.

A 403 means the server understood the request and refuses it. If you sent credentials, it found them insufficient. In that case, RFC 9110 says a client shouldn’t automatically repeat the request with the same credentials. More tries only use up your rate limit.

I’d end the run with a Stop And Error node, so the error workflow can alert someone with the API name and status.

429: wait as long as the API asks

A 429 means you’ve sent the API too many requests. The response may include a Retry-After header that says how long to wait. The value is either a number of seconds, such as 120, or a date, such as Fri, 13 Nov 2026 09:00:00 GMT.

Retry On Fail can’t follow it. According to n8n’s page on rate limits, Retry On Fail waits at most 5 seconds between tries and tries at most five times. The same page says the HTTP Request node doesn’t honor Retry-After. You have to read the header yourself.

The chat on this site sends the same response from the other side: a 429 with Retry-After when one visitor asks too often.

Then think about who’s waiting. Imagine a student asks the chatbot about a book and the API answers Retry-After: 3600. Nobody stays in a chat for an hour. Tell them the catalog isn’t available right now and stop. A nightly sync can actually wait.

Empty results: accept them

A successful search with no matches is an answer. If you treat it as a failure, the chatbot says something went wrong when the library just doesn’t have the book.

“Empty” looks different from one API to another. It could be [], null, or a 404 on a lookup. Some APIs even return 200 with an error in the body, and that one should become a failure, not an empty result.

I’d turn each API’s version of “empty” into one shape, once per integration, before the shared part of the workflow. A 404 that means “nothing found” then reaches the next step as a 200 with an empty body.

Reading the status in n8n

Turn on Never Error and Include Response Headers and Status in the HTTP Request node. The node now reports success for any status, so Retry On Fail won’t fire on a 429. What happens next is up to the steps you add after it. I’ll set those up below.

The decision itself goes in a Code node, which runs your own JavaScript. In the mode used below, the code runs once for each item. In n8n, an item is one record the workflow is working on, here one request and its response.

A timeout has no status code, and Never Error doesn’t catch it. So also set the node’s On Error setting to Continue (using error output). A failed request then leaves through a second output instead of stopping the run. Connect that output to the same Code node.

I tried this in n8n 1.110.1 with the node’s timeout set to 1 second. The item on the error output kept the fields it came in with and got an error field, with the message timeout of 1000ms exceeded. It has no statusCode, so the code handles it the same way as a server error. A server error is a 5xx status, such as 500 or 503: the server itself failed.

The code also needs to know whether a request is safe to send again. An earlier node sets safeToRepeat for that. It’s true for reads. It’s also true for two kinds of writes.

The first kind carries an idempotency key: a unique value you send with the request, so the server can recognize a repeat and return the first result instead of doing the work twice. The second kind is a write you can look up first. Run that lookup before each retry, and send the POST again only if it finds nothing. The section on writes below covers both.

The code retries a 5xx or a timeout only when safeToRepeat is true. Otherwise the item goes to review, which means a person checks it.

Every other status, 401 and 403 included, becomes stop. This code doesn’t refresh tokens. If you refresh tokens on a 401, give that its own branch before this node.

js
// n8n Code node, mode: Run Once for Each Item.
// Assumes a previous "Prepare request" node sets safeToRepeat.
const { statusCode: status, headers = {}, body } = $json;
const { safeToRepeat } = $("Prepare request").item.json;

function retryAfterSeconds(value, fallback = 60) {
  if (!value) return fallback;
  if (/^\d+$/.test(value)) return Number(value);
  const date = Date.parse(value);
  if (Number.isNaN(date)) return fallback;
  return Math.max(0, Math.ceil((date - Date.now()) / 1000));
}

let action = "stop";
if (status >= 200 && status < 300) action = "accept";
else if (status === 429) action = "wait";
// No status: the item came from the timeout (error output) branch.
else if (status === undefined || status >= 500) {
  action = safeToRepeat ? "retry" : "review";
}

return {
  json: {
    action,
    status,
    body,
    waitSeconds: retryAfterSeconds(headers["retry-after"]),
  },
};

I ran this node in n8n 1.110.1 against httpbin, with safeToRepeat set to true. One request got each status below, and a fifth took longer than the node’s 1-second timeout. httpbin’s responses here have no body, so there’s no body field in what came out:

text
{"action":"wait","status":429,"waitSeconds":60}
{"action":"retry","status":503,"waitSeconds":60}
{"action":"stop","status":403,"waitSeconds":60}
{"action":"accept","status":200,"waitSeconds":60}
{"action":"retry","waitSeconds":60}

httpbin’s 429 has no Retry-After header, so the wait fell back to 60 seconds.

Both tests ran on 1.x. I haven’t rerun them on 2.x, but n8n’s current docs list the node settings above under the same names.

Send action into a Switch node, which sends each item down a branch by that value. The wait and retry branches loop back through a Wait node that takes its duration from waitSeconds. Both branches need an attempt counter, or a stuck API keeps the run going forever.

I prefer keeping this decision in one small Code node over a chain of IF nodes. Logic spread across nodes gets hard to read, and one Code node is easy to test separately.

Writes: a timeout might mean it worked

A POST that timed out is the risky case. The server may have saved the record, for example a hold on a library book, even though your workflow never got the answer, and then a retry saves it twice.

A request is idempotent when sending it twice has the same effect as sending it once. RFC 9110 counts PUT and DELETE as idempotent, along with methods like GET that only read. It says a client shouldn’t automatically retry anything else unless it knows that’s safe.

A POST that timed out after the server saved itAn illustrative example of three ways to continue.

Blind retry

The workflow can't tell the first request worked.

  • POST times out
  • Server already saved the hold (changed)
  • Retry the same POST
  • Second hold created (added)

Idempotency key

The server recognizes the repeat.

  • POST with key times out
  • Server already saved the hold (changed)
  • Retry with the same key
  • One hold, first result returned

Look up first

For APIs without keys, if you can search by your own reference.

  • POST with your reference times out
  • GET by reference
  • Found, so skip the retry

So I’d go in this order:

  1. If the API supports an idempotency key, use one. Stripe’s Idempotency-Key header is a good example. Generate the key once, before the first attempt, and keep it with the item. A key that changes on each try protects nothing.
  2. If it doesn’t, send your own reference: a unique ID you generate and save in a field the API lets you search by. Look it up before repeating.
  3. If you can do neither, stop and send it to a person. A manual check is cheaper than a duplicate record.

A consumer that reads messages from a queue has the same problem. I wrote about how to handle it in a queue consumer too.

References

  1. RFC 9110: HTTP Semantics
  2. RFC 6585: 429 Too Many Requests
  3. n8n: Handling API rate limits
  4. n8n: HTTP Request node
  5. n8n: Error handling
  6. n8n: Node settings
  7. Stripe API: Idempotent requests

Written by:

Full Stack Software EngineerSamsun, Türkiye

Full Stack Software Engineer. I started with client sites at an agency in 2019, then worked on embeds, a dashboard and analytics at a startup called Popupsmart.