npm.io
2.22.7 • Published 4d ago

@medusajs/core-flows

Licence
SEE LICENSE IN LICENSE
Version
2.22.7
Deps
3
Size
4.8 MB
Vulns
0
Weekly
0
Stars
36.6K

Medusa logo

@medusajs/core-flows

Documentation | Website

Building blocks for digital commerce

Medusa is released under the MIT license. PRs welcome! Discord Chat Follow @medusajs

Medusa's built-in commerce workflows and steps: the business flows behind the Medusa API routes, such as creating products, completing carts, and managing orders, returns, and exchanges. Use them in API routes, subscribers, scheduled jobs, or your own workflows to reuse Medusa's logic, or consume their hooks to extend it. Medusa applications import them from @medusajs/medusa/core-flows, which re-exports this package.

At a glance

Package @medusajs/core-flows
Type Core library
Import from @medusajs/medusa/core-flows

Features

  • Workflows and steps grouped by commerce domain under src/<domain>/, such as cart, order, product, payment, fulfillment, inventory, pricing, promotion, customer, and auth.
  • Each domain has a workflows/ directory and a steps/ directory. locking and notification only have steps.
  • common holds generic steps used across domains, such as useQueryGraphStep, createRemoteLinkStep, and emitEventStep.
  • Naming convention: workflows end in Workflow (for example, createProductsWorkflow), steps end in Step (for example, createProductsStep), and each has a matching ID constant (for example, createProductsWorkflowId).
  • Many workflows expose hooks, such as createProductsWorkflow.hooks.productsCreated, to run custom logic within the workflow.

Installation

No separate installation is needed in a Medusa application. It's installed with @medusajs/medusa.

Usage

Run a workflow by passing it the Medusa container, then calling run:

import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { createProductsWorkflow } from "@medusajs/medusa/core-flows"

export async function POST(req: MedusaRequest, res: MedusaResponse) {
  const { result } = await createProductsWorkflow(req.scope).run({
    input: {
      products: [
        {
          title: "Shirt",
        },
      ],
    },
  })

  res.json({ products: result })
}

Steps, such as useQueryGraphStep, are used inside your own workflows created with createWorkflow from @medusajs/framework/workflows-sdk.

Resources