Workflows API
The Workflows API provides methods to interact with and execute automated workflows in Mastra.
Getting All Workflows
Retrieve a list of all available workflows:
const workflows = await mastraClient.getWorkflows();
Working with a Specific Workflow
Get an instance of a specific workflow as defined by the const name:
src/mastra/workflows/test-workflow.ts
export const testWorkflow = createWorkflow({
id: 'city-workflow'
})
const workflow = mastraClient.getWorkflow("testWorkflow");
Workflow Methods
Get Workflow Details
Retrieve detailed information about a workflow:
const details = await workflow.details();
Start workflow run asynchronously
Start a workflow run with inputData and await full run results:
const run = await workflow.createRunAsync();
const result = await run.startAsync({
inputData: {
city: "New York",
},
});
Resume Workflow run asynchronously
Resume a suspended workflow step and await full run result:
const run = await workflow.createRunAsync();
const result = await run.resumeAsync({
step: "step-id",
resumeData: { key: "value" },
});
Watch Workflow
Watch workflow transitions:
try {
const workflow = mastraClient.getWorkflow("testWorkflow");
const run = await workflow.createRunAsync();
run.watch((record) => {
console.log(record);
});
const result = await run.start({
inputData: {
city: "New York",
},
});
} catch (e) {
console.error(e);
}
Resume Workflow
Resume workflow run and watch workflow step transitions:
try {
const workflow = mastraClient.getWorkflow("testWorkflow");
const run = await workflow.createRunAsync({ runId: prevRunId });
run.watch((record) => {
console.log(record);
});
run.resume({
step: "step-id",
resumeData: { key: "value" },
});
} catch (e) {
console.error(e);
}
Stream Workflow
Stream workflow execution for real-time updates:
try {
const workflow = mastraClient.getWorkflow("testWorkflow");
const run = await workflow.createRunAsync();
const stream = await run.stream({
inputData: {
city: 'New York',
},
});
for await (const chunk of stream) {
console.log(JSON.stringify(chunk, null, 2));
}
} catch (e) {
console.error('Workflow error:', e);
}
Get Workflow Run result
Get the result of a workflow run:
try {
const workflow = mastraClient.getWorkflow("testWorkflow");
const run = await workflow.createRunAsync();
// start the workflow run
const startResult = await run.start({
inputData: {
city: "New York",
},
});
const result = await workflow.runExecutionResult(run.runId);
console.log(result);
} catch (e) {
console.error(e);
}
This is useful when dealing with long running workflows. You can use this to poll the result of the workflow run.
Workflow run result
A workflow run result yields the following:
Field | Type | Description |
---|---|---|
payload | {currentStep?: {id: string, status: string, output?: Record<string, any>, payload?: Record<string, any>}, workflowState: {status: string, steps: Record<string, {status: string, output?: Record<string, any>, payload?: Record<string, any>}>}} | The current step and workflow state of the run |
eventTimestamp | Date | The timestamp of the event |
runId | string | Unique identifier for this workflow run instance |