Skip to main content
This page contains a full reference for the step function.
const data = await step("fetch-data", async () => {
  return await fetchDataFromAPI();
});

Parameters

name
string
required
Unique identifier for this step within the workflow.
Make sure you set a unique identifier for each step. Otherwise, the second step won’t execute and will reuse the output of the first step.
run
(opts: { attempt: number }) => T | Promise<T>
required
Function to execute. Receives the current attempt number.
options
object
Optional configuration object.

Methods

The following methods are available on each step:

listen

Put the workflow into listening mode, waiting for external events to resume:
await step.listen("wait-for-approval");
// Workflow pauses here until triggered
Parameters:
name
string
required
The name of the step.

sleep

Pause workflow execution for a specified duration:
// Sleep for 5 minutes
await step.sleep("wait-5-min", 5 * 60 * 1000);
Parameters:
name
string
required
The name of the step.
ms
number
required
Duration to sleep in milliseconds.

sleepUntil

Sleep until a specific date:
await step.sleepUntil("wait-until-noon", new Date("2025-01-15T12:00:00Z"));
Parameters:
name
string
required
The name of the step.
date
Date | string
required
The date to sleep until.

fail

Mark the workflow as failed and stop execution:
if (!user.isVerified) {
  await step.fail("User verification required");
}
Parameters:
reason
string
required
Description of why the workflow failed.

abort

Immediately abort the workflow execution without marking it as failed:
if (shouldPause) {
  step.abort();
}
Parameters: No parameters.

progress

Record a progress checkpoint without performing any action:
await step.progress("Started processing");
// ... do work ...
await step.progress("Finished processing");
Parameters:
name
string
required
The name of the progress checkpoint.

waitForWorkflow

Wait for another workflow to complete before continuing:
const childWorkflow = await childWorkflowInstance.start({});
const result = await step.waitForWorkflow("wait-for-child", childWorkflow.id);
Parameters:
name
string
required
The name of the step.
workflowId
string
required
ID of the workflow to wait for.

executeWorkflow

Start another workflow and wait for it to complete:
import ProcessingWorkflow from "../workflows/processing";

const result = await step.executeWorkflow(
  "process-data",
  ProcessingWorkflow,
  { data: inputData }
);
Parameters:
name
string
required
The name of the step.
workflow
Workflow
required
The workflow instance to execute.
input
object
Input data for the workflow (typed based on workflow’s input schema).

map

Process an array of items in parallel with controlled concurrency:
const results = await step.map(
  "process-users",
  users,
  async (user, { i }) => await processUser(user),
  { concurrency: 5, maxAttempts: 3 }
);
Parameters:
name
string
required
The name of the map operation.
items
T[]
required
Array of items to process.
run
(item: T, opts: { i: number }) => Promise<U>
required
Function to process each item. Receives the item and its index.
options
object
Optional configuration object.

forEach

Process an array of items without collecting results:
await step.forEach(
  "notify-users",
  users,
  async (user) => await sendNotification(user),
  { concurrency: 10 }
);
Parameters:
name
string
required
The name of the forEach operation.
items
T[]
required
Array of items to process.
run
(item: T, opts: { i: number }) => Promise<void>
required
Function to process each item. Receives the item and its index.
options
object
Optional configuration object.

batch

Process items in sequential batches:
await step.batch(
  "bulk-insert",
  records,
  async (batch) => await database.bulkInsert(batch),
  { batchSize: 100 }
);
Parameters:
name
string
required
The name of the batch operation.
items
T[]
required
Array of items to process.
run
(batch: T[], opts: { i: number }) => Promise<void>
required
Function to process each batch. Receives the batch array and starting index.
options
object
Optional configuration object.

request

Request data from a conversation and wait for a response. Requires defining requests in the workflow:
export default new Workflow({
  name: "order-workflow",
  requests: {
    orderId: z.object({
      orderId: z.string(),
    }),
  },
  handler: async ({ step }) => {
    const data = await step.request("orderId", "Please provide the order ID");
    // data is typed based on the request schema
  },
});
Parameters:
request
string
required
The name of the request (must be defined in workflow’s requests field).
message
string
required
Message to display to the user describing what data is needed.
stepName
string
Optional custom name for the step. Defaults to the request name.