Create a list-based loop with the For block

You can build a Flow that loops over a list. For example, you can run a Flow that loops over a list of email addresses and emails each person in the list.

This tutorial's goal is to teach you how to set up and use list-based loops. To achieve this goal, you'll build a Flow that iterates over the first three letters of the alphabet.

Create a new Flow

You build Flows in a workspace. Workspaces let you organize your API projects and collaborate with your team.

To create a new Flow, do the following:

  1. Choose an existing workspace or create a new one.
  2. From the Postman sidebar, select Flow icon (stroke, large) Flows.
  3. Select Add icon (stroke, large) Create a new Flow > Flow icon (stroke, small) Create new Flow.
  4. Enter a name. For example, enter Create a list-based loop.
  5. Select the Return or Enter key.

Add a For block

The For block lets you build a loop that iterates over a list. For example, if you want to run a loop that loops over a list of the alphabet, you connect the For block to such a list.

In the next procedure, you'll use the List block to define this list.

To add a For block, do the following:

  1. From the canvas toolbar, select Add icon (stroke, large) Block.
  2. Select For. If you want to search for the block, enter For in search.
  3. Decide where on the canvas you want to place the For block and select that location.

Connect a List block

When you connect two blocks, you connect one block's input to another block's output. Inputs are on the block's left side and outputs are on its right side.

In the tutorial, you'll use the List block to define the list you want your loop to iterate over.

To connect a List block, do the following:

  1. Hover over the For block's List input port. The pointer changes to a crosshair.
  2. Decide where on the canvas you want to place the List block and drag the port to that location.
  3. Select List icon (stroke, large) List. If you want to search for the block, enter List in search.
  4. Select Add icon (stroke, large) Add data blocks and choose String icon (stroke, large) String. If you want to search for the block, enter String in search.
  5. Enter the string's value. For example, if you want to iterate over the first three letters of the alphabet, enter a.
  6. Repeat the previous two steps until you complete your list.

You can loop over any list. For example, you can get a list from a Send Request block's response or use the Evaluate block to generate a list.

As an alternative, replace the List block with an Evaluate block, switch to TypeScript (upper-right corner), and add the following code to the code editor:

const alphabet = []

for (let i = 0; i < 26; i++) {
	alphabet.push(String.fromCharCode(97 + i))
}

alphabet

This code creates an empty list, adds the letter "a" to it, and loops over the code until it adds all 26 letters of the alphabet to the list.

Connect a Collect block

The Collect block saves multiple outputs to a list. In this tutorial, your loop will run three times, output three values, and use this block to save these values to a list.

To connect a Collect block, do the following:

  1. Hover over the For block's output port. The pointer changes to a crosshair.
  2. Decide where on the canvas you want to place the Collect block and drag the port to that location.
  3. Select Collect icon (stroke, large) Collect. If you want to search for the block, enter Collect in search.

Connect an Output block

The Output block displays incoming data, such as the Collect block's outgoing list.

To connect an Output block, do the following:

  1. Hover over the Collect block's output port. The pointer changes to a crosshair.
  2. Decide where on the canvas you want to place the Output block and drag the port to that location.
  3. Select View icon (stroke, large) Output. If you want to search for the block, enter Output in search.

Run the Flow

From the canvas toolbar, select Run icon (stroke, large) Run.

Create a list-based loop Flow

Congratulations! You created a list-based loop and displayed the result in an Output block.

Last modified: 2024/08/13