Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 13 / Topic 2 of 6

GET with the plain Request/Response objects

app/api/status/[pnr]/route.ts is this part's simplest handler — the JSON counterpart to the PNR status page Part 9 already built, reusing the exact same lookupPnr function:

// harness/app/api/status/[pnr]/route.ts
import { lookupPnr } from "../../../status/lib/pnr";

export async function GET(request: Request, context: RouteContext<"/api/status/[pnr]">) {
  const { pnr } = await context.params;
  const record = await lookupPnr(pnr);

  if (!record) {
    return Response.json({ error: `No PNR record for ${pnr}` }, { status: 404 });
  }

  const verbose = new URL(request.url).searchParams.get("verbose") === "1";
  if (!verbose) {
    return Response.json({ pnr: record.pnr, status: record.status });
  }
  return Response.json(record);
}

Run for real against this harness (npm run build && npm run start -p PORT):

$ curl -s http://localhost:PORT/api/status/2458001234
{"pnr":"2458001234","status":"Confirmed"}
$ curl -s "http://localhost:PORT/api/status/2458001234?verbose=1"
{"pnr":"2458001234","train":"Karnataka Express","trainNumber":"12627","from":"Bengaluru Cantt","to":"Chennai Central","status":"Confirmed","coach":"B2","seat":"34"}
$ curl -s -o /dev/null -w "status=%{http_code}\n" http://localhost:PORT/api/status/0000000000
status=404

Nothing here — request.url, new URL(...).searchParams, Response.json(...) — is a Next.js-specific type. These are the same Request/Response objects a Cloudflare Worker, a Deno server, or a browser's own fetch already use; the installed guide calls this out directly: "Route Handlers allow you to create custom request handlers for a given route using the Web Request and Response APIs." context.params is the one Next.js-specific piece, and it's a Promise — the same async-params shape Part 2 already taught for page.tsx, just typed here with the generated RouteContext<'/api/status/[pnr]'> helper instead of PageProps.

Done here? The practice set for Part 13 — including the solutions — is on the part page.

Search the guide