|
1
|
# Custom API Routes |
|
2
|
|
|
3
|
An API Route is a REST API endpoint. |
|
4
|
|
|
5
|
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`. |
|
6
|
|
|
7
|
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: |
|
8
|
|
|
9
|
```ts |
|
10
|
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"; |
|
11
|
|
|
12
|
export async function GET(req: MedusaRequest, res: MedusaResponse) { |
|
13
|
res.json({ |
|
14
|
message: "Hello world!", |
|
15
|
}); |
|
16
|
} |
|
17
|
``` |
|
18
|
|
|
19
|
## Supported HTTP methods |
|
20
|
|
|
21
|
The file based routing supports the following HTTP methods: |
|
22
|
|
|
23
|
- GET |
|
24
|
- POST |
|
25
|
- PUT |
|
26
|
- PATCH |
|
27
|
- DELETE |
|
28
|
- OPTIONS |
|
29
|
- HEAD |
|
30
|
|
|
31
|
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. |
|
32
|
|
|
33
|
For example: |
|
34
|
|
|
35
|
```ts |
|
36
|
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"; |
|
37
|
|
|
38
|
export async function GET(req: MedusaRequest, res: MedusaResponse) { |
|
39
|
// Handle GET requests |
|
40
|
} |
|
41
|
|
|
42
|
export async function POST(req: MedusaRequest, res: MedusaResponse) { |
|
43
|
// Handle POST requests |
|
44
|
} |
|
45
|
|
|
46
|
export async function PUT(req: MedusaRequest, res: MedusaResponse) { |
|
47
|
// Handle PUT requests |
|
48
|
} |
|
49
|
``` |
|
50
|
|
|
51
|
## Parameters |
|
52
|
|
|
53
|
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]`. |
|
54
|
|
|
55
|
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`: |
|
56
|
|
|
57
|
```ts |
|
58
|
import type { |
|
59
|
MedusaRequest, |
|
60
|
MedusaResponse, |
|
61
|
} from "@medusajs/framework/http" |
|
62
|
|
|
63
|
export async function GET(req: MedusaRequest, res: MedusaResponse) { |
|
64
|
const { productId } = req.params; |
|
65
|
|
|
66
|
res.json({ |
|
67
|
message: `You're looking for product ${productId}` |
|
68
|
}) |
|
69
|
} |
|
70
|
``` |
|
71
|
|
|
72
|
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]`. |
|
73
|
|
|
74
|
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`. |
|
75
|
|
|
76
|
## Using the container |
|
77
|
|
|
78
|
The Medusa container is available on `req.scope`. Use it to access modules' main services and other registered resources: |
|
79
|
|
|
80
|
```ts |
|
81
|
import type { |
|
82
|
MedusaRequest, |
|
83
|
MedusaResponse, |
|
84
|
} from "@medusajs/framework/http" |
|
85
|
|
|
86
|
export const GET = async ( |
|
87
|
req: MedusaRequest, |
|
88
|
res: MedusaResponse |
|
89
|
) => { |
|
90
|
const productModuleService = req.scope.resolve("product") |
|
91
|
|
|
92
|
const [, count] = await productModuleService.listAndCount() |
|
93
|
|
|
94
|
res.json({ |
|
95
|
count, |
|
96
|
}) |
|
97
|
} |
|
98
|
``` |
|
99
|
|
|
100
|
## Middleware |
|
101
|
|
|
102
|
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. |
|
103
|
|
|
104
|
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: |
|
105
|
|
|
106
|
```ts |
|
107
|
import { defineMiddlewares } from "@medusajs/framework/http" |
|
108
|
import type { |
|
109
|
MedusaRequest, |
|
110
|
MedusaResponse, |
|
111
|
MedusaNextFunction, |
|
112
|
} from "@medusajs/framework/http"; |
|
113
|
|
|
114
|
async function logger( |
|
115
|
req: MedusaRequest, |
|
116
|
res: MedusaResponse, |
|
117
|
next: MedusaNextFunction |
|
118
|
) { |
|
119
|
console.log("Request received"); |
|
120
|
next(); |
|
121
|
} |
|
122
|
|
|
123
|
export default defineMiddlewares({ |
|
124
|
routes: [ |
|
125
|
{ |
|
126
|
matcher: "/store/custom", |
|
127
|
middlewares: [logger], |
|
128
|
}, |
|
129
|
], |
|
130
|
}) |
|
131
|
``` |
|
132
|
|
|
133
|
The `matcher` property can be either a string or a regular expression. The `middlewares` property accepts an array of middleware functions. |