medusa-workflows
Build Medusa v2 workflows — steps with createStep, compensation/rollback, parallel execution, hooks for extending built-in workflows, and when conditions. Use when orchestrating multi-step operations.
Best use case
medusa-workflows is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Build Medusa v2 workflows — steps with createStep, compensation/rollback, parallel execution, hooks for extending built-in workflows, and when conditions. Use when orchestrating multi-step operations.
Teams using medusa-workflows should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/medusa-workflows/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How medusa-workflows Compares
| Feature / Agent | medusa-workflows | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Build Medusa v2 workflows — steps with createStep, compensation/rollback, parallel execution, hooks for extending built-in workflows, and when conditions. Use when orchestrating multi-step operations.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Medusa v2 Workflows
## Before writing code
**Fetch live docs**:
1. Fetch `https://docs.medusajs.com/learn/fundamentals/workflows` for workflow overview
2. Web-search `site:docs.medusajs.com createStep createWorkflow` for step and workflow API
3. Web-search `site:docs.medusajs.com workflow compensation rollback` for compensation patterns
4. Web-search `site:docs.medusajs.com workflow hooks` for extending built-in workflows
5. Web-search `site:docs.medusajs.com built-in workflows list` for available core workflows
## Workflow Concept
Workflows orchestrate multi-step, transactional operations in Medusa v2:
- Each step is an isolated, retryable unit of work
- Steps can define compensation (rollback) logic for failure recovery
- Workflows execute steps sequentially, in parallel, or conditionally
- Built-in workflows handle core commerce operations (cart, order, fulfillment)
- Custom workflows are invoked from API routes, subscribers, or scheduled jobs
## Step Lifecycle
| Phase | On Success | On Failure |
|-------|-----------|-----------|
| Step invoked | Execute function | — |
| Execute function | Proceed to next step | Trigger compensation |
| All steps done | Workflow complete | — |
| Compensation | Rollback completed steps in reverse order | Manual resolution |
## Creating Steps
### Step Definition Skeleton
```typescript
// Fetch live docs for createStep signature
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
const myStep = createStep("my-step", async (input, { container }) => {
const service = container.resolve("my-module")
const result = await service.createMyEntity(input)
return new StepResponse(result, result.id) // data, compensationInput
})
```
### Compensation (Rollback)
```typescript
// Fetch live docs for compensation function signature
const myStep = createStep("my-step",
async (input, { container }) => {
return new StepResponse(result, result.id) // forward logic
},
async (compensationInput, { container }) => {
// Rollback logic: undo what forward did
})
```
## Creating Workflows
### Workflow Definition Skeleton
```typescript
// Fetch live docs for createWorkflow API
import { createWorkflow, WorkflowResponse }
from "@medusajs/framework/workflows-sdk"
const myWorkflow = createWorkflow("my-workflow", (input) => {
const stepResult = myStep(input)
return new WorkflowResponse(stepResult)
})
```
## Execution Patterns
| Pattern | API | Purpose |
|---------|-----|---------|
| Sequential | Default step ordering | Steps run one after another |
| Parallel | `parallelize(stepA, stepB)` | Steps run concurrently |
| Conditional | `when(condition, () => step())` | Skip steps based on data |
| Transform | `transform(data, fn)` | Transform data between steps |
### Parallel Execution
```typescript
// Fetch live docs for parallelize import
import { parallelize } from "@medusajs/framework/workflows-sdk"
const [resultA, resultB] = parallelize(stepA(input), stepB(input))
```
### Conditional Execution
```typescript
// Fetch live docs for when() API
import { when } from "@medusajs/framework/workflows-sdk"
when(input, (data) => data.sendEmail === true, () => {
return sendEmailStep(input)
})
```
## Invoking Workflows
| Context | Invocation Pattern |
|---------|-------------------|
| API Route | `await myWorkflow(req.scope).run({ input })` |
| Subscriber | `await myWorkflow(container).run({ input })` |
| Scheduled Job | `await myWorkflow(container).run({ input })` |
| Another Workflow | Use as a step with `createStep` wrapping |
## Workflow Hooks
Hooks allow extending built-in workflows without modifying core code:
| Hook Type | Purpose |
|-----------|---------|
| `before` | Run custom logic before a built-in step |
| `after` | Run custom logic after a built-in step |
### Hook Registration Skeleton
```typescript
// src/workflows/hooks/my-hook.ts
// Fetch live docs for hook registration API
import { createProductsWorkflow } from "@medusajs/medusa/core-flows"
createProductsWorkflow.hooks.productsCreated(
async ({ products, additional_data }, { container }) => {})
// Fetch live docs for available hook names per workflow
```
## Built-in Workflows (Key Examples)
| Workflow | Domain | Key Hooks |
|----------|--------|-----------|
| `createProductsWorkflow` | Catalog | `productsCreated` |
| `updateProductsWorkflow` | Catalog | `productsUpdated` |
| `createCartWorkflow` | Cart | `cartCreated` |
| `completeCartWorkflow` | Checkout | `cartCompleted` |
| `createOrderWorkflow` | Orders | `orderCreated` |
| `createFulfillmentWorkflow` | Fulfillment | `fulfillmentCreated` |
| `createPaymentCollectionWorkflow` | Payments | hook available |
| `createCustomerAccountWorkflow` | Customers | hook available |
> **Fetch live docs** for the complete list -- new workflows and hooks are added in each Medusa release.
## Compensation Strategy
| Scenario | Compensation Action |
|----------|-------------------|
| Record created | Delete the created record |
| Payment captured | Refund the payment |
| Inventory reserved | Release the reservation |
| Email sent | Log warning (cannot unsend) |
| External API called | Call reverse/cancel endpoint |
Key rules:
- Every step that creates a side effect should define compensation
- Compensation runs in reverse order of step completion
- Compensation input is the second argument to `StepResponse`
- If compensation itself fails, the workflow enters a "failed" state requiring manual resolution
## Best Practices
- Keep steps small and focused -- one side effect per step
- Always define compensation for steps with side effects
- Use `parallelize` for independent steps to improve performance
- Use `when` for conditional logic -- do not put branching inside steps
- Resolve services from the container, never import them directly
- Name workflows and steps descriptively (e.g., `reserve-inventory-step`)
- Use `transform` for data reshaping -- do not create steps that only transform data
Fetch the Medusa workflow documentation for exact createStep/createWorkflow signatures, hook names, and built-in workflow list before implementing.Related Skills
medusa-testing
Test Medusa v2 applications — Jest setup, module unit tests, workflow integration tests, API route tests, medusaIntegrationTestRunner, and mock patterns. Use when writing tests for Medusa projects.
medusa-subscribers
Implement Medusa v2 event subscribers — pub/sub event handling, subscriber handler functions, scheduled jobs with cron, and the event module (Redis or in-memory). Use when reacting to commerce events.
medusa-storefront
Build Medusa v2 storefronts with Next.js 15 — App Router, JS SDK client setup, Tanstack Query, server components, product pages, cart, and checkout flow. Use when developing headless storefronts.
medusa-setup
Set up a Medusa v2 development environment — CLI, PostgreSQL/Redis prerequisites, project creation, medusa-config.ts, directory structure, environment variables. Use when starting a new Medusa project.
medusa-security
Secure Medusa v2 applications — authentication strategies, API key types (publishable vs secret), CORS configuration, JWT and cookie secrets, admin vs store auth, and session management. Use when configuring security.
medusa-pricing
Configure Medusa v2 pricing — pricing module, price lists with rules, currencies, tax calculation, regions, and promotion campaigns. Use when setting up pricing and discounts.
medusa-plugins
Develop and publish Medusa v2 plugins — plugin structure, plugin vs module comparison, npm packaging, and reusable plugin template. Use when building distributable Medusa extensions.
medusa-payments
Implement Medusa v2 payment processing — payment module, provider abstraction, payment sessions, authorization/capture/refund lifecycle, and Stripe/PayPal integration. Use when adding payment providers.
medusa-orders
Manage Medusa v2 orders — order lifecycle and state machine, fulfillment workflows, returns, exchanges, claims, draft orders, and order editing. Use when working with order processing.
medusa-modules
Build custom Medusa v2 modules — DML data models, services extending MedusaService, loaders, module links, and container registration. Use when creating custom modules.
medusa-fulfillment
Implement Medusa v2 fulfillment — fulfillment module, provider interface, shipping options, fulfillment sets, shipping profiles, and multi-warehouse support. Use when adding fulfillment providers.
medusa-deploy
Deploy Medusa v2 to production — build process, server vs worker mode, environment variables, hosting options, Redis caching, database configuration, and production checklist. Use when deploying Medusa applications.