Route Handler methods
What you need to know
A Route Handler maps an HTTP method at a URL to server code that returns a Response. It is the App Router mechanism for endpoints used by clients that need HTTP rather than rendered HTML.
By the end, you can: map method exports to endpoint behavior; choose a status code.
Read a synthetic collection
// app/api/books/route.tsexport function GET() { return Response.json({ items: [{ id: 'moss', title: 'Moss Atlas' }] });}Expected behavior (not framework-tested): GET /api/books returns a JSON object and status 200. No database or browser component is involved.
How it works
Create app/api/books/route.ts to own /api/books. Named functions GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS are the supported method conventions. Unimplemented unsupported methods produce a 405; When OPTIONS is omitted, Next.js supplies an OPTIONS response with an Allow header derived from the route methods. This does not automatically implement your cross-origin access policy; inspect actual headers and test the intended CORS behavior separately. A page.tsx and route.ts cannot both own the same segment. A method function may be async and receives the standard Web Request (or NextRequest for framework helpers); dynamic handler context contains asynchronous params in the selected version family.
GET is for reading, POST often creates a resource or performs a command, PUT replaces a resource, PATCH changes part of it and DELETE removes it; these are application contracts, not automatic persistence provided by function names. In the disabled Cache Components lane, GET handlers are not cached by default and eligible GET output can opt into static behavior; other methods are not response-cached by this convention. Never treat a method export as access control.
Read a dynamic resource
// app/api/books/[id]/route.tsexport async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; if (id !== 'moss') return Response.json({ error: 'NOT_FOUND' }, { status: 404 }); return Response.json({ id, title: 'Moss Atlas' });}Expected behavior (not framework-tested): moss returns 200; an unknown ID returns 404. This synthetic public fixture does not establish a private-resource authorization policy.
Common mistake
Do not put a page and handler in the same segment to serve two unrelated representations. Give the API its own route such as /api/books. A handler callable by a browser is also callable by other clients; hidden navigation links do not protect it.
Practice and verification
Sketch a collection GET, a single-resource GET and a mutation endpoint. Write the expected methods and status codes, including missing resources and unsupported methods. Do not implement persistent writes until validation and authorization are defined.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.