# Recursive API workflows

<details>

<summary>Help us improve this article</summary>

This article is part of a significant update to the Bubble manual and your feedback is critical to our efforts to continuously enhance our written documentation.\
\
We would greatly appreciate if you could take a moment to let us know your thoughts on the quality of it. Thank you for your support!\
\
[Give feedback on this article](https://docs.google.com/forms/d/e/1FAIpQLSfe7eaYVxkqTa_nn3QE6VObCxWB1hgh6sHUQGQ0Eit8JlAS7g/viewform?usp=pp_url\&entry.619913899=https://manual.bubble.io/help-guides/apis-connect-to-other-apps/the-bubble-api/the-workflow-api/api-workflows/recursive-api-workflows\&entry.80834677=Recursive+API+Workflows)

</details>

{% hint style="info" %}
In most scenarios, when using API workflows to process a list of things, *Schedule API workflow on a list* will be a more efficient method, both in terms of completion time and WU consumption. We generally recommend using *Schedule API workflow on a list,* for bulk processing, but there are corner cases where recursive workflows will give you more flexibility.

Article: [Scheduling API workflows](https://manual.bubble.io/~/changes/o0tR596HnVrTNeGgNtez/help-guides/integrations/api/the-bubble-api/the-workflow-api/api-workflows/scheduling-api-workflows)
{% endhint %}

API workflows can be set up to be recursive, meaning that they re-schedule themselves as part of the workflow. This is not a Bubble feature per sé but a method of combining Bubble’s backend features to solve specific problems.

By having an API workflow reschedule itself, you can open up for:

* Looping a workflow to perform processing on a list of things in sequence (as opposed to finishing each as fast as possible)
* Setting up workflows that automatically run at set or [flexible intervals](#user-content-fn-1)[^1]
* Setting up process where a workflows relies on data from a previous cycle of that workflow (by passing [dynamic parameters](#user-content-fn-2)[^2] to the next "cycle" of the workflow)

## **Processing a list of things**

Keeping in mind the points above, recursive workflows are a useful alternative to *Schedule API workflow on a list* in some corner cases.&#x20;

For example; by using a recursive workflow, we can instruct Bubble to go over each item one by one and we can set up a customizable delay in-between each iteration if needed. This can be less than a second, or months/years in-between, depending on your needs.

Imagine that you have a list of items that you need to process one by one. You could use a recursive workflow to process the items.

1. First, set up an API Workflow with a parameter containing the data type you want to process. Make sure to check the *Is list/array* to tell Bubble that we are passing a list of things rather than a single record.\
   \
   For this example, we’ll call the parameter rentalunits and set the data type to Rental Units.<br>
2. Now add the action that you want to perform. For this example we’ll simply generate a random string for the name of the rental unit. Set the Thing to change to rentalunits:first item. This way we’ll make the change on the first item in the list.<br>
3. Now we’ll change the Unit name field by using the Calculate Randomstring feature<br>
4. Our next step is what makes this a recursive workflow. We’ll now use the Schedule API Workflow to make the workflow run again. Three points are important to include:<br>
   1. First, we schedule the workflow using the regular *Schedule API workflow* action.<br>
   2. Secondly, we need to pass the same list as was originally passed to the request, but remove the first item. This way we make sure that it goes on to work on the next item in the list in the upcoming loop.\
      \
      We do this by passing the parameter rentalunits:minus item:rentalunits:first item<br>
   3. Then, we need to add a condition that checks whether there is a next item. We do this by counting the number of items left after the first one has been removed: rentalunits:minus item:rentalunits:first item:count > 0.\
      \
      This condition makes sure that the workflow doesn’t loop forever but stops when the count is zero.

## **Running at intervals**

To set up a workflow to run at set or flexible intervals, you simply set the rescheduling action to user the *current date/time* + any value that you want in-between the cycles. For example, setting the schedule action to use the *current date/time + months:1* will ensure that the cycle runs exactly once every month from the time it is scheduled.

[^1]: Since each new cycle is set using the *Schedule API workflow* action, you can schedule it at any point in time in the future, or even dynamically change the time to fit specific needs.\
    \
    Article: [Scheduling API workflows](https://manual.bubble.io/~/changes/o0tR596HnVrTNeGgNtez/help-guides/integrations/api/the-bubble-api/the-workflow-api/api-workflows/scheduling-api-workflows)\
    Reference: [Schedule API workflow](https://manual.bubble.io/~/changes/o0tR596HnVrTNeGgNtez/core-resources/actions/custom#schedule-api-workflow)

[^2]: API workflows can accept parameters when they are scheduled. Paramters can contain every type of data, including custom data types.\
    \
    Article: [Scheduling API workflows](https://manual.bubble.io/~/changes/o0tR596HnVrTNeGgNtez/help-guides/integrations/api/the-bubble-api/the-workflow-api/api-workflows/scheduling-api-workflows)\
    Reference: [Schedule API workflow](https://manual.bubble.io/~/changes/o0tR596HnVrTNeGgNtez/core-resources/actions/custom#schedule-api-workflow)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://manual.bubble.io/~/changes/o0tR596HnVrTNeGgNtez/help-guides/integrations/api/the-bubble-api/the-workflow-api/api-workflows/recursive-api-workflows.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
