Skip to main content
Skip to main content

createStep - Workflows API Reference

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

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

Example

import {
createStep,
StepResponse,
StepExecutionContext,
WorkflowData
} from "@medusajs/workflows-sdk"

interface CreateProductInput {
title: string
}

export const createProductStep = createStep(
"createProductStep",
async function (
input: CreateProductInput,
context
) {
const productService = context.container.resolve(
"productService"
)
const product = await productService.create(input)
return new StepResponse({
product
}, {
product_id: product.id
})
},
async function (
input,
context
) {
const productService = context.container.resolve(
"productService"
)
await productService.delete(input.product_id)
}
)

Type Parameters

TInvokeInputobjectRequired
The type of the expected input parameter to the invocation function.
TInvokeResultOutputobjectRequired
The type of the expected output parameter of the invocation function.
TInvokeResultCompensateInputobjectRequired
The type of the expected input parameter to the compensation function.

Parameters

nameOrConfigstring | object & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">Required
The name of the step or its configuration.
invokeFnInvokeFn<TInvokeInput, TInvokeResultOutput, TInvokeResultCompensateInput>Required
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>
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>Required
A step function to be used in a workflow.
Was this section helpful?