---
displayed_sidebar: workflowsSidebar
slug: /references/workflows/createWorkflow
sidebar_label: createWorkflow
---

import ParameterTypes from "@site/src/components/ParameterTypes"

# 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](workflows.createStep.mdx) 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

```ts
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

<ParameterTypes parameters={[
  {
    "name": "TData",
    "type": "`object`",
    "description": "The type of the input passed to the composer function.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "TResult",
    "type": "`object`",
    "description": "The type of the output returned by the composer function.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "THooks",
    "type": "`Record<string, Function>`",
    "description": "The type of hooks defined in the workflow.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

## Parameters

<ParameterTypes parameters={[
  {
    "name": "name",
    "type": "`string`",
    "description": "The name of the workflow.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  },
  {
    "name": "composer",
    "type": "(`input`: [WorkflowData](../types/workflows.WorkflowData.mdx)&#60;TData&#62;) => `void` \\| [WorkflowData](../types/workflows.WorkflowData.mdx)&#60;TResult&#62; \\| &#123; [K in string \\| number \\| symbol]: WorkflowDataProperties&#60;TResult[K]&#62; \\| WorkflowData&#60;TResult[K]&#62; &#125;",
    "description": "The constructor function that is executed when the `run` method in ReturnWorkflow is used.\nThe function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.\nYou'll have to use the [transform](workflows.transform.mdx) function if you need to directly manipulate data.",
    "optional": false,
    "defaultValue": "",
    "expandable": false,
    "children": []
  }
]} />

## Returns

<ParameterTypes parameters={[
  {
    "name": "ReturnWorkflow",
    "type": "ReturnWorkflow&#60;TData, TResult, THooks&#62;",
    "optional": false,
    "defaultValue": "",
    "description": "The created workflow. You can later execute the workflow by invoking it, then using its `run` method.",
    "expandable": false,
    "children": []
  }
]} />
