Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
API Endpoints  ›  Lesson

Route Handlers

API Endpoints 13 minA route file exports functions named after HTTP methods, and each one receives a Request and returns a Response, giving you an API endpoint at that folder's URL.

Route Handlers

A volunteer is building a phone app for Trailhead and asks for the trail data as JSON. You do not need a separate backend for that. Next.js lets you write API endpoints in the same app folder as your pages, using the same routing rules.

The rule

A route file exports HTTP verbs; each takes a Request and returns a Response.

Create route.ts in a folder and export async functions named after the HTTP methods you support: GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS.

// app/api/trails/route.ts  →  /api/trails
import { getTrails, createTrail } from "@/lib/trails";

export async function GET() {
  const trails = await getTrails();
  return Response.json(trails);
}

export async function POST(request: Request) {
  const body = await request.json();
  const trail = await createTrail(body);
  return Response.json(trail, { status: 201 });
}

Handlers use the standard Web Request and Response objects, the same ones fetch uses. Next.js also offers NextRequest and NextResponse, which extend them with conveniences such as easy cookie access.

Dynamic segments work exactly as for pages. The second argument carries params, which you await:

// app/api/trails/[slug]/route.ts  →  /api/trails/misty-ridge
import { getTrail } from "@/lib/trails";

export async function GET(
  request: Request,
  { params }: { params: Promise<{ slug: string }> }
) {
  const { slug } = await params;
  const trail = await getTrail(slug);
  if (!trail) return Response.json({ error: "Not found" }, { status: 404 });
  return Response.json(trail);
}

Rules to remember:

  • A method you do not export gets 405 Method Not Allowed.
  • A folder can hold a page.tsx or a route.ts, not both: one URL cannot be a page and an endpoint at once. That is why APIs usually live under app/api/.
  • Route handlers run only on the server, so they can use secrets and databases, like server components.

The shortcut

Translate the request into a path: URL → folder, method → function name. DELETE /api/trails/fox-hollow means the DELETE export in app/api/trails/[slug]/route.ts.

Serving GET /api/trails/misty-ridgeStep 1 of 4
  1. URL to folder

    /api/trails/misty-ridge → app/api/trails/[slug]/route.ts.

The trap is returning a plain object: return trail. A handler must return a Response; wrap data with Response.json(...).

Drills

Answer fast — trust the rule.

quick drill

Which file answers requests to /api/trails?

quick drill

A client sends PATCH /api/trails but the file exports only GET and POST. What is the response?

quick drill

How should a GET handler send a list of trails as JSON?

quick drill

Can app/trails/route.ts and app/trails/page.tsx both exist?

Remember

  • A route file exports HTTP verbs; each takes a Request and returns a Response.
  • Shortcut: URL → folder, method → function name.
  • Trap: return a Response, not a plain object, and never put route beside page.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainBuild the trails API

A phone app needs Trailhead's data as JSON at /api/trails (list) and /api/trails/misty-ridge (one trail). Explain the files you create, what each exports, and what a client gets if it sends a DELETE request.