route.js

Defines a custom Route Handler (API endpoint) for a route segment.

Since Next 13.2 (App Router) Spec ↗

Syntax

export async function GET(request) { return Response.json(...) }

Parameters

NameTypeRequiredDescription
request Request | NextRequest No The incoming Web Request (NextRequest adds cookies/geo helpers).
context { params: Promise<...> } No Route context with async dynamic params (Next 15).

Returns

Response | Promise<Response> — A Web Response.

Examples

// app/api/posts/route.ts
export async function GET() {
  const posts = await db.posts.all()
  return Response.json(posts)
}

export async function POST(request: Request) {
  const body = await request.json()
  const post = await db.posts.create(body)
  return Response.json(post, { status: 201 })
}
// app/api/users/[id]/route.ts
export async function GET(
  _req: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const { id } = await params
  return Response.json(await db.users.find(id))
}

Notes

Export functions named after HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.). A `route.js` cannot coexist with `page.js` in the same segment. GET handlers are cached by default unless dynamic; opt out with `export const dynamic = 'force-dynamic'`. Next 15: `params` is async.

See also