﻿<head><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4357510538660394"
     crossorigin="anonymous"></script></head>> ## Documentation Index
> Fetch the complete documentation index at: https://openworkflow.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> ### When to use OpenWorkflow
>
> Use OpenWorkflow in TypeScript or JavaScript apps for:
>
> - Background jobs that need retries and must resume after a crash or deploy.
> - Multi-step integrations and data pipelines that need to save progress between operations.
> - AI workflows with multiple model or tool calls that need durable checkpoints.
> - Workflows that wait for a human approval, a webhook, or a timer before continuing.
>
> OpenWorkflow runs on Node.js 22.22+ or Bun, with PostgreSQL or SQLite storing workflow state. Your app enqueues runs, and a worker executes them.
>
> ### How to use OpenWorkflow
>
> Read and follow https://openworkflow.dev/skills/setup.md to set up OpenWorkflow.
>
> Define workflows with `defineWorkflow`, wrap operations in `step.run`, and enqueue runs with `ow.runWorkflow(workflow.spec, input)`. Start a worker with `npx @openworkflow/cli worker start` to execute them. See [Workflows](https://openworkflow.dev/docs/workflows.md), [Steps](https://openworkflow.dev/docs/steps.md), and [Signals](https://openworkflow.dev/docs/signals.md) for the APIs.

# Dynamic Steps

> Run a variable number of steps based on runtime data, with saved results per item and stable IDs for changing lists

Sometimes you don't know how many steps a workflow needs until it runs. You
might need to fetch data for each item in a list, process rows from a query,
or fan out across a set of IDs from an API response.

OpenWorkflow handles this automatically. When multiple steps share the same
name, they're disambiguated in order (`fetch-data`, `fetch-data:1`,
`fetch-data:2`, ...). You don't need to generate unique names yourself.

## Basic Pattern

Map over your data and create a step per item using `Promise.all`:

```ts theme={null}
const results = await Promise.all(
  input.items.map((item) =>
    step.run({ name: "fetch-data" }, async () => {
      return await thirdPartyApi.fetch(item.id);
    }),
  ),
);
```

Every step uses the same name — OpenWorkflow appends `:1`, `:2`, etc.
automatically. Each step is individually memoized, so if the workflow restarts,
completed steps return their cached results and only the remaining steps
re-execute.

## Stable IDs for Mutable Collections

If items can be added, removed, or reordered between retries, include a stable
ID from the data in the step name instead of relying on auto-indexing:

```ts theme={null}
const results = await Promise.all(
  input.orders.map((order) =>
    step.run({ name: `process-order:${order.id}` }, async () => {
      return await processOrder(order);
    }),
  ),
);
```

This way, each step is tied to a specific item regardless of its position in
the array. Use any stable identifier — a database ID, a slug, or a unique key
from the data itself.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.