createWorkflow - Workflows API Reference

This documentation provides a reference to the createWorkflow . It belongs to the @medusajs/framework/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#

Code
1import {2  createWorkflow,3  WorkflowResponse4} from "@medusajs/framework/workflows-sdk"5import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"6import {7  createProductStep,8  getProductStep,9  createPricesStep10} from "./steps"11
12interface WorkflowInput {13 title: string14}15
16const myWorkflow = createWorkflow(17  "my-workflow",18  (input: WorkflowInput) => {19   // Everything here will be executed and resolved later20   // during the execution. Including the data access.21
22    const product = createProductStep(input)23    const prices = createPricesStep(product)24    return new WorkflowResponse(getProductStep(product.id))25  }26)27
28export async function GET(29  req: MedusaRequest,30  res: MedusaResponse31) {32  const { result: product } = await myWorkflow(req.scope)33    .run({34      input: {35        title: "Shirt"36      }37    })38
39  res.json({40    product41  })42}

Type Parameters#

TDataobjectOptional
The type of the input passed to the composer function.
TResultobjectOptional
The type of the output returned by the composer function.
THooksany[]Optional
The type of hooks defined in the workflow.

Parameters#

nameOrConfigstring | object & TransactionModelOptions
The name of the workflow or its configuration.
composer(input: WorkflowData<TData>) => void | WorkflowResponse<TResult, THooks>
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>
The created workflow. You can later execute the workflow by invoking it, then using its run method.
Was this page helpful?