Loops for pagination

You can use Postman Flows to build a “while do” loop that’s well-suited to pagination use cases.

The loops covered in the previous topics about loops and loops with external data all run through an entire list or for a specified number of iterations. Other than waiting for them to complete, you can’t short circuit or break out of those loops.

This topic’s pagination use case requires a different design pattern: one that enables you break out of a loop:

  • Update a value within the loop, then use an HTTP Request block to send the value to and retrieve it from the Postman Echo API. This enables your flow module to manipulate state across multiple iterations of the loop.

  • Within the loop, use an Evaluate block to increment or otherwise changes the value and a Condition block to decide whether the loop should iterate or terminate based on the new value.

Build a loop for pagination

Here’s how the finished loop will look:

Flow module to paginate within a loop

Before building the flow module, create an HTTP GET request in a collection of your choice. Configure the request with the following URL:

https://postman-echo.com/get?pageNumber={{value}}

Rename the request “Current page number”.

Create a new flow module.

Construct the loop

In this section you’ll add blocks to build a loop, including a Condition block to determine whether to continue iterating or stop.

  1. Create an HTTP Request block, choose the “Current page number” HTTP request you created above. This renames the block as Current page number.

    Make sure the Start block isn’t connected to this or any other block.

  2. Connect a Number block to the value input of the Current page number block. Leave its value at 0.

  3. Connect an Evaluate block to the Current page number block’s Success output and rename it “Turn the page”. Change value1 to “pageBeforeTurning”. In the inline Select block, enter the path body.args.pageNumber.

  4. Paste the following expression into the TypeScript window:

    Number(pageBeforeTurning) + 1
    
  5. Connect a Delay block to the Result output of the Turn the page block and set its value to 1500 milliseconds.

  6. Connect a Condition block to the Delay block and rename it “Stop after page 5”. Rename value1 as onPage and paste the following expression into the Condition 1 window:

    onPage > 5
    

    This expression returns true when the page number exceeds 5. When that happens, the loop terminates. As long as the page number is less than or equal to 5, the loop keeps iterating.

  7. Run the flow module so that output values appear for each block. In the output of the Stop after page 5 block, under default, click and drag onPage to create a Select block that outputs the page number value.

  8. Connect the output of the new Select block to the value input of the Current page number block. Note that this connection loops back to the beginning, making the flow module a true loop in form, not only in function.

Add blocks to display status

The blocks you add in this section will enable you to follow the loop’s action throughout its lifecycle.

  1. Connect a Template block to the Success output of the Current page number block and rename it Message when iterating. Rename key as onPage. In the inline Select block, enter the path body.args.pageNumber. Paste the following expression into the text window:

    We are now on page {{onPage}}. Turning the page ...
    
  2. Connect a Display block to the Message when iterating block. From Setting icon Additional Settings, select Text size > Large.

  3. Connect a Template block to the Condition 1 output of the Stop after page 5 block and rename it Message when terminating. Rename key as lastPage. In the inline Select block, click Enter path... and choose onPage.

  4. Paste the following expression into the text window:

    Stopping! We got to page {{lastPage}}.
    
  5. Connect the Display block you created earlier to the Message when terminating block.

Run the flow module. You’ll see each block activate in sequence until the loop goes beyond page 5 and terminates.

Last modified: 2025/10/03