Next.js Discord

Discord Forum

- error TypeError: res.status is not a function

Answered
Sun bear posted this in #help-forum
Open in Discord
Avatar
Sun bearOP
Hi everyone, im having trouble with my api since I moved to typescript and im not too sure why. So my api is in /app/api/submitId/route.ts.
Its forced me to go from handler to GET which is fine, but now im getting a - error TypeError: res.status is not a function and I have no idea why. Any help would be appreciated because I feel that its simple but I cant seem to figure out whats wrong :/
import { NextApiRequest, NextApiResponse } from 'next';
interface Data {
  message?: string;
}

export function GET(req: NextApiRequest, res: NextApiResponse<Data>) {
  res.status(200).json({ message: 'John Doe' })
}
Answered by Hunter Bertoson
Had this same issue!! The APIs for route handlers is different than the API that used to be used for api routes.

NextApiRequest and NextApiResponse are no longer used and only Request is a parameter to your handler. You now return a Response object to do things like set the status code.

See examples here:

https://nextjs.org/docs/app/building-your-application/routing/route-handlers
View full answer

3 Replies

Avatar
Had this same issue!! The APIs for route handlers is different than the API that used to be used for api routes.

NextApiRequest and NextApiResponse are no longer used and only Request is a parameter to your handler. You now return a Response object to do things like set the status code.

See examples here:

https://nextjs.org/docs/app/building-your-application/routing/route-handlers
Answer
Avatar
Here is a similar function I have.
export async function GET() {
    try {
        const tollFreeNumbers = await twilioClient
            .availablePhoneNumbers('US')
            .tollFree.list({limit: 5});
        const res = tollFreeNumbers.map((t) => t.friendlyName);
        return NextResponse.json({numbers: res});
    } catch (error) {
        return NextResponse.json({error}, {status: 500});
    }
}