Skip to main content
Skip to main content

createWorkflow - Workflows API Reference

This documentation provides a reference to the createWorkflow . It belongs to the @medusajs/workflows-sdk package.

This function creates a workflow with the provided name and a constructor function. The constructor function builds the workflow from steps created by the createStep function. The returned workflow is an exported workflow of type ReturnWorkflow, meaning it's not executed right away. To execute it, invoke the exported workflow, then run its run method.

Example

import { createWorkflow } from "@medusajs/workflows-sdk"
import { MedusaRequest, MedusaResponse, Product } from "@medusajs/medusa"
import {
createProductStep,
getProductStep,
createPricesStep
} from "./steps"

interface WorkflowInput {
title: string
}

const myWorkflow = createWorkflow<
WorkflowInput,
Product
>("my-workflow", (input) => {
// Everything here will be executed and resolved later
// during the execution. Including the data access.

const product = createProductStep(input)
const prices = createPricesStep(product)
return getProductStep(product.id)
}
)

export async function GET(
req: MedusaRequest,
res: MedusaResponse
) {
const { result: product } = await myWorkflow(req.scope)
.run({
input: {
title: "Shirt"
}
})

res.json({
product
})
}

Type Parameters

TDataobjectRequired
The type of the input passed to the composer function.
TResultobjectRequired
The type of the output returned by the composer function.
THooksRecord<string, Function>Required
The type of hooks defined in the workflow.

Parameters

nameOrConfigstring | object & TransactionModelOptionsRequired
The name of the workflow or its configuration.
composer(input: WorkflowData<TData>) => void | WorkflowData<TResult> | { [K in string | number | symbol]: WorkflowDataProperties<TResult[K]> | WorkflowData<TResult[K]> }Required
The constructor function that is executed when the run method in ReturnWorkflow is used. The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data. You'll have to use the transform function if you need to directly manipulate data.

Returns

ReturnWorkflowReturnWorkflow<TData, TResult, THooks>Required
The created workflow. You can later execute the workflow by invoking it, then using its run method.
Was this section helpful?