workflowRoute()
Creates a workflow route handler for streaming workflow execution using the AI SDK format. This function registers an HTTP POST endpoint that accepts input data, executes a workflow, and streams the response back to the client in AI SDK-compatible format. You have to use it inside a custom API route.
Use handleWorkflowStream() if you need a framework-agnostic handler.
When a workflow step pipes an agent's stream to the workflow writer (e.g., await response.fullStream.pipeTo(writer)), the agent's text chunks and tool calls are forwarded to the UI stream in real time, even when the agent runs inside workflow steps.
See Workflow Streaming for more details.
Usage exampleDirect link to Usage example
This example shows how to set up a workflow route at the /workflow endpoint that uses a workflow with the ID weatherWorkflow.
import { Mastra } from "@mastra/core";
import { workflowRoute } from "@mastra/ai-sdk";
export const mastra = new Mastra({
server: {
apiRoutes: [
workflowRoute({
path: "/workflow",
workflow: "weatherWorkflow",
}),
],
},
});
You can also use dynamic workflow routing based on a workflowId. The URL /workflow/weatherWorkflow will resolve to the workflow with the ID weatherWorkflow.
import { Mastra } from "@mastra/core";
import { workflowRoute } from "@mastra/ai-sdk";
export const mastra = new Mastra({
server: {
apiRoutes: [
workflowRoute({
path: "/workflow/:workflowId",
}),
],
},
});
ParametersDirect link to Parameters
path?:
workflow?:
includeTextStreamParts?:
Additional configurationDirect link to Additional configuration
You can use prepareSendMessagesRequest to customize the request sent to the workflow route, for example to pass additional configuration to the workflow:
const { error, status, sendMessage, messages, regenerate, stop } = useChat({
transport: new DefaultChatTransport({
api: "http://localhost:4111/workflow",
prepareSendMessagesRequest({ messages }) {
return {
body: {
inputData: {
city: messages[messages.length - 1].parts[0].text,
},
// Or resumeData for resuming a suspended workflow
resumeData: {
confirmation: messages[messages.length - 1].parts[0].text
}
},
};
},
}),
});