Medusa is an open source headless commerce platform built using Node.js. It aims to provide developers with a great experience and limitless customization capabilities.
The core of Medusa is the headless server. It exposes a set of APIs that the other 2 components, admin and storefront, can use to connect to the server and access or manipulate data.
In this tutorial, you’ll learn how to create custom endpoints on your Medusa server for both the storefront and admin.
Prerequisites
This tutorial assumes you already have a Medusa server installed. If not, you can follow the Quickstart guide to get started in a few minutes.
Overview
Custom endpoints reside under the
directory in your Medusa Backend. To define a new endpoint, you can add the fileCopy to clipboardsrc/api
under theCopy to clipboardindex.js
directory. This file should export a function that returns an Express router.Copy to clipboardsrc/api
Your endpoint can be under any path you wish.
Storefront Endpoint
By Medusa’s conventions, all Storefront REST APIs are prefixed by
. For example, theCopy to clipboard/store
lets you retrieve the products to display them on your storefront.Copy to clipboard/store/products
Admin Endpoint
By Medusa’s conventions, all Admin REST APIs are prefixed by
. For example, theCopy to clipboard/admin
lets you retrieve the products to display them on your Admin.Copy to clipboard/admin/products
Implementation
To create a new endpoint, start by creating a new file in
calledCopy to clipboardsrc/api
. At its basic format,Copy to clipboardindex.js
should look something like this:Copy to clipboardindex.js
This endpoint is accessible under the path
. If you want to create an endpoint for the storefront and follow Medusa’s conventions you can prefix the path withCopy to clipboard/hello
:Copy to clipboard/store
Similarly, you can create an endpoint for the admin and follow Medusa’s conventions by prefixing the path with
:Copy to clipboard/admin
Making Endpoints Accessible from the Admin
If you’re customizing the admin dashboard or creating your own, you need to use the
library. AnCopy to clipboardcors
request should be added for each route and handle the requests with theCopy to clipboardOPTIONS
library.Copy to clipboardcors
First, you need to import your Medusa’s configurations along with the
library:Copy to clipboardcors
Then, create an object that holds the CORS configurations:
Finally, for each route you add, create an
request:Copy to clipboardOPTIONS
Multiple Endpoints
Same File
You can add more than one endpoints in
:Copy to clipboardsrc/api/index.js
Multiple Files
Alternatively, you can add multiple files for each endpoint or set of endpoints for readability and easy maintenance.
To do that with the previous example, first, create the file
with the following content:Copy to clipboardsrc/api/hello.js
You export a function that receives an Express router as a parameter and adds the endpoint
to it.Copy to clipboardadmin/hello
Next, create the file
with the following content:Copy to clipboardsrc/api/bye.js
Again, you export a function that receives an Express router as a parameter and adds the endpoint
to it.Copy to clipboardadmin/bye
Finally, in
import the two functions at the beginning of the file:Copy to clipboardsrc/api/index.js
and in the exported function, call each of the functions passing them the Express router:
Use Services
Services in Medusa bundle a set of functionalities related to a model into one class. Then, you can use that class anywhere in your backend. For example, you can use the
to retrieve products or perform operations like creating or updating a product.Copy to clipboardProductService
You can retrieve any registered service in your endpoint using
passing it the service’s registration name.Copy to clipboardreq.scope.resolve
Here’s an example of an endpoint that retrieves the count of products in your store:
The
has aCopy to clipboardproductService
method that returns a Promise. This Promise resolves to the count of the products. You return a JSON of the product count.Copy to clipboardcount
Protected Routes for
Protected routes are routes that should be accessible by logged-in users only.
To make a route protected, first, import the
middleware:Copy to clipboardauthenticate
Then, add the middleware to your route:
Now, only authenticated customers or users can access this endpoint.
Accessing Current Customer
You can get the logged-in customer’s ID using
:Copy to clipboardreq.user
To get the customer’s details, you can use the
:Copy to clipboardcustomerService
Accessing Current User
You can get the logged-in user ID using
:Copy to clipboardreq.user
To get the user’s details, you can use the
:Copy to clipboarduserService
Route Parameters
The routes you create receive 2 parameters. The first one is the absolute path to the root directory that your server is running from. The second one is an object that has your plugin's options. If your API route is not implemented in a plugin, then it will be an empty object.
What’s Next?
Endpoints are just one essential part of Medusa’s architecture. Here are more resources to learn more about Medusa’s server and how to customize it:
- What are subscribers and how to create them?
- What are services and how to create them?
- How to create a payment provider?
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.
