Skip to content
LogoLogo

How to retry a failed call with a loop

This guide shows you how to make a workflow send a request again after the request fails. The workflow waits before each new try. It stops after a number of tries that you set.

You need a workflow with an External API node first. Refer to How to call an external API from a workflow.

The loop

A loop is a path of edges that goes back to a node that the run reached before. This loop has three nodes:

NodeFunction
External API sendSends the request.
Condition checkDecides if the workflow sends the request again.
Sleep pauseWaits before the next try.

When you save a workflow, Picked refuses a loop that does not have a sleep node and a condition node on it. Picked also refuses an edge from a node to the same node.

1. Connect the error exit to a condition

  1. Open the workflow.
  2. Choose Add condition on the error exit of send.
  3. In the rule of check, test {{ nodes.send.error.retryable }}.
  4. Connect the false exit of check to the nodes that record the failure.

check runs only when the call fails. The rule can then read the error of the last try.

2. Add the sleep node

  1. Choose Add sleep on the true exit of check.
  2. Set Wait for to the time between two tries.
  3. Connect the exit of pause to the input of send.

The editor shows a loop as an edge that goes back to send.

3. Set the number of tries

Each node in the loop runs one time for each try. A run can visit one node a maximum number of times. This number is the visit cap of the workflow.

  1. Open the workflow settings.
  2. Set Visit cap to a number from 1 to 100. If you do not set it, the visit cap is 10.

A node can get to the visit cap. If the run then gets to the node again, the run stops. The run shows Failed with the code loop_limit_reached.

To stop the loop before the visit cap, test current.visit in the rule of check. Its value is the number of the visit to check. This is also the number of failed tries. This rule tries a maximum of 3 times:

{
  "and": [{ "var": "nodes.send.error.retryable" }, { "<": ["{{ current.visit }}", 3] }]
}

With this rule, the run continues on the false exit of check after 3 failures.

4. Read the result

A node reads the last visit of another node. After the loop, {{ nodes.send.status }} gives the status of the last try.

The Tasks card of the run shows each visit as a task. Refer to the Workflow runs reference.