@medusajs/workflows-sdk
Documentation | Website
Building blocks for digital commerce
Workflows SDK for Medusa: the tools to define durable, multi-step workflows with steps, compensation (rollback), hooks, and conditions. Use it to build custom commerce flows in a Medusa application, module, or plugin. Applications import it through @medusajs/framework/workflows-sdk, which re-exports this package; importing @medusajs/workflows-sdk directly is discouraged.
At a glance
| Package | @medusajs/workflows-sdk |
| Type | Core library |
| Import from | @medusajs/framework/workflows-sdk |
| Entry points | ., ./composer (available as @medusajs/framework/workflows-sdk/composer) |
Features
createStepandStepResponseto define steps with an optional compensation function.createWorkflowandWorkflowResponseto compose steps into a workflow.transform,when, andparallelizeto manipulate data, run steps conditionally, and run steps in parallel inside a workflow.createHookto expose hooks that other code can consume to extend a workflow.
Installation
No separate installation is needed in a Medusa application. It's installed with @medusajs/framework.
Usage
import {
createStep,
createWorkflow,
StepResponse,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
type WorkflowInput = {
name: string
}
const greetStep = createStep(
"greet-step",
async ({ name }: WorkflowInput) => {
return new StepResponse(`Hello ${name}!`)
}
)
const helloWorldWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const message = greetStep(input)
return new WorkflowResponse({ message })
}
)
export default helloWorldWorkflow
Execute the workflow by passing it the Medusa container, for example in an API route:
const { result } = await helloWorldWorkflow(req.scope).run({
input: { name: "John" },
})