Route Handlers

Route Handlers provide HTTP endpoints inside the App Router. A route.ts file can define handlers for methods such as GET, POST, PUT, PATCH, and DELETE.

Route Handlers are appropriate when an API must be consumed by external clients, browser code, webhooks, mobile applications, or third-party integrations.

If only a Server Component needs server-side data, directly calling the data layer is often simpler than creating an internal HTTP request.

GET Route Handler
import { NextResponse } from 'next/server';

export async function GET() {
  const users = await db.user.findMany();

  return NextResponse.json({ users });
}
💡 The GET handler retrieves data and returns a JSON response.
Key Takeaways
  • Route Handlers implement HTTP endpoints.
  • Use them for genuine API boundaries.
  • Avoid unnecessary internal HTTP calls from Server Components.