Implement a Hydrogen cart using createCartHandler and wire it to cart actions in a Remix app

domain: shopify.dev · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

In-depth guide

Shopify GraphQL Admin API rate limits — the full failure-mode walkthrough related to shopify.dev, checked against official docs, with linked verified routes.

Steps

  1. In server.ts call createCartHandler with storefront and getCartId / setCartId callbacks; getCartId reads the cart ID from a cookie (or session), setCartId writes it back on the response headers
  2. The handler returns a cart object with methods: cart.get(), cart.addLines(), cart.updateLines(), cart.removeLines(), cart.updateDiscountCodes(), and others — call these in Remix action functions
  3. Create a cart.tsx route (or an action in root.tsx) with a Remix action that reads a formData action field to dispatch the correct cart mutation (for example, 'add-line', 'remove-line')
  4. In the action call await cart.addLines([{ merchandiseId, quantity }]) and return the response using cart.setCartId inside a redirect or json response so the updated cart cookie is propagated
  5. In the root loader call cart.get() to fetch the current cart and pass it to the Outlet context; components subscribe to cart state via useCart() or useMatches()
  6. Ensure the CartProvider from @shopify/hydrogen is wrapping the app root for client-side optimistic cart updates if you use the built-in cart components

Known gotchas

Related routes

Shopify Hydrogen and Oxygen headless cart with @shopify/hydrogen-react
shopify.dev · 6 steps · unrated
Initialize a Hydrogen storefront with createStorefrontClient and make an authenticated Storefront API request inside a Remix loader
shopify.dev · 6 steps · unrated
Implement Shopify Customer Account API OAuth flow in a Hydrogen app to authenticate buyers and access account data
shopify.dev · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans