building-storefronts

Integração frontend SDK-first para storefronts Medusa com padrões React Query e regras críticas de chamadas de API. Sempre use o Medusa JS SDK para todas as requisições de API — nunca use fetch() comum, pois ele não possui os cabeçalhos necessários (chave de API publicável para rotas de loja, autenticação para rotas de admin). Passe objetos JavaScript simples para os métodos do SDK; nunca use JSON.stringify() nos parâmetros do corpo, pois o SDK lida com a serialização automaticamente. Use useQuery para requisições GET e useMutation para requisições POST/DELETE,...

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.

Mais skills de medusajs

mcloud-variables
medusajs
Executar comandos de variáveis mcloud para listar e obter variáveis de ambiente para um ambiente Cloud. Use ao inspecionar, ler ou exportar variáveis de ambiente…
official
building-admin-dashboard-customizations
medusajs
Extensões de UI personalizadas para o painel administrativo do Medusa usando o Admin SDK e componentes de UI do Medusa. Carregue esta skill PRIMEIRO para qualquer trabalho de UI administrativa (planejamento, implementação, exploração); servidores MCP fornecem apenas referência de API, não padrões de design ou estratégias de carregamento de dados CRÍTICO: Sempre use o Medusa JS SDK para todas as requisições de API (nunca fetch comum); separe consultas de exibição de consultas modais e invalide dados de exibição após mutações Implemente widgets em páginas existentes ou crie rotas de UI personalizadas;...
official
learning-medusa
medusajs
Bootcamp interativo passo a passo de desenvolvimento Medusa, onde você constrói uma funcionalidade de marcas enquanto aprende padrões de arquitetura. Três lições progressivas (2–3 horas no total) abordando módulos, workflows, rotas de API, links de módulos, hooks de workflow e personalização da interface administrativa. Verificação de checkpoint após cada componente principal testa compreensão conceitual, qualidade de código e funcionalidade antes de prosseguir. Trata erros como oportunidades de ensino; depura em conjunto com perguntas diagnósticas e análise de causa raiz...
official
db-migrate
medusajs
Executa migrações pendentes do banco de dados Medusa e relata os resultados. Executa npx medusa db:migrate via Bash para aplicar todas as migrações pendentes ao seu banco de dados Medusa. Relata os resultados das migrações, incluindo a quantidade de migrações aplicadas, quaisquer erros encontrados e confirmação de sucesso. Projetado para projetos Medusa com configuração padrão npm/npx.
official
mcloud-environments
medusajs
Execute comandos de ambientes mcloud para listar, obter, criar, excluir, reimplantar ou acionar builds para ambientes Cloud. Use ao gerenciar o ciclo de vida de ambientes,…
official
db-generate
medusajs
Gera migrações de banco de dados para módulos Medusa com um único comando. Encapsula o comando CLI npx medusa db:generate para criar arquivos de migração para módulos Medusa especificados. Aceita o nome do módulo como argumento e informa a localização do arquivo de migração, erros e próximos passos. Sugere automaticamente executar npx medusa db:migrate após a geração para aplicar as migrações.
official
mcloud-deployments
medusajs
Execute mcloud deployments commands to list deployments, retrieve deployment details, and fetch build logs. Use when listing deployments, checking deployment…
official
building-with-medusa
medusajs
Guia abrangente para arquitetura backend Medusa, fluxos de trabalho e regras críticas de implementação. Abrange seis categorias de regras (arquitetura, segurança de tipos, posicionamento de lógica de negócios, importações, acesso a dados, organização de arquivos) com antipadrões específicos e verificações de conformidade. Impõe separação estrita de camadas: Módulo → Workflow → Rota de API → Frontend, com workflows obrigatórios para todas as mutações e apenas métodos HTTP GET/POST/DELETE permitidos. Inclui regras críticas de manipulação de dados: preços armazenados como estão...
official