ExamplesWorkflowsCreating a Workflow

Creating a Workflow

A workflow allows you to define and execute sequences of operations in a structured path. This example shows a workflow with a single step.

import { Step, Workflow } from '@mastra/core';
import { z } from 'zod';
 
const myWorkflow = new Workflow({
  name: 'my-workflow',
  triggerSchema: z.object({
    input: z.number(),
  }),
});
 
const stepOne = new Step({
  id: 'stepOne',
  inputSchema: z.object({
    value: z.number(),
  }),
  outputSchema: z.object({
    doubledValue: z.number(),
  }),
  execute: async ({ context }) => {
    const doubledValue = context.machineContext?.triggerData?.input * 2;
    return { doubledValue };
  },
})
 
myWorkflow.step(
  stepOne
).commit();
 
const { runId, start } = myWorkflow.createRun({ 
  triggerData: { input: 90 } 
});
 
const res = await start();
 
console.log(res.results);





View Example on GitHub

MIT 2025 © Nextra.