Skip to content

Commit 1c07848

Browse files
fix(docs): adjust api route documentation examples (#56660)
Fixed the example functions in the documentation to use `async` in order to use `await` within the function body. Co-authored-by: Michael Novotny <[email protected]>
1 parent f6d6acd commit 1c07848

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

docs/03-pages/01-building-your-application/01-routing/07-api-routes.mdx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ The following example sends a JSON response with the status code `200` (`OK`) an
217217
```ts filename="pages/api/hello.ts" switcher
218218
import type { NextApiRequest, NextApiResponse } from 'next'
219219

220-
export default function handler(req: NextApiRequest, res: NextApiResponse) {
220+
export default async function handler(
221+
req: NextApiRequest,
222+
res: NextApiResponse
223+
) {
221224
try {
222225
const result = await someAsyncOperation()
223226
res.status(200).json({ result })
@@ -228,7 +231,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
228231
```
229232

230233
```js filename="pages/api/hello.js" switcher
231-
export default function handler(req, res) {
234+
export default async function handler(req, res) {
232235
try {
233236
const result = await someAsyncOperation()
234237
res.status(200).json({ result })
@@ -247,7 +250,10 @@ The following example sends a HTTP response with the status code `200` (`OK`) an
247250
```ts filename="pages/api/hello.ts" switcher
248251
import type { NextApiRequest, NextApiResponse } from 'next'
249252

250-
export default function handler(req: NextApiRequest, res: NextApiResponse) {
253+
export default async function handler(
254+
req: NextApiRequest,
255+
res: NextApiResponse
256+
) {
251257
try {
252258
const result = await someAsyncOperation()
253259
res.status(200).send({ result })
@@ -258,7 +264,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
258264
```
259265

260266
```js filename="pages/api/hello.js" switcher
261-
export default function handler(req, res) {
267+
export default async function handler(req, res) {
262268
try {
263269
const result = await someAsyncOperation()
264270
res.status(200).send({ result })
@@ -277,7 +283,10 @@ The following example redirects the client to the `/` path if the form is succes
277283
```ts filename="pages/api/hello.ts" switcher
278284
import type { NextApiRequest, NextApiResponse } from 'next'
279285

280-
export default function handler(req: NextApiRequest, res: NextApiResponse) {
286+
export default async function handler(
287+
req: NextApiRequest,
288+
res: NextApiResponse
289+
) {
281290
const { name, message } = req.body
282291

283292
try {
@@ -290,7 +299,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
290299
```
291300

292301
```js filename="pages/api/hello.js" switcher
293-
export default function handler(req, res) {
302+
export default async function handler(req, res) {
294303
const { name, message } = req.body
295304

296305
try {

0 commit comments

Comments
 (0)