createStep - Workflows API Reference

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

This function creates a StepFunction that can be used as a step in a workflow constructed by the createWorkflow function.

Example#

Code
1import {2  createStep,3  StepResponse4} from "@medusajs/framework/workflows-sdk"5
6interface CreateProductInput {7  title: string8}9
10export const createProductStep = createStep(11  "createProductStep",12  async function (13    input: CreateProductInput,14    context15  ) {16    const productService = context.container.resolve(17      "productService"18    )19    const product = await productService.createProducts(input)20    return new StepResponse({21      product22    }, {23      product_id: product.id24    })25  },26  async function (27    input,28    context29  ) {30    const productService = context.container.resolve(31      "productService"32    )33    await productService.deleteProducts(input.product_id)34  }35)

Type Parameters#

TInvokeInputobjectOptional
The type of the expected input parameter to the invocation function.
TInvokeResultOutputobjectOptional
The type of the expected output parameter of the invocation function.
TInvokeResultCompensateInputobjectOptional
The type of the expected input parameter to the compensation function.

Parameters#

nameOrConfigstring | object & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">
The name of the step or its configuration.
invokeFnInvokeFn<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>
An invocation function that will be executed when the workflow is executed. The function must return an instance of StepResponse. The constructor of StepResponse accepts the output of the step as a first argument, and optionally as a second argument the data to be passed to the compensation function as a parameter.
compensateFnCompensateFn<TInvokeResultCompensateInput>Optional
A compensation function that's executed if an error occurs in the workflow. It's used to roll-back actions when errors occur. It accepts as a parameter the second argument passed to the constructor of the StepResponse instance returned by the invocation function. If the invocation function doesn't pass the second argument to StepResponse constructor, the compensation function receives the first argument passed to the StepResponse constructor instead.

Returns#

StepFunctionStepFunction<TInvokeInput, TInvokeResultOutput>
A step function to be used in a workflow.
Was this page helpful?