Medusa is an open source headless ecommerce platform targeted toward developers. It can be used to build fully-fledged ecommerce stores.
A fully-fledged ecommerce store is an ecommerce website that allows merchants to sell online. It also allows customers to browse through product pages and purchase products.
Medusa is an ecommerce platform that has a lot of essential ecommerce features including automated RMA flows, plug-and-play integrations, product and order management, and much more.
In this tutorial, you’ll learn how to create a comic book store with Medusa as the ecommerce platform.
You’ll also be adding important ecommerce features to your store including a search engine using MeiliSearch and a payment gateway using PayPal.
You can find the full code for this tutorial on this GitHub repository.
Architecture Overview
Before you create an online store with Medusa, here’s a short overview of Medusa’s ecommerce platform architecture in case you’re not familiar with it.
You can go ahead and skip to the next section if you are.
Medusa ecommerce platform is made up of 3 primary components:
Headless Server
The Headless Server is the core ecommerce platform of your online store.
It takes care of handling all logic, ecommerce features, and data.
All other components connect to the server using REST APIs.
Medusa Admin
The Medusa Admin is the user interface that store operators can use to view and manage their data (for example, products and orders).
Medusa provides an intuitive ready-made Admin panel that you can use.
Alternatively, you can build your own admin and connect to the server using the REST APIs.
Storefront
The Storefront is the ecommerce website where customers view products and make purchases.
You can also build a storefront with any framework of your choice and connect to the server using REST APIs.
In this tutorial, you’ll learn about setting up each and how to use them to create a comic book store.
Prerequisites
Before you start building your own online store you’ll need the following requirements installed:
- Node v14 or higher.
- Postgres with an empty database created.
- MeiliSearch for the search engine.
- A PayPal developer account.
- MinIO for file storage. You can alternatively use S3 or DigitalOcean Spaces.
Install Server of Ecommerce Store
To install the Medusa server, you need to first install the Medusa CLI:
Then, run the following command to install the Medusa server in a new directory
:Copy to clipboardcomic-store
Install Plugins
The next step is to install the plugins you’ll be using on your Medusa server. For this tutorial, you need the plugins for the PayPal, MeiliSearch, and MinIO integrations.
Run the following command inside the
directory to install the 3 plugins:Copy to clipboardcomic-store
Replacewith the file service you’re using if it’s not MinIO.Copy to clipboardmedusa-file-minio
Make sure in
that the versions forCopy to clipboardpackage.json
,Copy to clipboard@medusajs/medusa
andCopy to clipboardmedusa-interfaces
are greater than or equal toCopy to clipboard@medusajs/medusa-cli
. If not, update them with the following command:Copy to clipboard1.3.0
Add Environment Variables
Medusa gives you the freedom to handle your environment variables based on your server.
In this tutorial, you’ll be adding all environment variables in a
variable.Copy to clipboard.env
Open the
file. Add the following variables:Copy to clipboard.env
These environment variables are important for configurations related to the database, MinIO, PayPal, and MeiliSearch.
is the URL to connect to your PostgreSQL database schema. It should be of the formatCopy to clipboardDATABASE_URL
.Copy to clipboardpostgres://<USERNAME>:<PASSWORD>@<HOST>/<DB_NAME>
You can refer to our documentation to learn how to retrieve the necessary variables for MinIO and MeiliSearch.
If you’re using a different file service than MinIO please refer to the documentation of your file service to learn what variables you need.
For PayPal, you can refer to PayPal’s documentation to retrieve the Client ID, Client Secret, and Webhook ID. You also turned sandbox mode on for testing by setting
toCopy to clipboardPAYPAL_SANDBOX
.Copy to clipboardtrue
Configure Server
You need to pass these environment variables to the server configurations.
Server configurations are all in
. This includes database, plugins, and more configurations.Copy to clipboardmedusa-config.js
Open
. Add the following to the beginning of the file:Copy to clipboardmedusa-config.js
This allows you to load environment variables from a
file.Copy to clipboard.env
Next, in the
array, add the following 3 plugins at the end of the array:Copy to clipboardplugins
This loads the 3 plugins you installed earlier and passes the necessary options for each.
Finally, change the database configurations in
in the exported function to use your PostgreSQL database instead of an SQLite database:Copy to clipboardprojectConfig
Migrate and Seed Database
The final step before running your server is migrating and seeding your database.
Migration means adding the necessary tables into your database schema to make it work with Medusa.
Seeding means adding dummy data into your database to quickly get started.
Run the following command to migrate and seed your database:
This will connect to your database using the URL you passed to the environment variable
.Copy to clipboardDATABASE_URL
Make sure that you already created the database before running this command.
Run the Server
Make sure that the MeiliSearch and MinIO services are running. Then, run your server with the following command:
This will run your server on the port
.Copy to clipboard9000
You should keep the server running for the entire tutorial, as the Medusa admin and storefront depend on the server.
Setup Medusa Admin for the Ecommerce Website
In this section, you’ll install the Medusa Admin, add products to it, and enable PayPal as a payment gateway.
Install Admin
In your terminal and in a different directory than the
directory, run the following command:Copy to clipboardcomic-store
Then, change to the newly created
directory and install the necessary dependencies:Copy to clipboardcomic-admin
Make sure the Medusa server is still running.
Then, run the following command to start the admin:
This will start your Medusa admin on the port
by default.Copy to clipboard7000
Open it in your browser and you should see a login screen.
You can use the default email “admin@medusa-test.com” and password “supersecret” to log in.
Add Products
After you log in, choose from the sidebar “Products”.
You’ll see a few products that were added when you seeded your database.
Go ahead and delete those by clicking on the 3 dots for each one then Delete.
Next, add products to your comic book store by clicking the “New Product” button at the top right.
You need to fill out the fields related to the product info.
Add as many products as you want before moving on to the next step.
Enable PayPal
To enable PayPal as a payment gateway, click on Settings, then choose Regions.
For each region you want to add PayPal as a payment provider, click on the “Payment Providers” input and choose “paypal”, then click Save.
Setup the Storefront for Your Online Store
The last step is to set up the storefront.
This section covers installing the Gatsby storefront, making some customizations to it, adding MeiliSearch bar, and adding the UI for PayPal.
Install Storefront
In your terminal and in a different directory than the
andCopy to clipboardcomic-store
directories, run the following command:Copy to clipboardcomic-admin
This will install the Gatsby storefront in a new directory
.Copy to clipboardcomic-storefront
Then, change to the
directory and renameCopy to clipboardcomic-storefront
toCopy to clipboard.env.template
:Copy to clipboard.env.development
Add Environment Variables
You need to add environment variables to use MeiliSearch and PayPal on your storefront.
In
add the following variables:Copy to clipboard.env.development
The values for these configurations are the same as those you used on your server.
Run Gatsby Storefront
Make sure that the Medusa server is running.
Then run the following command to start the Gatsby storefront:
This will run your storefront on
. Open it in your browser.Copy to clipboardlocalhost:8000
You should see a hero image and the products you added.
Customize Storefront
The hero banner is a static one that is added to the code.
You’ll now customize it to show something related to your comic book store.
Open
. You should find in the returned JSX the componentCopy to clipboardsrc/pages/index.js
followed by a div.Copy to clipboardStaticImage
Change them to the following:
This changes the text and image used.
You can download the new image from here. Place it at
with the nameCopy to clipboardsrc/images
.Copy to clipboardhero.png
If you open your storefront now you should see the hero image updated.
Add Search Bar
In this section, you’ll add a search bar to search products using MeiliSearch.
In your terminal, run the following command to install some necessary dependencies:
Then, create the file
with the following content:Copy to clipboardsrc/components/header/search.jsx
This creates a search client using the method
that is exported from the dependencyCopy to clipboardinstantMeiliSearch
which you just installed.Copy to clipboard@meilisearch/instant-meilisearch
You pass the method the environment variables you added earlier for the configurations.
The
component then displays a search bar using components fromCopy to clipboardSearch
.Copy to clipboardreact-instantsearch-dom
When the user enters a query and there are results, each result is rendered using the
component.Copy to clipboardHit
If you want to learn more about how you can customize the UI of the search bar and its options you can check out the documentation of React InstantSearch by Algolia.
Next, you’ll add the search bar to the navigation bar. To do that, open
and import theCopy to clipboardindex.jsx
component at the beginning of the file:Copy to clipboardSearch
Then, in the returned JSX add the
component beforeCopy to clipboardSearch
:Copy to clipboardRegionPopover
Save all changes and open the storefront now.
You should see a search bar in the navigation bar.
Try to enter the name of one of your products and you should see it in the result.
Add PayPal UI
In this section, you’ll add the UI necessary to use PayPal as a payment method.
In your terminal use the following command to install PayPal’s React library:
Then, create the file
with the following content:Copy to clipboardsrc/components/payment/paypal-payment/index.jsx
To briefly explain this code snippet:
- You render a PayPal button that allows customers to pay with PayPal using components from
which you just installed. You pass the componentCopy to clipboard@paypal/react-paypal-js
the PayPal client ID from the environment variables.Copy to clipboardPayPalScriptProvider
- When the button is clicked, the method
is executed which initiates authorization with PayPal using the methodCopy to clipboardhandlePayment
. This opens PayPal’s payment portal in a new window.Copy to clipboardactions.order.authorize()
- After the customer successfully completes payment, the fulfillment callback function passed to
is executed. If there are any errors in the authorization, an error message will be shown on your online store. Otherwise, theCopy to clipboardthen
method will be called.Copy to clipboardcompleteOrder
- In the
method, PayPal is first set as the payment session of the current cart on your online store. Then, it is updated on the server with data that is received from PayPal after the customer authorized the payment.Copy to clipboardcompleteOrder
- Finally, the order is placed and the customer is redirected to the
page on your online store where they can see a summary of their order details.Copy to clipboardorder-confirmed
Next, in
add an import for theCopy to clipboardsrc/components/payment/index.jsx
component at the beginning of the file:Copy to clipboardPaypalPayment
Then, in the returned JSX you’ll find a switch statement that renders components on your online store based on the ID of the payment provider. Add a new case to the switch statement before the
case.Copy to clipboarddefault
This renders
when the ID of the payment provider available for the customer on your online store isCopy to clipboardPaypalPayment
:Copy to clipboardpaypal
Save all changes before moving on to test the entire flow on your online store.
Test Checkout Flow: Start Selling Online
In this section, you’ll test placing an order on your online store on the storefront, then viewing the details on the admin and capturing payment.
Make sure that all 3 components (Medusa server, Medusa admin, and storefront) of your online store are running.
Then, on the storefront of your online store, choose a product and it to the cart.
Then, click on the cart icon and click on the “Checkout” button in the popup.
You’ll be taken to a one-page checkout on your online store where you have to enter your details and choose a shipping method.
Once you reach the last step of the checkout on your online store you should see the available payment methods in the current region including PayPal.
If you can’t see PayPal, make sure the correct region is selected at the top right of the navigation bar of your online store.
Try paying with PayPal by clicking on the first PayPal button on your online store.
A new page will open where you’re asked to log in to PayPal and authorize the payment.
Once you authorize the payment with a sandbox account, you’ll be taken back to the storefront of your online store.
If the authorization was successful, you’ll shortly be redirected to the Order Summary page on your online store.
On the Medusa admin of your online store, click on Orders in the sidebar.
You should see a new order on your online store.
Click on the order. You’ll see the order details including the items ordered and payment details.
To capture the payment on your online store, click on the “Capture payment” button.
What’s Next?
You just created a comic book online store using Medusa that has a search engine using MeiliSearch and PayPal as a payment provider.
Medusa is an open source ecommerce platform that has a lot of ecommerce features but also allows you to integrate other services into your ecommerce platform.
There’s much more that you can do with your online business:
- Check more plugins that you can add to add more features to your ecommerce store, including using Strapi for CMS.
- Customize your Medusa ecommerce platform by adding custom endpoints.
- Deploy the server on Heroku and the Medusa Admin and Storefront of your online store on Netlify.
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.
