Custom API Routes
Last updated
Last updated
An API Route is a REST API endpoint.
An API Route is created in a TypeScript or JavaScript file under the /src/api
directory of your Medusa application. The file’s name must be route.ts
or route.js
.
Learn more about API Routes in
For example, to create a GET
API Route at /store/hello-world
, create the file src/api/store/hello-world/route.ts
with the following content:
The file based routing supports the following HTTP methods:
GET
POST
PUT
PATCH
DELETE
OPTIONS
HEAD
You can define a handler for each of these methods by exporting a function with the name of the method in the paths route.ts
file.
For example:
To create an API route that accepts a path parameter, create a directory within the route's path whose name is of the format [param]
.
For example, if you want to define a route that takes a productId
parameter, you can do so by creating a file called /api/products/[productId]/route.ts
:
To create an API route that accepts multiple path parameters, create within the file's path multiple directories whose names are of the format [param]
.
For example, if you want to define a route that takes both a productId
and a variantId
parameter, you can do so by creating a file called /api/products/[productId]/variants/[variantId]/route.ts
.
The Medusa container is available on req.scope
. Use it to access modules' main services and other registered resources:
You can apply middleware to your routes by creating a file called /api/middlewares.ts
. This file must export a configuration object with what middleware you want to apply to which routes.
For example, if you want to apply a custom middleware function to the /store/custom
route, you can do so by adding the following to your /api/middlewares.ts
file:
The matcher
property can be either a string or a regular expression. The middlewares
property accepts an array of middleware functions.