This guide will explain how to set up Medusa in a React application. Medusa is an open source headless commerce engine built for developers. Medusa's composable architecture allows for endless customization and provides a friendly developer experience. React is a popular JavaScript library that provides a declarative and component-based architecture for building user interfaces.
Throughout this tutorial, you will learn how to build a React ecommerce store with Medusa. You will learn how to set up the Medusa server and admin panel and build out some pages to make up the storefront.
The code for the project developed in this article can be found in this GitHub repository.
Prerequisites
To complete this tutorial, you will need:
- Node.js version 14.18+ or 16+ installed on your local machine.
- One of the following package managers:
,Copy to clipboardnpm
, orCopy to clipboardyarn
(This tutorial will specifically useCopy to clipboardpnpm
.)Copy to clipboardnpm
Set up the Medusa Server
In order to set up the server, you first need to install Medusa's CLI with the following command:
Next, run the following command to install the Medusa server:
The command above installs the necessary packages and adds the Medusa server to the
directory. TheCopy to clipboardmedusa-server
command populates an SQLite database with sample data that can be referenced from the storefront and admin panel.Copy to clipboard--seed
Test the Medusa Server
Navigate to the
folder and start the server:Copy to clipboardmedusa-server
The command above launches the server on
.Copy to clipboardlocalhost:9000
You now have a complete commerce engine running locally. You can verify that the server is running by going to
in your browser. You should see a JSON blob that has the following response:Copy to clipboardhttp://localhost:9000/store/products
JSON response of sample products data
If you want the ability to upload product images to your Medusa server, you will need to install and configure a file service plugin like S3 or DigitalOcean Spaces.
Setup the Medusa Admin
Medusa provides an admin dashboard with numerous functionalities that equip you to manage your store, including order and product management.
To set up the Medusa Admin, clone the Admin GitHub repository:
Change into the cloned directory and then install the dependencies:
Test the Medusa Admin
Before testing the Medusa admin, ensure that the Medusa server is up and running.
Once the installation of dependencies is complete and the Medusa server is running, execute the following command to start the Medusa admin:
You can now view the Medusa Admin in the browser at
where you will be prompted with the following login screen:Copy to clipboardhttp://localhost:7000/
Medusa Admin Login Screen
To log in, you’ll need to input the credentials of the demo user that was created when the server’s database was seeded. For the email field, enter
, and for the password field, enterCopy to clipboardadmin@medusa-test.com
.Copy to clipboardsupersecret
Once logged in, click on “Products” in the sidebar menu. Here, you will see a list of demo products and you can add a few products by clicking on the “New Product” button in the upper right-hand corner.
To learn more about the features and functionalities of the Medusa Admin, check out the user guide.
Setup the React App
In this section, you will scaffold a React application with Vite. Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects and is an alternative to tools like Create React App.
Run one of the following commands to create a Vite app. The command will vary slightly depending on the npm version that you have installed:
From here, change into the new directory and install the project dependencies:
Start the dev server locally:
Navigate to
in your browser and you should now see a screen similar to the one below.Copy to clipboardhttp://localhost:5173/
Vite default landing page
Install React Bootstrap
To assist with styling and building the components for the storefront you will use React Bootstrap. To install, run the following command:
In order to apply the styles needed for the React Bootstrap library, you need to add the following import to the top of
:Copy to clipboardsrc/main.jsx
Update the CSS
Importing the React Bootstrap stylesheet in
applies a myriad of base styles therefore the stylesheet can be trimmed down.Copy to clipboardsrc/main.jsx
Open
and replace its content with the following CSS:Copy to clipboardsrc/index.css
Install React Router
In order to enable client-side routing in our app, we will need to install and configure React Router by running the following command:
Since
is our entry point, we need to importCopy to clipboardsrc/main.jsx
and wrap theCopy to clipboard[BrowserRouter](https://reactrouter.com/en/main/router-components/browser-router)
component within it. Doing so will enable client-side routing for our entire web app.Copy to clipboardApp
Open
and replace its contents with the following code:Copy to clipboardsrc/main.jsx
With React Router installed and configured, we can begin to build out the other pages for the storefront.
Connect the React Storefront to the Medusa Server
The Medusa JS Client is an SDK that provides an easy way to access the Medusa API from a web application. This Medusa JS client is an alternative to interacting with the REST APIs.
Run the following command to install the Medusa JS Client:
Medusa uses CORS to only allow specific origins to access the server. By default, the Medusa server is configured to allow access to the storefront on port
. Because the default port for Vite isCopy to clipboard8000
, you need to configure Vite to point to the desired port number.Copy to clipboard5713
To address this, add the
option to the `` file:Copy to clipboard[server.port](https://vitejs.dev/config/server-options.html#server-port)
Restart the Vite dev server to reflect the changes made in the config file, and you can now open a new browser tab at
.Copy to clipboardhttp://localhost:8000/
Create Medusa JS Client Utility
Create a new file at the root of the project called
and add the following environment variable that points to the URL of the Medusa server:Copy to clipboard.env
Vite requires all env variables that need to be exposed to the client source code to be prefixed with
and exposes the environment variable to the client viaCopy to clipboardVITE_
.Copy to clipboardimport.meta.env.VITE_MEDUSA_SERVER_URL
Create the file
and add the following code to create a utility that will make accessing the instance of the Medusa JS Client reusable across the application:Copy to clipboardsrc/utils/client.js
Create the Storefront Components
This section will cover building out the components that will compose the home page of product listings. Here is a list of the components that we will be building in this section:
- NavHeader - Displays the logo, links, and shopping cart info.
- ProductCard - Displays metadata about the products.
To start, create a new
folder in theCopy to clipboardcomponents
directory to house all the components we will build for the storefront.Copy to clipboard/src
NavHeader Component
Create the file
and add the following code:Copy to clipboardsrc/components/NavHeader.jsx
The
component contains a link back to the home page via the “Products” link and a total count of the number of items that have been added to the cart.Copy to clipboardNavHeader
ProductCard Component
In the
directory, create a new file calledCopy to clipboardcomponents
and add the following code:Copy to clipboardProductCard.jsx
This component uses React Bootstrap’s Card component to display metadata about each product.
Create the Home Page
The home page is the root route that uses the Medusa JS Client’s
method to pull in a list of products and display information about them using theCopy to clipboardlist
component that was created earlier.Copy to clipboardProductCard
Create a new file in
and add the following code:Copy to clipboardsrc/routes/Home.jsx
Register the Home Page Route
Next, leverage React Routers
andCopy to clipboard[Routes](https://reactrouter.com/en/main/components/routes)
components in order to register the pages needed for the react webshop.Copy to clipboard[Route](https://reactrouter.com/en/main/route/route)
Open up
and update it to include the following code:Copy to clipboardsrc/App.jsx
With this setup, the
component will be visible across all our routes and the list of all products can be viewed at the root route.Copy to clipboardNavHeader
Test the Home Page
Ensure that the Medusa server and the storefront are running and navigate to
to see a preview of the home page:Copy to clipboardhttp://localhost:8000/
List of all products on the home page
Create the Product Page
In the
folder, create a new file calledCopy to clipboardroutes
and add the following code:Copy to clipboardProduct.jsx
This code takes the ID in the URL’s query param and uses the Medusa JS Client’s
method to obtain information about an individual product.Copy to clipboardretrieve
Register the Product Page Route
Re-open
, import theCopy to clipboardsrc/App.jsx
page component, and add a new React RouterCopy to clipboardProduct
that will point to the individual product page:Copy to clipboardRoute
Test the Product Page
To test this out, click on the “View Details” button for the “Medusa Sweatpants” and you will be directed to the product's details page:
Product page for the “Medusa Sweatpants”
Create Add to Cart Functionality
To wire up the logic for the add-to-cart functionality, you need to check if a cart was previously created or not.
For this example, if the
is present, then we call the add the line item to the cart and store the cart’s item count in local storage under theCopy to clipboardcartID
field. If theCopy to clipboardcartCount
is not present, then we create a new cart and store the newly generated ID in local storage. TheCopy to clipboardcartID
is used to update the display count in the navigation header.Copy to clipboardcartCount
Re-open
in theCopy to clipboardProduct.jsx
folder and update the file to include the following code:Copy to clipboardroutes
The code above adds the following functionality:
async function that uses theCopy to clipboardaddProduct
utility to add a product to a cart.Copy to clipboardmedusaClient
function to grab a list of all the regions and set it to the US region found at the first index in theCopy to clipboardgetRegions
array. This ID then gets stored in theCopy to clipboardregions
state variable.Copy to clipboardregionId
function that is invoked when the “Add to Cart” button is clicked.Copy to clipboardhandleAddToCart
For simplicities sake, the code adds the first variant of a product. This means that a product can only be added to a cart once. In addition, a page reload is initiated in order to pick up the new cart count. This decision was intentional to keep the project simple but know that a real-world app could allow the ability to select different variants and add more advanced capabilities.
Test Add to Cart Functionality
To test out the functionality, click on the “View Details” link for any item to navigate to the product details page. From there, click on the “Add to Cart” button which will initiate a page refresh and update the cart’s badge count in the navigation header.
Conclusion
Throughout this tutorial, you learned about the Medusa Admin, Medusa Server, and how you can use the Medusa JS Client to create a sample ecommerce store with React. A lot of material was covered, however, there is still so much more that you can generate with Medusa such as:
- Expanding upon the “add to cart” functionality and building a shopping cart screen that retrieves a cart and displays a list of all the products that have been added to it.
- Payment integrations with Stripe.
- Implementing a Checkout Flow
Should you have any issues or questions related to Medusa, then feel free to reach out to the Medusa team via Discord.
Share this post
Try Medusa
Spin up your environment in a few minutes.

You may also like
On this page