Skip to content

How to understand a Medusa codebase

Medusa v2 is a commerce framework built from modules (product, cart, order and so on). Custom business logic is written as workflows made of steps with compensation for rollback, exposed through file-based API routes and triggered by events or scheduled jobs.

How to tell it's Medusa

  • @medusajs/* packages and medusa-config.ts
  • src/workflows/, src/api/, src/modules/ and src/subscribers/ folders
  • Calls to createWorkflow and createStep

Where things usually live

medusa-config.ts
Database, Redis, modules and plugins.
src/api/**/route.ts
File-based HTTP routes exporting GET, POST handlers.
src/workflows/
Workflows and steps; the main place for business logic.
src/modules/
Custom modules with their own data models and services.
src/links/
Links between modules' data models.
src/subscribers/
Handlers for events such as order.placed.
src/jobs/
Scheduled jobs.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. medusa-config.ts
  2. src/api/ routes for the feature
  3. The workflows those routes run

How a request moves through the code

  1. 1A request reaches a route in src/api/, after any middlewares.
  2. 2The route runs a workflow with the request input.
  3. 3Each step calls module services; if a later step fails, earlier steps run their compensation functions.
  4. 4The workflow emits events, which subscribers handle asynchronously.

Questions to ask about the repository

  • Which custom workflows exist, and what does each step do and undo?
  • What happens after an order is placed: which subscribers run?
  • Which modules are custom, and how are they linked to core modules?

What trips people up

  • Medusa v1 and v2 are structurally very different; v1 patterns like services/ with a service locator don't apply.
  • Workflow code is a composition, not a normal function body; plain if statements and await inside createWorkflow don't behave as you expect.