Skip to main content
Mastra 1.0 is available 🎉 Read announcement

Mastra.getWorkflow()

The .getWorkflow() method is used to retrieve a workflow by its registration key. This method provides full TypeScript type inference for workflow input and output schemas.

Usage example
Direct link to Usage example

// Retrieve workflow by its registration key (recommended for type inference)
const workflow = mastra.getWorkflow("testWorkflow");
const run = await workflow.createRun();

// TypeScript will properly infer the input schema type
await run.startAsync({
inputData: { /* properly typed */ }
});

Type inference
Direct link to Type inference

For best TypeScript support, use getWorkflow() with the workflow's registration key (the key used when adding the workflow to Mastra). This provides full type inference for:

  • Input data schemas
  • Output data schemas
  • Step results

If you need to retrieve a workflow by its id property instead of its registration key, you can use getWorkflowById(), but note that type inference will not be as precise.

Parameters
Direct link to Parameters

id:

TWorkflowId extends keyof TWorkflows
The ID of the workflow to retrieve. Must be a valid workflow ID that exists in the Mastra configuration.

options:

{ serialized?: boolean }
Optional configuration object. When `serialized` is true, returns only the workflow name instead of the full workflow instance.

Returns
Direct link to Returns

workflow:

TWorkflows[TWorkflowId]
The workflow instance with the specified ID. Throws an error if the workflow is not found.
On this page