Medusa.js Integration with Frontend (Next.js/Gatsby)

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1 servicesAll 2065 services
Medusa.js Integration with Frontend (Next.js/Gatsby)
Medium
~5 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Medusa.js Frontend Integration (Next.js/Gatsby)

Medusa provides Store API and optional TypeScript SDK @medusajs/js-sdk for frontend. Official Next.js Storefront Starter — ready start point, but production projects need significant customization. Gatsby used less — suits catalogs with slow-changing content via SSG.

Setup Medusa JS SDK

npm install @medusajs/js-sdk @medusajs/types
import Medusa from '@medusajs/js-sdk';

export const medusaClient = new Medusa({
  baseUrl: process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL!,
  auth: {
    type: 'session',
  },
  publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
});

Next.js App Router: Product Page

export async function generateStaticParams() {
  const { products } = await medusaClient.store.product.list({
    fields: 'handle',
    limit: 200,
  });
  return products.map(p => ({ handle: p.handle }));
}

export async function generateMetadata({ params }): Promise<Metadata> {
  const { products } = await medusaClient.store.product.list({
    handle: params.handle,
  });
  const product = products[0];
  return {
    title: product.title,
    description: product.description,
  };
}

export default async function ProductPage({ params }) {
  const { products } = await medusaClient.store.product.list({
    handle: params.handle,
    fields: '*variants,*variants.prices,*images',
  });
  const product = products[0];
  if (!product) notFound();
  return <ProductTemplate product={product} />;
}

Cart Management

'use client';
import { createContext, useContext, useState, useEffect } from 'react';

type CartContextType = {
  cart: HttpTypes.StoreCart | null;
  addItem: (variantId: string, quantity: number) => Promise<void>;
};

const CartContext = createContext<CartContextType | null>(null);

export function CartProvider({ children }) {
  const [cart, setCart] = useState(null);

  useEffect(() => {
    const cartId = localStorage.getItem('cart_id');
    if (cartId) {
      medusaClient.store.cart.retrieve(cartId)
        .then(({ cart }) => setCart(cart));
    }
  }, []);

  const addItem = async (variantId: string, quantity: number) => {
    let currentCart = cart;
    if (!currentCart) {
      const { cart: newCart } = await medusaClient.store.cart.create({
        region_id: process.env.NEXT_PUBLIC_MEDUSA_REGION_ID,
      });
      localStorage.setItem('cart_id', newCart.id);
      currentCart = newCart;
    }

    const { cart: updatedCart } = await medusaClient.store.cart.createLineItem(
      currentCart.id,
      { variant_id: variantId, quantity }
    );
    setCart(updatedCart);
  };

  return (
    <CartContext.Provider value={{ cart, addItem }}>
      {children}
    </CartContext.Provider>
  );
}

export const useCart = () => useContext(CartContext);

Multi-Region

import { NextRequest, NextResponse } from 'next/server';

const REGION_MAP = {
  RU: process.env.MEDUSA_REGION_RU!,
  BY: process.env.MEDUSA_REGION_BY!,
  DEFAULT: process.env.MEDUSA_REGION_DEFAULT!,
};

export function middleware(request: NextRequest) {
  const country = request.geo?.country ?? 'DEFAULT';
  const regionId = REGION_MAP[country] ?? REGION_MAP.DEFAULT;

  const response = NextResponse.next();
  response.cookies.set('medusa_region', regionId, { maxAge: 60 * 60 * 24 });
  return response;
}

Timeline

  • Next.js Storefront Starter + customization: 2–3 weeks
  • Custom Next.js from scratch (App Router, SSR/SSG, cart, checkout): 6–10 weeks
  • Gatsby SSG catalog with dynamic cart (hybrid): 4–6 weeks
  • Multi-region with localization: +2–3 weeks