Next.js Discord

Discord Forum

api routes

Answered
American Chinchilla posted this in #help-forum
Open in Discord
American ChinchillaOP
can someone link me docs on how to create api routes with new app router i know about the api subfolder and route.ts file but what functions do i define there what do they take how to send response etc

26 Replies

Answer
and the api folder is no longer necessary
@riský this: https://nextjs.org/docs/app/building-your-application/routing/route-handlers? ?
American ChinchillaOP
yea so i was here already but i just dont get what that Request is didnt next had some wrapper like NextRequest?
@riský and the api folder is no longer necessary
American ChinchillaOP
how is that
have you read the docs?
bc you would find that you create a folder and include route.ts to make it a route (and make sure you don't put page.tsx)
American ChinchillaOP
okay looks like i somehow skipped the first segment of it
@American Chinchilla yea so i was here already but i just dont get what that `Request` is didnt next had some wrapper like NextRequest?
do you mean NextResponse or NextRequest as they both add things on top of initial web apis?
found docs on it just now
still one thing
so i understand i export the functions like GET POST PATCH DELETE PUT etc.
but what do they take
@riský this: https://nextjs.org/docs/app/building-your-application/routing/route-handlers? ?
yeah, they both are mentioned in theses docs
@American Chinchilla but what do they take
im kinda confused what you want to know... the docs have examples on how to use each of them...
@riský im kinda confused what you want to know... the docs have examples on how to use each of them...
American ChinchillaOP
how is that i only found GET and POST but what still makes me unsure is in each example they take other args
ahh your right, some of them aren't much in depth explained
but the premise is you get a request arg and respond with nextresponse
and you can try and see how it works 🙂
American ChinchillaOP
already tried this seems to be working
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
  return NextResponse.json({ hello: "world" });
}
how do i send other status though 😵‍💫
do something like:
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
American ChinchillaOP
yea just found it on web thanks tho
@American Chinchilla so your question is answered :success: