Tutorial

Next.js ecommerce combined with Medusa

Next.js ecommerce: operations never cease to impress. In this tutorial, you’ll learn how to link your Medusa server to the Next.js starter.Next.js ecommerce

Next.js ecommerce implementations are endless. They give developers the ability to truly leverage functionalities such as server-side rendering and the generation of static websites.

Medusa is an open source headless commerce platform giving engineers the foundation for building unique and scalable digital commerce projects through our API-first engine.

As the Medusa server is headless, our starters serve as a good foundation for you to get coupled with a frontend in a matter of minutes.

In this tutorial, you’ll learn how to link your Medusa server to the Next.js starter. If you don’t have a Medusa server set up, please follow along the Quickstart guide to learn how you can do that.

Create Next.js Starter

Open the terminal and use the following command to create an instance of your storefront:

npx create-next-app -e https://github.com/medusajs/nextjs-starter-medusa my-medusa-storefront

Now you have a storefront codebase that is ready to be used with your Medusa server.

By default, the storefront is linked to the server at the URL localhost:9000. If you need to change that, create the file .env in the root of your Next.js starter and add a new variable:

NEXT_PUBLIC_MEDUSA_URL=<BACKEND_URL>

Make sure to replace <BACKEND_URL> with the URL of your Medusa server.

Update the STORE_CORS variable

By default, the storefront runs at [localhost:8000](http://localhost:8000) and the backend uses that URL to avoid CORS errors. If you need to change the URL or port, in .env file in the root of your Medusa Server add the following new variable:

STORE_CORS=<STOREFRONT_URL>

Make sure you replace <STOREFRONT_URL> with the URL to your storefront.

Start your Store

To start your store, first, you need to run the Medusa server. In the directory that holds your Medusa server run the following:

npm start

Then, in the directory that holds your Next.js storefront, run the following command:

npm run dev

Now, open the storefront at [localhost:8000](http://localhost:8000) (or the URL/port you specified) and you’ll see your store and the products!

next.js ecommerce

next.js ecommerce

Conclusion

Hooking up your headless commerce server with the Next.js ecommerce storefront is very easy using Medusa! You can now have your entire server up and running with the products, cart, and checkout functionalities.

If you want to discover the endless possibilities of Medusa’s ecosystem you can find a lot more useful resources on our docs page which can help you to build your awesome commerce project!

Should you have any issues or questions related to Medusa, then feel free to reach out to the Medusa team via Discord.

Person photo
Shahed NasserMar. 15, 20222 min.

Want to know more about Medusa

Find related blog posts below

Tutorial

How to Create Custom Endpoints in Medusa

In this tutorial, you’ll learn how to create custom endpoints on your Medusa server for both the storefront and admin.

Tutorial

How to Create a Comic Book Online Store with Medusa, Gatsby, PayPal, and MeiliSearch

In this tutorial, you’ll learn how to create a comic book store with Medusa and add important ecommerce features.

Tutorial

Medusa Open Source Marketplace Part 2: Make Orders Vendor Specific

In this part, you’ll learn how to link orders to their respective stores in a marketplace.

NEWSLETTER

Receive the most important updates around Medusa and our ecosystem