building-with-medusa
Original:🇺🇸 English
Translated
Load automatically when planning, researching, or implementing ANY Medusa backend features (custom modules, API routes, workflows, data models, module links, business logic). REQUIRED for all Medusa backend work in ALL modes (planning, implementation, exploration). Contains architectural patterns, best practices, and critical rules that MCP servers don't provide.
7installs
Added on
NPX Install
npx skill4agent add medusajs/medusa-claude-plugins building-with-medusaTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Medusa Backend Development
Comprehensive backend development guide for Medusa applications. Contains patterns across 6 categories covering architecture, type safety, business logic placement, and common pitfalls.
When to Apply
Load this skill for ANY backend development task, including:
- Creating or modifying custom modules and data models
- Implementing workflows for mutations
- Building API routes (store or admin)
- Defining module links between entities
- Writing business logic or validation
- Querying data across modules
- Implementing authentication/authorization
Also load these skills when:
- building-admin-dashboard-customizations: Building admin UI (widgets, pages, forms)
- building-storefronts: Calling backend API routes from storefronts (SDK integration)
CRITICAL: Load Reference Files When Needed
The quick reference below is NOT sufficient for implementation. You MUST load relevant reference files before writing code for that component.
Load these references based on what you're implementing:
- Creating a module? → MUST load first
reference/custom-modules.md - Creating workflows? → MUST load first
reference/workflows.md - Creating API routes? → MUST load first
reference/api-routes.md - Creating module links? → MUST load first
reference/module-links.md - Querying data? → MUST load first
reference/querying-data.md - Adding authentication? → MUST load first
reference/authentication.md
Minimum requirement: Load at least 1-2 reference files relevant to your specific task before implementing.
Critical Architecture Pattern
ALWAYS follow this flow - never bypass layers:
Module (data models + CRUD operations)
↓ used by
Workflow (business logic + mutations with rollback)
↓ executed by
API Route (HTTP interface, validation middleware)
↓ called by
Frontend (admin dashboard/storefront via SDK)Key conventions:
- Only GET, POST, DELETE methods (never PUT/PATCH)
- Workflows are required for ALL mutations
- Business logic belongs in workflow steps, NOT routes
- Query with for cross-module data retrieval
query.graph() - Query with (Index Module) for filtering across separate modules with links
query.index() - Module links maintain isolation between modules
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Architecture Violations | CRITICAL | |
| 2 | Type Safety | CRITICAL | |
| 3 | Business Logic Placement | HIGH | |
| 4 | Import & Code Organization | HIGH | |
| 5 | Data Access Patterns | MEDIUM (includes CRITICAL price rule) | |
| 6 | File Organization | MEDIUM | |
Quick Reference
1. Architecture Violations (CRITICAL)
- - Use workflows for ALL mutations, never call module services from routes
arch-workflow-required - - Never bypass layers (route → service without workflow)
arch-layer-bypass - - Use only GET, POST, DELETE (never PUT/PATCH)
arch-http-methods - - Use module links, not direct cross-module service calls
arch-module-isolation - - Don't set explicit
arch-query-config-fieldswhen usingfieldsreq.queryConfig
2. Type Safety (CRITICAL)
- - Pass Zod inferred type to
type-request-schemawhen usingMedusaRequest<T>req.validatedBody - - Use
type-authenticated-requestfor protected routes (notAuthenticatedMedusaRequest)MedusaRequest - - Export both Zod schema AND inferred type from middlewares
type-export-schema - - Never add
type-linkable-autoto data models (automatically added).linkable() - - Module names MUST be camelCase, never use dashes (causes runtime errors)
type-module-name-camelcase
3. Business Logic Placement (HIGH)
- - Put business validation in workflow steps, not API routes
logic-workflow-validation - - Validate ownership/permissions in workflows, not routes
logic-ownership-checks - - Keep modules simple (CRUD only), put logic in workflows
logic-module-service
4. Import & Code Organization (HIGH)
- - Import workflows/modules at file top, never use
import-top-levelin route bodyawait import() - - Use static imports for all dependencies
import-static-only - - Dynamic imports add overhead and break type checking
import-no-dynamic-routes
5. Data Access Patterns (MEDIUM)
- - CRITICAL: Prices are stored as-is in Medusa (49.99 stored as 49.99, NOT in cents). Never multiply by 100 when saving or divide by 100 when displaying
data-price-format - - Use
data-query-methodfor retrieving data; usequery.graph()(Index Module) for filtering across linked modulesquery.index() - - Use
data-query-graphfor cross-module queries with dot notation (without cross-module filtering)query.graph() - - Use
data-query-indexwhen filtering by properties of linked data models in separate modulesquery.index() - - Use
data-list-and-countfor single-module paginated querieslistAndCount - -
data-linked-filteringcan't filter by linked module fields - usequery.graph()or query from that entity directlyquery.index() - - Don't use JavaScript
data-no-js-filteron linked data - use database filters (.filter()or query the entity)query.index() - - Can filter by same-module relations with
data-same-module-ok(e.g., product.variants)query.graph() - - Trust
data-auth-middlewaremiddleware, don't manually checkauthenticatereq.auth_context
6. File Organization (MEDIUM)
- - Recommended: Create steps in
file-workflow-stepssrc/workflows/steps/[name].ts - - Composition functions in
file-workflow-compositionsrc/workflows/[name].ts - - Export schemas and types from middleware files
file-middleware-exports - - Define module links in
file-links-directorysrc/links/[name].ts
Workflow Composition Rules
The workflow function has critical constraints:
typescript
// ✅ CORRECT
const myWorkflow = createWorkflow(
"name",
function (input) { // Regular function, not async, not arrow
const result = myStep(input) // No await
return new WorkflowResponse(result)
}
)
// ❌ WRONG
const myWorkflow = createWorkflow(
"name",
async (input) => { // ❌ No async, no arrow functions
const result = await myStep(input) // ❌ No await
if (input.condition) { /* ... */ } // ❌ No conditionals
return new WorkflowResponse(result)
}
)Constraints:
- No async/await (runs at load time)
- No arrow functions (use )
function - No conditionals/ternaries (use )
when() - No variable manipulation (use )
transform() - No date creation (use )
transform() - Multiple step calls need to avoid conflicts
.config({ name: "unique-name" })
Common Mistakes Checklist
Before implementing, verify you're NOT doing these:
Architecture:
- Calling module services directly from API routes
- Using PUT or PATCH methods
- Bypassing workflows for mutations
- Setting explicitly with
fieldsreq.queryConfig - Skipping migrations after creating module links
Type Safety:
- Forgetting type argument
MedusaRequest<SchemaType> - Using instead of
MedusaRequestfor protected routesAuthenticatedMedusaRequest - Not exporting Zod inferred type from middlewares
- Adding to data models
.linkable() - Using dashes in module names (must be camelCase)
Business Logic:
- Validating business rules in API routes
- Checking ownership in routes instead of workflows
- Manually checking when middleware already applied
req.auth_context?.actor_id
Imports:
- Using in route handler bodies
await import() - Dynamic imports for workflows or modules
Data Access:
- CRITICAL: Multiplying prices by 100 when saving or dividing by 100 when displaying (prices are stored as-is: $49.99 = 49.99)
- Filtering by linked module fields with (use
query.graph()or query from other side instead)query.index() - Using JavaScript on linked data (use
.filter()or query the linked entity directly)query.index() - Not using for cross-module data retrieval
query.graph() - Using when you need to filter across separate modules (use
query.graph()instead)query.index()
Validating Implementation
CRITICAL: Always run the build command after completing implementation to catch type errors and runtime issues.
When to Validate
- After implementing any new feature
- After making changes to modules, workflows, or API routes
- Before marking tasks as complete
- Proactively, without waiting for the user to ask
How to Run Build
Detect the package manager and run the appropriate command:
bash
npm run build # or pnpm build / yarn buildHandling Build Errors
If the build fails:
- Read the error messages carefully
- Fix type errors, import issues, and syntax errors
- Run the build again to verify the fix
- Do NOT mark implementation as complete until build succeeds
Common build errors:
- Missing imports or exports
- Type mismatches (e.g., missing type argument)
MedusaRequest<T> - Incorrect workflow composition (async functions, conditionals)
Next Steps - Testing Your Implementation
After successfully implementing a feature, always provide these next steps to the user:
1. Start the Development Server
If the server isn't already running, start it:
bash
npm run dev # or pnpm dev / yarn dev2. Access the Admin Dashboard
Open your browser and navigate to:
- Admin Dashboard: http://localhost:9000/app
Log in with your admin credentials to test any admin-related features.
3. Test API Routes
If you implemented custom API routes, list them for the user to test:
Admin Routes (require authentication):
- - Description of what it does
POST http://localhost:9000/admin/[your-route] - - Description of what it does
GET http://localhost:9000/admin/[your-route]
Store Routes (public or customer-authenticated):
- - Description of what it does
POST http://localhost:9000/store/[your-route] - - Description of what it does
GET http://localhost:9000/store/[your-route]
Testing with cURL example:
bash
# Admin route (requires authentication)
curl -X POST http://localhost:9000/admin/reviews/123/approve \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
--cookie "connect.sid=YOUR_SESSION_COOKIE"
# Store route
curl -X POST http://localhost:9000/store/reviews \
-H "Content-Type: application/json" \
-d '{"product_id": "prod_123", "rating": 5, "comment": "Great product!"}'4. Additional Testing Steps
Depending on what was implemented, mention:
- Workflows: Test mutation operations and verify rollback on errors
- Subscribers: Trigger events and check logs for subscriber execution
- Scheduled jobs: Wait for job execution or check logs for cron output
Format for Presenting Next Steps
Always present next steps in a clear, actionable format after implementation:
markdown
## Implementation Complete
The [feature name] has been successfully implemented. Here's how to test it:
### Start the Development Server
[server start command based on package manager]
### Access the Admin Dashboard
Open http://localhost:9000/app in your browser
### Test the API Routes
I've added the following routes:
**Admin Routes:**
- POST /admin/[route] - [description]
- GET /admin/[route] - [description]
**Store Routes:**
- POST /store/[route] - [description]
### What to Test
1. [Specific test case 1]
2. [Specific test case 2]
3. [Specific test case 3]How to Use
For detailed patterns and examples, load reference files:
reference/custom-modules.md - Creating modules with data models
reference/workflows.md - Workflow creation and step patterns
reference/api-routes.md - API route structure and validation
reference/module-links.md - Linking entities across modules
reference/querying-data.md - Query patterns and filtering rules
reference/authentication.md - Protecting routes and accessing users
reference/error-handling.md - MedusaError types and patterns
reference/scheduled-jobs.md - Cron jobs and periodic tasks
reference/subscribers-and-events.md - Event handling
reference/troubleshooting.md - Common errors and solutionsEach reference file contains:
- Step-by-step implementation checklists
- Correct vs incorrect code examples
- TypeScript patterns and type safety
- Common pitfalls and solutions
When to Use This Skill vs MedusaDocs MCP Server
⚠️ CRITICAL: This skill should be consulted FIRST for planning and implementation.
Use this skill for (PRIMARY SOURCE):
- Planning - Understanding how to structure Medusa backend features
- Architecture - Module → Workflow → API Route patterns
- Best practices - Correct vs incorrect code patterns
- Critical rules - What NOT to do (common mistakes and anti-patterns)
- Implementation patterns - Step-by-step guides with checklists
Use MedusaDocs MCP server for (SECONDARY SOURCE):
- Specific method signatures after you know which method to use
- Built-in module configuration options
- Official type definitions
- Framework-level configuration details
Why skills come first:
- Skills contain opinionated guidance and anti-patterns MCP doesn't have
- Skills show architectural patterns needed for planning
- MCP is reference material; skills are prescriptive guidance
Integration with Frontend Applications
When building features that span backend and frontend:
For Admin Dashboard:
- Backend (this skill): Module → Workflow → API Route
- Frontend: Load skill
building-admin-dashboard-customizations - Connection: Admin widgets call custom API routes via
sdk.client.fetch()
For Storefronts:
- Backend (this skill): Module → Workflow → API Route
- Frontend: Load skill
building-storefronts - Connection: Storefront calls custom API routes via
sdk.client.fetch()
See respective frontend skills for complete integration patterns.