diff --git a/docs/api-routes/introduction.md b/docs/api-routes/introduction.md index e3f7e5021424..cf252ad0ca30 100644 --- a/docs/api-routes/introduction.md +++ b/docs/api-routes/introduction.md @@ -19,13 +19,11 @@ API routes provide a straightforward solution to build your **API** with Next.js Any file inside the folder `pages/api` is mapped to `/api/*` and will be treated as an API endpoint instead of a `page`. They are server-side only bundles and won't increase your client-side bundle size. -For example, the following API route `pages/api/user.js` handles a `json` response: +For example, the following API route `pages/api/user.js` returns a `json` response with a status code of `200`: ```js export default function handler(req, res) { - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') - res.end(JSON.stringify({ name: 'John Doe' })) + res.status(200).json({ name: 'John Doe' })) } ``` diff --git a/examples/progressive-web-app/pages/api/hello.js b/examples/progressive-web-app/pages/api/hello.js index 57f492cdc141..1424b47343d6 100644 --- a/examples/progressive-web-app/pages/api/hello.js +++ b/examples/progressive-web-app/pages/api/hello.js @@ -1,8 +1,7 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction const hello = (req, res) => { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } export default hello diff --git a/examples/with-gsap/pages/api/hello.js b/examples/with-gsap/pages/api/hello.js index 21637a0bcbe4..df63de88fa67 100644 --- a/examples/with-gsap/pages/api/hello.js +++ b/examples/with-gsap/pages/api/hello.js @@ -1,6 +1,5 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction export default function handler(req, res) { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-mux-video/pages/api/asset/[id].js b/examples/with-mux-video/pages/api/asset/[id].js index 1886c48c1ed7..a9bf34692ac5 100644 --- a/examples/with-mux-video/pages/api/asset/[id].js +++ b/examples/with-mux-video/pages/api/asset/[id].js @@ -17,9 +17,8 @@ export default async function assetHandler(req, res) { }, }) } catch (e) { - res.statusCode = 500 console.error('Request error', e) - res.json({ error: 'Error getting upload/asset' }) + res.status(500).json({ error: 'Error getting upload/asset' }) } break default: diff --git a/examples/with-mux-video/pages/api/upload.js b/examples/with-mux-video/pages/api/upload.js index 6096286def53..1ee2f2b4e929 100644 --- a/examples/with-mux-video/pages/api/upload.js +++ b/examples/with-mux-video/pages/api/upload.js @@ -16,9 +16,8 @@ export default async function uploadHandler(req, res) { url: upload.url, }) } catch (e) { - res.statusCode = 500 console.error('Request error', e) - res.json({ error: 'Error creating upload' }) + res.status(500).json({ error: 'Error creating upload' }) } break default: diff --git a/examples/with-mux-video/pages/api/upload/[id].js b/examples/with-mux-video/pages/api/upload/[id].js index a9e48b81dec8..86f0a3f4320a 100644 --- a/examples/with-mux-video/pages/api/upload/[id].js +++ b/examples/with-mux-video/pages/api/upload/[id].js @@ -16,9 +16,8 @@ export default async function uploadHandler(req, res) { }, }) } catch (e) { - res.statusCode = 500 console.error('Request error', e) - res.json({ error: 'Error getting upload/asset' }) + res.status(500).json({ error: 'Error getting upload/asset' }) } break default: diff --git a/examples/with-sentry/pages/api/test1.js b/examples/with-sentry/pages/api/test1.js index 560ad9ce9a80..546d3215d071 100644 --- a/examples/with-sentry/pages/api/test1.js +++ b/examples/with-sentry/pages/api/test1.js @@ -6,6 +6,5 @@ const doAsyncWork = () => Promise.reject(new Error('API Test 1')) doAsyncWork() export default async function handler(req, res) { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-sentry/pages/api/test2.js b/examples/with-sentry/pages/api/test2.js index bf96ded450da..3df4ab637289 100644 --- a/examples/with-sentry/pages/api/test2.js +++ b/examples/with-sentry/pages/api/test2.js @@ -9,6 +9,5 @@ function work() { work() export default async function handler(req, res) { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-sentry/pages/api/test3.js b/examples/with-sentry/pages/api/test3.js index 6e55df70fedc..152c058a33df 100644 --- a/examples/with-sentry/pages/api/test3.js +++ b/examples/with-sentry/pages/api/test3.js @@ -9,6 +9,5 @@ function work() { export default async function handler(req, res) { work() - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-sentry/pages/api/test4.js b/examples/with-sentry/pages/api/test4.js index 052b574a4e70..699569c781b3 100644 --- a/examples/with-sentry/pages/api/test4.js +++ b/examples/with-sentry/pages/api/test4.js @@ -14,6 +14,5 @@ export default async function handler(req, res) { // Flushing before returning is necessary if deploying to Vercel, see // https://vercel.com/docs/platform/limits#streaming-responses await Sentry.flush(2000) - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-tailwindcss/pages/api/hello.js b/examples/with-tailwindcss/pages/api/hello.js index 5b0218cc09ff..f163396631ba 100644 --- a/examples/with-tailwindcss/pages/api/hello.js +++ b/examples/with-tailwindcss/pages/api/hello.js @@ -1,6 +1,5 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction export default function helloAPI(req, res) { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } diff --git a/examples/with-typescript-eslint-jest/pages/api/hello.ts b/examples/with-typescript-eslint-jest/pages/api/hello.ts index 9c59e5b8be5a..b3be12cef86c 100644 --- a/examples/with-typescript-eslint-jest/pages/api/hello.ts +++ b/examples/with-typescript-eslint-jest/pages/api/hello.ts @@ -3,8 +3,7 @@ import { NextApiRequest, NextApiResponse } from 'next' const handler = (req: NextApiRequest, res: NextApiResponse) => { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) } export default handler diff --git a/examples/with-unsplash/pages/api/collection/[id].tsx b/examples/with-unsplash/pages/api/collection/[id].tsx index 9a6084112a5e..5283e085ae88 100644 --- a/examples/with-unsplash/pages/api/collection/[id].tsx +++ b/examples/with-unsplash/pages/api/collection/[id].tsx @@ -16,15 +16,12 @@ export default function getCollection( .getCollection(parseInt(id.toString())) .then(toJson) .then((json) => { - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') res.setHeader('Cache-Control', 'max-age=180000') - res.end(JSON.stringify([json])) + res.status(200).json([json]) resolve() }) .catch((error) => { - res.json(error) - res.status(405).end() + res.status(405).json(error) resolve() }) }) diff --git a/examples/with-unsplash/pages/api/collection/index.tsx b/examples/with-unsplash/pages/api/collection/index.tsx index 6edcd21eeaf9..784ce1f03f42 100644 --- a/examples/with-unsplash/pages/api/collection/index.tsx +++ b/examples/with-unsplash/pages/api/collection/index.tsx @@ -15,15 +15,12 @@ export default function getCollections( .then((json) => { json.map((c) => (c.slug = slug(c.title))) - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') res.setHeader('Cache-Control', 'max-age=180000') - res.end(JSON.stringify(json)) + res.status(200).json(json) resolve() }) .catch((error) => { - res.json(error) - res.status(405).end() + res.status(405).json(error) resolve() }) }) diff --git a/examples/with-unsplash/pages/api/photo/[id].tsx b/examples/with-unsplash/pages/api/photo/[id].tsx index 5a9c499b0d36..e7125d4aa5ae 100644 --- a/examples/with-unsplash/pages/api/photo/[id].tsx +++ b/examples/with-unsplash/pages/api/photo/[id].tsx @@ -16,15 +16,12 @@ export default function getCollectionPhotos( .getCollectionPhotos(parseInt(id.toString())) .then(toJson) .then((json) => { - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') res.setHeader('Cache-Control', 'max-age=180000') - res.end(JSON.stringify(json)) + res.status(200).json(json) resolve() }) .catch((error) => { - res.json(error) - res.status(405).end() + res.status(405).json(error) resolve() }) }) diff --git a/examples/with-unsplash/pages/api/photo/index.tsx b/examples/with-unsplash/pages/api/photo/index.tsx index 2b91a212a266..74a6c8c75b1f 100644 --- a/examples/with-unsplash/pages/api/photo/index.tsx +++ b/examples/with-unsplash/pages/api/photo/index.tsx @@ -9,15 +9,12 @@ export default function getPhotos(req: NextApiRequest, res: NextApiResponse) { .photos(process.env.UNSPLASH_USER, 1, 50, 'latest') .then(toJson) .then((json: string) => { - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') res.setHeader('Cache-Control', 'max-age=180000') - res.end(JSON.stringify(json)) + res.status(200).json(json) resolve() }) .catch((error) => { - res.json(error) - res.status(405).end() + res.status(405).json(error) resolve() }) }) diff --git a/examples/with-unsplash/pages/api/stats/index.tsx b/examples/with-unsplash/pages/api/stats/index.tsx index 8ded04165516..defe8845d50d 100644 --- a/examples/with-unsplash/pages/api/stats/index.tsx +++ b/examples/with-unsplash/pages/api/stats/index.tsx @@ -9,15 +9,12 @@ export default function getStats(req: NextApiRequest, res: NextApiResponse) { .statistics(process.env.UNSPLASH_USER, 'days', 30) .then(toJson) .then((json) => { - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') res.setHeader('Cache-Control', 'max-age=180000') - res.end(JSON.stringify(json)) + res.status(200).json(json) resolve() }) .catch((error) => { - res.json(error) - res.status(405).end() + res.status(405).json(error) resolve() }) }) diff --git a/examples/with-unsplash/pages/api/user/index.tsx b/examples/with-unsplash/pages/api/user/index.tsx index 5b8d58715b4e..2dff59a6774c 100644 --- a/examples/with-unsplash/pages/api/user/index.tsx +++ b/examples/with-unsplash/pages/api/user/index.tsx @@ -9,15 +9,12 @@ export default function getUser(req: NextApiRequest, res: NextApiResponse) { .profile(process.env.UNSPLASH_USER) .then(toJson) .then((json) => { - res.statusCode = 200 - res.setHeader('Content-Type', 'application/json') res.setHeader('Cache-Control', 'max-age=180000') - res.end(JSON.stringify(json)) + res.status(200).json([json]) resolve() }) .catch((error) => { - res.json(error) - res.status(405).end() + res.status(405).json(error) resolve() }) }) diff --git a/packages/create-next-app/templates/default/pages/api/hello.js b/packages/create-next-app/templates/default/pages/api/hello.js index 5b77ec0d11dc..9987aff4c39a 100644 --- a/packages/create-next-app/templates/default/pages/api/hello.js +++ b/packages/create-next-app/templates/default/pages/api/hello.js @@ -1,6 +1,5 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction export default (req, res) => { - res.statusCode = 200 - res.json({ name: 'John Doe' }) + res.status(200).json({ name: 'John Doe' }) }