building-storefronts

作者: medusajs

Load automatically when planning, researching, or implementing Medusa storefront features (calling custom API routes, SDK integration, React Query patterns,…

npx skills add https://github.com/medusajs/medusa-agent-skills --skill building-storefronts

Medusa Storefront Development

Frontend integration guide for building storefronts with Medusa. Covers SDK usage, React Query patterns, and calling custom API routes.

When to Apply

Load this skill for ANY storefront development task, including:

  • Calling custom Medusa API routes from the storefront
  • Integrating Medusa SDK in frontend applications
  • Using React Query for data fetching
  • Implementing mutations with optimistic updates
  • Error handling and cache invalidation

Also load building-with-medusa when: Building the backend API routes that the storefront calls

CRITICAL: Load Reference Files When Needed

The quick reference below is NOT sufficient for implementation. You MUST load the reference file before writing storefront integration code.

Load this reference when implementing storefront features:

  • Calling API routes? → MUST load references/frontend-integration.md first
  • Using SDK? → MUST load references/frontend-integration.md first
  • Implementing React Query? → MUST load references/frontend-integration.md first

Rule Categories by Priority

PriorityCategoryImpactPrefix
1SDK UsageCRITICALsdk-
2React Query PatternsHIGHquery-
3Data DisplayHIGH (includes CRITICAL price rule)display-
4Error HandlingMEDIUMerror-

Quick Reference

1. SDK Usage (CRITICAL)

  • sdk-always-use - ALWAYS use the Medusa JS SDK for ALL API requests - NEVER use regular fetch()
  • sdk-existing-methods - For built-in endpoints, use existing SDK methods (sdk.store.product.list(), sdk.admin.order.retrieve())
  • sdk-client-fetch - For custom API routes, use sdk.client.fetch()
  • sdk-required-headers - SDK automatically adds required headers (publishable API key for store, auth for admin) - regular fetch() missing these headers causes errors
  • sdk-no-json-stringify - NEVER use JSON.stringify() on body - SDK handles serialization automatically
  • sdk-plain-objects - Pass plain JavaScript objects to body, not strings
  • sdk-locate-first - Always locate where SDK is instantiated in the project before using it

2. React Query Patterns (HIGH)

  • query-use-query - Use useQuery for GET requests (data fetching)
  • query-use-mutation - Use useMutation for POST/DELETE requests (mutations)
  • query-invalidate - Invalidate queries in onSuccess to refresh data after mutations
  • query-keys-hierarchical - Structure query keys hierarchically for effective cache management
  • query-loading-states - Always handle isLoading, isPending, isError states

3. Data Display (HIGH)

  • display-price-format - CRITICAL: Prices from Medusa are stored as-is ($49.99 = 49.99, NOT in cents). Display them directly - NEVER divide by 100

4. Error Handling (MEDIUM)

  • error-on-error - Implement onError callback in mutations to handle failures
  • error-display - Show error messages to users when mutations fail
  • error-rollback - Use optimistic updates with rollback on error for better UX

Critical SDK Pattern

ALWAYS pass plain objects to the SDK - NEVER use JSON.stringify():

// ✅ CORRECT - Plain object
await sdk.client.fetch("/store/reviews", {
  method: "POST",
  body: {
    product_id: "prod_123",
    rating: 5,
  }
})

// ❌ WRONG - JSON.stringify breaks the request
await sdk.client.fetch("/store/reviews", {
  method: "POST",
  body: JSON.stringify({  // ❌ DON'T DO THIS!
    product_id: "prod_123",
    rating: 5,
  })
})

Why this matters:

  • The SDK handles JSON serialization automatically
  • Using JSON.stringify() will double-serialize and break the request
  • The server won't be able to parse the body

Common Mistakes Checklist

Before implementing, verify you're NOT doing these:

SDK Usage:

  • Using regular fetch() instead of the Medusa JS SDK (causes missing header errors)
  • Not using existing SDK methods for built-in endpoints (e.g., using sdk.client.fetch("/store/products") instead of sdk.store.product.list())
  • Using JSON.stringify() on the body parameter
  • Manually setting Content-Type headers (SDK adds them)
  • Hardcoding SDK import paths (locate in project first)
  • Not using sdk.client.fetch() for custom routes

React Query:

  • Not invalidating queries after mutations
  • Using flat query keys instead of hierarchical
  • Not handling loading and error states
  • Forgetting to disable buttons during mutations (isPending)

Data Display:

  • CRITICAL: Dividing prices by 100 when displaying (prices are stored as-is: $49.99 = 49.99, NOT in cents)

Error Handling:

  • Not implementing onError callbacks
  • Not showing error messages to users
  • Not handling network failures gracefully

How to Use

For detailed patterns and examples, load reference file:

references/frontend-integration.md - SDK usage, React Query patterns, API integration

The reference file contains:

  • Step-by-step SDK integration patterns
  • Complete React Query examples
  • Correct vs incorrect code examples
  • Query key best practices
  • Optimistic update patterns
  • Error handling strategies

When to Use MedusaDocs MCP Server

Use this skill for (PRIMARY SOURCE):

  • How to call custom API routes from storefront
  • SDK usage patterns (sdk.client.fetch)
  • React Query integration patterns
  • Common mistakes and anti-patterns

Use MedusaDocs MCP server for (SECONDARY SOURCE):

  • Built-in SDK methods (sdk.admin., sdk.store.)
  • Official Medusa SDK API reference
  • Framework-specific configuration options

Why skills come first:

  • Skills contain critical patterns like "don't use JSON.stringify" that MCP doesn't emphasize
  • Skills show correct vs incorrect patterns; MCP shows what's possible
  • Planning requires understanding patterns, not just API reference

Integration with Backend

⚠️ CRITICAL: ALWAYS use the Medusa JS SDK - NEVER use regular fetch()

When building features that span backend and frontend:

  1. Backend (building-with-medusa skill): Module → Workflow → API Route
  2. Storefront (this skill): SDK → React Query → UI Components
  3. Connection:
    • Built-in endpoints: Use existing SDK methods (sdk.store.product.list())
    • Custom API routes: Use sdk.client.fetch("/store/my-route")
    • NEVER use regular fetch() - missing publishable API key causes errors

Why the SDK is required:

  • Store routes need x-publishable-api-key header
  • Admin routes need Authorization and session headers
  • SDK handles all required headers automatically
  • Regular fetch() without headers → authentication/authorization errors

See building-with-medusa for backend API route patterns.

来自 medusajs 的更多技能

mcloud-variables
medusajs
执行mcloud变量命令,列出并获取云环境的环境变量。用于检查、读取或导出环境…
official
building-storefronts
medusajs
以SDK为先的前端集成方案,适用于Medusa商店前端,采用React Query模式并遵循关键API调用规则。所有API请求必须使用Medusa JS SDK——禁止使用常规fetch(),因其缺少必要请求头(商店路由需可发布API密钥,管理路由需认证信息)。向SDK方法传递纯JavaScript对象,切勿对请求体参数使用JSON.stringify(),SDK会自动处理序列化。GET请求使用useQuery,POST/DELETE请求使用useMutation,...
official
building-admin-dashboard-customizations
medusajs
使用管理SDK和Medusa UI组件为Medusa管理后台定制的UI扩展。对于任何管理后台UI工作(规划、实施、探索),请优先加载此技能;MCP服务器仅提供API参考,不包含设计模式或数据加载策略。关键:始终使用Medusa JS SDK进行所有API请求(切勿使用常规fetch);将显示查询与模态查询分离,并在变更后使显示数据失效。在现有页面上实现小部件或创建自定义UI路由;...
official
learning-medusa
medusajs
交互式逐步Medusa开发训练营,在构建品牌功能的同时学习架构模式。包含三个渐进式课程(总计2-3小时),涵盖模块、工作流、API路由、模块链接、工作流钩子及管理界面自定义。每个主要组件完成后进行检查点验证,测试概念理解、代码质量和功能实现。将错误视为教学机会,通过诊断性问题共同调试并定位根本原因……
official
db-migrate
medusajs
执行待处理的Medusa数据库迁移并报告结果。通过Bash运行npx medusa db:migrate,将所有待处理的迁移应用到Medusa数据库。报告迁移结果,包括已应用的迁移数量、遇到的任何错误以及成功确认。专为具有标准npm/npx设置的Medusa项目设计。
official
mcloud-environments
medusajs
执行mcloud environments命令,列出、获取、创建、删除、重新部署或触发云环境的构建。用于管理环境生命周期时使用…
official
db-generate
medusajs
通过单个命令为Medusa模块生成数据库迁移。封装了npx medusa db:generate CLI命令,用于为指定的Medusa模块创建迁移文件。接受模块名称作为参数,并报告迁移文件位置、错误及后续步骤。生成后自动建议运行npx medusa db:migrate以应用迁移。
official
mcloud-deployments
medusajs
执行 mcloud deployments 命令以列出部署、获取部署详情并获取构建日志。在列出部署、检查部署…时使用
official