Next.js Discord

Discord Forum

NextJS example API responds with error 500 [Resolved]

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I receive an error 500 using the example API script on the NextJS website (https://nextjs.org/docs/pages/building-your-application/routing/api-routes)

import type { NextApiRequest, NextApiResponse } from 'next'

type ResponseData = {
    message: string
}

export default function handler(
    req: NextApiRequest,
    res: NextApiResponse<ResponseData>
) {
    if (res) {
        res.status(200).json({ message: 'Hello from Next.js!' })
    }
}

12 Replies

Check to see if are using pages router or the app router in your app.
Using this code in app router won't work
@Clown Using this code in app router won't work
Transvaal lionOP
so how do i use API endpoints with app router?
do i just make a pages folder and put my api stuff in there?
@Transvaal lion so how do i use API endpoints with app router?
Let me send you the link
Transvaal lionOP
alright
Answer
@"use php" https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Transvaal lionOP
thank you, i will have a look at this
Right Click > Apps > Mark Solution