For icon

For

Base for a For-Loop

Overview

This node implements a configurable "For" loop within an n8n workflow. It allows users to define the start, end, and step values for iteration, and optionally use input data as part of the loop. The node is designed with a safety mechanism to prevent infinite or excessively long loops, making it suitable for controlled, repeatable operations such as batch processing, pagination, or iterative calculations.

Common scenarios:

  • Iterating over a range of numbers to trigger actions multiple times.
  • Paginating through API results by incrementing a counter.
  • Repeating a set of workflow steps a specific number of times.

Practical example:
You could use this node to send reminder emails to a list of users, process items in batches, or perform repeated API calls with incremental parameters.

Properties

Name Type Meaning
StartValue Number The initial value from which the loop starts. Defines the starting point of the counter.
EndValue Number The last index or value at which the loop should stop. The loop continues until this value is reached or exceeded (depending on the step direction).
StepValue Number The amount by which the counter increases (or decreases if negative) each iteration.
useInput Boolean Whether the initial input data should be used within the loop. If true, the node uses incoming items as the basis for looping; otherwise, it creates its own counter object.
Info! The For-Loop includes a certain risk to block the server for a long while. To avoid this, we have a safety switch for an execution time of maximum 1000 runs/ 2 minutes. Please only deactivate this switch if you're really sure you know what you're doing and if you have access to your backend to restart it! Notice Informational notice warning about potential risks of long-running loops and the existence of a safety switch.
SafetySwitch Boolean Enables or disables the endless-loop-prevention mechanism. When enabled, the node will halt after 1000 iterations or 2 minutes, whichever comes first. Only disable if you are certain of safe operation.

Output

The node produces up to three outputs per execution:

  1. First output ("finished"):

    • Contains the original input items when the loop has finished (i.e., the counter has reached or passed the end value).
    • Each item retains its original structure.
  2. Second output ("dataOut"):

    • Used during ongoing iterations. When the loop is not yet finished, the current input items are sent here for further processing in the next loop cycle.
  3. Third output ("counterOut"):

    • Outputs a single item per iteration with the following JSON structure:
      {
        "counter": <current counter value>,
        "start": <start value>,
        "step": <step value>,
        "end": <end value>,
        "runIndex": <current run index>
      }
      
    • This can be used to track the progress of the loop or drive other nodes that require the current counter state.

Note:
If the safety switch triggers (over 1000 runs or 2 minutes), all outputs will be empty arrays, effectively halting the loop.

Dependencies

  • No external services or API keys required.
  • Relies on n8n's built-in context and parameter handling.
  • No special environment variables needed.

Troubleshooting

Common issues:

  • Loop does not terminate:
    If the EndValue is not reachable from StartValue using the given StepValue, the loop may never finish unless the safety switch is active.

    • Resolution: Ensure that StepValue moves the counter towards EndValue.
  • Safety switch triggered (outputs are empty):
    If the loop exceeds 1000 iterations or 2 minutes, the node stops and returns no data.

    • Resolution: Adjust your loop parameters or disable the safety switch only if you are confident it won't cause problems.
  • Unexpected output structure:
    If useInput is false, the node generates its own counter objects instead of using incoming data.

    • Resolution: Set useInput according to whether you want to iterate over existing input items or just use the counter.

Error messages:

  • The node does not throw explicit error messages but silently halts if the safety switch conditions are met.

Links and References

Discussion