|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} API Route Parameters`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you’ll learn about path, query, and request body parameters. |
|
8
|
|
|
9
|
## Path Parameters |
|
10
|
|
|
11
|
To create an API route that accepts a path parameter, create a directory within the route file's path whose name is of the format `[param]`. |
|
12
|
|
|
13
|
For example, to create an API Route at the path `/hello-world/:id`, where `:id` is a path parameter, create the file `src/api/hello-world/[id]/route.ts` with the following content: |
|
14
|
|
|
15
|
export const singlePathHighlights = [ |
|
16
|
["11", "req.params.id", "Access the path parameter `id`."] |
|
17
|
] |
|
18
|
|
|
19
|
```ts title="src/api/hello-world/[id]/route.ts" highlights={singlePathHighlights} |
|
20
|
import type { |
|
21
|
MedusaRequest, |
|
22
|
MedusaResponse, |
|
23
|
} from "@medusajs/framework/http" |
|
24
|
|
|
25
|
export const GET = async ( |
|
26
|
req: MedusaRequest, |
|
27
|
res: MedusaResponse |
|
28
|
) => { |
|
29
|
res.json({ |
|
30
|
message: `[GET] Hello ${req.params.id}!`, |
|
31
|
}) |
|
32
|
} |
|
33
|
``` |
|
34
|
|
|
35
|
The `MedusaRequest` object has a `params` property. `params` holds the path parameters in key-value pairs. |
|
36
|
|
|
37
|
### Multiple Path Parameters |
|
38
|
|
|
39
|
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]`. |
|
40
|
|
|
41
|
For example, to create an API route at `/hello-world/:id/name/:name`, create the file `src/api/hello-world/[id]/name/[name]/route.ts` with the following content: |
|
42
|
|
|
43
|
export const multiplePathHighlights = [ |
|
44
|
["12", "req.params.id", "Access the path parameter `id`."], |
|
45
|
["13", "req.params.name", "Access the path parameter `name`."] |
|
46
|
] |
|
47
|
|
|
48
|
```ts title="src/api/hello-world/[id]/name/[name]/route.ts" highlights={multiplePathHighlights} |
|
49
|
import type { |
|
50
|
MedusaRequest, |
|
51
|
MedusaResponse, |
|
52
|
} from "@medusajs/framework/http" |
|
53
|
|
|
54
|
export const GET = async ( |
|
55
|
req: MedusaRequest, |
|
56
|
res: MedusaResponse |
|
57
|
) => { |
|
58
|
res.json({ |
|
59
|
message: `[GET] Hello ${ |
|
60
|
req.params.id |
|
61
|
} - ${req.params.name}!`, |
|
62
|
}) |
|
63
|
} |
|
64
|
``` |
|
65
|
|
|
66
|
You access the `id` and `name` path parameters using the `req.params` property. |
|
67
|
|
|
68
|
--- |
|
69
|
|
|
70
|
## Query Parameters |
|
71
|
|
|
72
|
You can access all query parameters in the `query` property of the `MedusaRequest` object. `query` is an object of key-value pairs, where the key is a query parameter's name, and the value is its value. |
|
73
|
|
|
74
|
For example: |
|
75
|
|
|
76
|
export const queryHighlights = [ |
|
77
|
["11", "req.query.name", "Access the query parameter `name`."], |
|
78
|
] |
|
79
|
|
|
80
|
```ts title="src/api/hello-world/route.ts" highlights={queryHighlights} |
|
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
|
res.json({ |
|
91
|
message: `Hello ${req.query.name}`, |
|
92
|
}) |
|
93
|
} |
|
94
|
``` |
|
95
|
|
|
96
|
The value of `req.query.name` is the value passed in `?name=John`, for example. |
|
97
|
|
|
98
|
### Validate Query Parameters |
|
99
|
|
|
100
|
You can apply validation rules on received query parameters to ensure they match specified rules and types. |
|
101
|
|
|
102
|
Learn more in the [API Route Validation](../validation/page.mdx#how-to-validate-request-query-parameters) chapter. |
|
103
|
|
|
104
|
--- |
|
105
|
|
|
106
|
## Request Body Parameters |
|
107
|
|
|
108
|
The Medusa application parses the body of any request with JSON, URL-encoded, or text content types. The request body parameters are set in the `MedusaRequest`'s `body` property. |
|
109
|
|
|
110
|
<Note title="Tip"> |
|
111
|
|
|
112
|
Learn more about configuring body parsing in the [Body Parsing](../parse-body/page.mdx) chapter. |
|
113
|
|
|
114
|
</Note> |
|
115
|
|
|
116
|
For example: |
|
117
|
|
|
118
|
export const bodyHighlights = [ |
|
119
|
["11", "HelloWorldReq", "Specify the type of the request body parameters."], |
|
120
|
["15", "req.body.name", "Access the request body parameter `name`."], |
|
121
|
] |
|
122
|
|
|
123
|
```ts title="src/api/hello-world/route.ts" highlights={bodyHighlights} |
|
124
|
import type { |
|
125
|
MedusaRequest, |
|
126
|
MedusaResponse, |
|
127
|
} from "@medusajs/framework/http" |
|
128
|
|
|
129
|
type HelloWorldReq = { |
|
130
|
name: string |
|
131
|
} |
|
132
|
|
|
133
|
export const POST = async ( |
|
134
|
req: MedusaRequest<HelloWorldReq>, |
|
135
|
res: MedusaResponse |
|
136
|
) => { |
|
137
|
res.json({ |
|
138
|
message: `[POST] Hello ${req.body.name}!`, |
|
139
|
}) |
|
140
|
} |
|
141
|
``` |
|
142
|
|
|
143
|
In this example, you use the `name` request body parameter to create the message in the returned response. |
|
144
|
|
|
145
|
<Note title="Tip"> |
|
146
|
|
|
147
|
The `MedusaRequest` type accepts a type argument that indicates the type of the request body. This is useful for auto-completion and to avoid typing errors. |
|
148
|
|
|
149
|
</Note> |
|
150
|
|
|
151
|
To test it out, send the following request to your Medusa application: |
|
152
|
|
|
153
|
```bash |
|
154
|
curl -X POST 'http://localhost:9000/hello-world' \ |
|
155
|
-H 'Content-Type: application/json' \ |
|
156
|
--data-raw '{ |
|
157
|
"name": "John" |
|
158
|
}' |
|
159
|
``` |
|
160
|
|
|
161
|
This returns the following JSON object: |
|
162
|
|
|
163
|
```json |
|
164
|
{ |
|
165
|
"message": "[POST] Hello John!" |
|
166
|
} |
|
167
|
``` |
|
168
|
|
|
169
|
### Validate Body Parameters |
|
170
|
|
|
171
|
You can apply validation rules on received body parameters to ensure they match specified rules and types. |
|
172
|
|
|
173
|
Learn more in the [Body Validation](../validation/page.mdx#how-to-validate-request-body) chapter. |