ExamplesWorkflowsSequential Steps

Workflow with Sequential Steps

Workflow can be chained to run one after another in a specific sequence.

Control Flow Diagram

This example shows how to chain workflow steps by using the then method demonstrating how to pass data between sequential steps and execute them in order.

Here’s the control flow diagram:

Diagram showing workflow with sequential steps

Creating the Steps

Let’s start by creating the steps and initializing the workflow.

import { Step, Workflow } from "@mastra/core";
import { z } from "zod";
 
const stepOne = new Step({
  id: "stepOne",
  execute: async ({ context: { machineContext } }) => ({
    doubledValue: machineContext.triggerData.inputValue * 2,
  }),
});
 
const stepTwo = new Step({
  id: "stepTwo",
  execute: async ({ context: { machineContext } }) => ({
    incrementedValue:
      machineContext.stepResults.stepOne.payload.doubledValue + 1,
  }),
});
 
const stepThree = new Step({
  id: "stepThree",
  execute: async ({ context: { machineContext } }) => ({
    tripledValue:
      machineContext.stepResults.stepTwo.payload.incrementedValue * 3,
  }),
});
 
// Build the workflow
const myWorkflow = new Workflow({
  name: "my-workflow",
  triggerSchema: z.object({
    inputValue: z.number(),
  }),
});

Chaining the Steps and Executing the Workflow

Now let’s chain the steps together.

// sequential steps
myWorkflow.step(stepOne).then(stepTwo).then(stepThree);
 
myWorkflow.commit();
 
const { runId, start } = myWorkflow.createRun();
 
const res = await start({ triggerData: { inputValue: 90 } });





View Example on GitHub

MIT 2025 © Nextra.