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

# For-each loop

export const item_0 = undefined

export const itemId_0 = undefined

export const prefix_item_0 = undefined

export const prefix_itemId_0 = undefined

Loops through each item in a list to run the same logic.

<img src="https://mintcdn.com/writer/KruNpIclsgQbhj82/images/agent-builder/blueprints/for-each-loop-block.png?fit=max&auto=format&n=KruNpIclsgQbhj82&q=85&s=bbc6254daef003626690709b7cd5b200" alt="" width="2310" height="1490" data-path="images/agent-builder/blueprints/for-each-loop-block.png" />

## Overview

The **For-each loop** block iterates over a list or dictionary and executes a workflow for each item.

The following variables are available in the workflow when iterating with the **For-each loop** block:

* For a list of items (for example, `["a", "b", "c"]`):
  * `@{item}`: The current item in the list (for example, `"a"`, `"b"`, or `"c"`)
  * `@{itemId}`: The current item's index in the list, starting at 0 (for example, `0`, `1`, or `2`)
* For a dictionary of items (for example, `{"a": "apple", "b": "banana", "c": "cherry"}`):
  * `@{item}`: The current item's value in the dictionary (for example, `"apple"`, `"banana"`, or `"cherry"`)
  * `@{itemId}`: The current item's key in the dictionary (for example, "a", "b", or "c")

## Example

The following example shows a **For-each loop** block that loops over a list of file IDs and makes an HTTP request to the Writer API delete each file in the list by ID. It then sets a state variable to display a success message when it's done.

<img src="https://mintcdn.com/writer/KruNpIclsgQbhj82/images/agent-builder/blueprints/for-each-loop-example.png?fit=max&auto=format&n=KruNpIclsgQbhj82&q=85&s=88de5b30417585c07a64f502a4489c0e" alt="" width="2230" height="1262" data-path="images/agent-builder/blueprints/for-each-loop-example.png" />

<Info>In this example, the list of file IDs is a hardcoded list of strings. In a more realistic example, the list of file IDs would be returned from a previous block or stored in state, rather than hardcoded. In that case, you could use the environment variable such as `@{payload}` or `@{state_element}` to access the list in the `Items` field.</Info>

The **For-each loop** block iterates over the list of items in the `Items` field and executes the blocks that follow for each item. To reference the current item in the loop, you can use the environment variable `@{item}`. To reference the current item's index in the list, you can use the environment variable `@{itemId}`.

The HTTP request in this example uses the `@{item}` environment variable to reference the current file ID in the HTTP request's `URL` field.

<img src="https://mintcdn.com/writer/KruNpIclsgQbhj82/images/agent-builder/blueprints/for-each-loop-http-request.png?fit=max&auto=format&n=KruNpIclsgQbhj82&q=85&s=d0a3733edc23eef057989659f229389e" alt="" width="2300" height="1438" data-path="images/agent-builder/blueprints/for-each-loop-http-request.png" />

When the **For-each loop** block is done, it moves to the next block connected to the **Success** connection point. In this example, the next block is a **Set state** block that sets a state variable to display a success message when it's done.

## Fields

<table className="blueprintFields">
  <thead>
    <th>Name</th>
    <th>Type</th>
    <th>Control</th>
    <th>Default</th>
    <th>Description</th>
    <th>Options</th>
    <th>Validation</th>
  </thead>

  <tbody>
    <tr>
      <td>Items</td>
      <td>Object</td>
      <td>Textarea</td>

      <td>
        <code>\[]</code>
      </td>

      <td>The item value will be passed in the execution environment. It will be available at @{item_0}, and its ID at @{itemId_0}. You can use either a list or a dictionary.</td>

      <td>
        <span>-</span>
      </td>

      <td>
        <span>-</span>
      </td>
    </tr>

    <tr>
      <td>Prefix</td>
      <td>Text</td>
      <td>-</td>

      <td>
        <span>-</span>
      </td>

      <td>If set, the item will be available at @{prefix_item_0} and the item id at @{prefix_itemId_0}.</td>

      <td>
        <span>-</span>
      </td>

      <td>
        <span>-</span>
      </td>
    </tr>
  </tbody>
</table>

## End states

Below are the possible end states of the block call.

<table className="blueprintFields">
  <thead>
    <th>Name</th>
    <th>Field</th>
    <th>Type</th>
    <th>Description</th>
  </thead>

  <tbody>
    <tr>
      <td>Loop</td>
      <td>-</td>
      <td>dynamic</td>
      <td>Connect the branch that you'd like to loop on. The branch plugged in here will be executed once per item.</td>
    </tr>

    <tr>
      <td>Success</td>
      <td>-</td>
      <td>success</td>
      <td>The branch referenced executed successfully for each item.</td>
    </tr>

    <tr>
      <td>Error</td>
      <td>-</td>
      <td>error</td>
      <td>The branch referenced has failed for at least one of the items.</td>
    </tr>
  </tbody>
</table>

The `dynamic` end state means that the exact values of this end state change based on how you define the block.
