> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spojit.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Loop

> Iterate over items or repeat steps a fixed number of times.

The **Loop** node repeats a set of steps. It supports three iteration modes: forEach, while, and times.

## Loop modes

### ForEach

Iterates over each item in an array. The current item and index are available to nodes inside the loop body.

| Field     | Description                                                    |
| --------- | -------------------------------------------------------------- |
| **Array** | The array to iterate over (reference a previous node's output) |

### While

Repeats as long as a condition is true. Evaluates the condition before each iteration.

| Field              | Description                                      |
| ------------------ | ------------------------------------------------ |
| **Condition**      | The expression to evaluate before each iteration |
| **Max iterations** | Safety limit to prevent infinite loops           |

### Times

Repeats a fixed number of times.

| Field     | Description          |
| --------- | -------------------- |
| **Count** | Number of iterations |

## Output handles

| Handle       | Description                                             |
| ------------ | ------------------------------------------------------- |
| **Body**     | Connect the nodes that should execute on each iteration |
| **Complete** | Continues here after all iterations are finished        |

## Accessing loop variables

Inside the loop body, two special variables are injected on each iteration:

| Variable           | Default name | Description                              |
| ------------------ | ------------ | ---------------------------------------- |
| **Item variable**  | `item`       | The current array element (forEach mode) |
| **Index variable** | `index`      | The current iteration index (0-based)    |

You can customize these names in the loop node properties panel. Use them in any template expression inside the loop body:

| Expression                | Resolves to                                                               |
| ------------------------- | ------------------------------------------------------------------------- |
| `{{ item }}`              | The entire current element (preserves type: object, string, number, etc.) |
| `{{ item.email }}`        | A specific field on the current element                                   |
| `{{ item.address.city }}` | Nested field access                                                       |
| `{{ index }}`             | Current iteration index: `0`, `1`, `2`, ...                               |

<Note>
  When `{{ item }}` is the **entire value** (not mixed with other text), the original type is preserved. For example, if `item` is an object, `{{ item }}` resolves to that object, not a JSON string. When mixed with text like `https://api.com/{{ item.id }}`, it resolves to a string.
</Note>

## Example: Fetch users and post each to a webhook

This example fetches a list of users from an API, then loops over each user and sends their data to a webhook.

### Canvas layout

```
[Trigger] → [Fetch Users] → [Loop] ─── body ───→ [Post to Webhook]
                                    └── complete ─→ [Send Summary]
```

### Step 1: Fetch Users (Connector node, direct mode)

| Field               | Value                                                   |
| ------------------- | ------------------------------------------------------- |
| **Connector**       | HTTP Requests                                           |
| **Runner mode**     | Direct                                                  |
| **Tool**            | `http-get`                                              |
| **Tool args**       | `{"url": "https://jsonplaceholder.typicode.com/users"}` |
| **Output variable** | `users`                                                 |

### Step 2: Loop (forEach)

| Field              | Value              |
| ------------------ | ------------------ |
| **Loop type**      | ForEach            |
| **Array**          | `{{ users.data }}` |
| **Item variable**  | `item`             |
| **Index variable** | `index`            |

The array path `users.data` navigates into the HTTP response: `users` (output variable) is the parsed HTTP response object `{ data, status, headers, statusText }`, and `.data` is the response body (here a JSON array of user objects).

### Step 3: Post to Webhook (Connector node inside loop body, direct mode)

| Field           | Value                                                          |
| --------------- | -------------------------------------------------------------- |
| **Connector**   | HTTP Requests                                                  |
| **Runner mode** | Direct                                                         |
| **Tool**        | `http-post`                                                    |
| **Tool args**   | `{"url": "https://example.com/webhook", "body": "{{ item }}"}` |

On each iteration, `{{ item }}` resolves to the current user object (e.g., `{"id": 1, "name": "Leanne Graham", "email": "Sincere@april.biz", ...}`).

You can also map individual fields:

```json theme={null}
{
  "url": "https://example.com/webhook",
  "body": {
    "name": "{{ item.name }}",
    "email": "{{ item.email }}",
    "city": "{{ item.address.city }}"
  }
}
```

### Step 4: Send Summary (Connector node after loop completes)

Connect from the loop's **Complete** handle. This node runs once after all iterations are finished. The loop's collected results are available via its output variable.

## Tips

* Always set a **max iterations** limit on while loops to prevent runaway execution.
* The loop collects results from each iteration; the complete output is an array of all iteration results.
* For parallel processing of array items, consider using a [Parallel](/workflow-editor/nodes/parallel) node instead.
