Pages API Routes
What you need to know
A Pages API Route is a server-side HTTP endpoint under pages/api. It receives a NextApiRequest and NextApiResponse and must explicitly define supported methods, validation and response contracts.
By the end, you can: define handler request and response contract; handle methods explicitly.
A public read-only Pages API contract
// pages/api/stock.tsimport type {NextApiRequest,NextApiResponse} from 'next';export default function handler(req:NextApiRequest,res:NextApiResponse){ if(req.method!=='GET'){res.setHeader('Allow','GET');return res.status(405).json({error:'Method not allowed'});} const slug=req.query.slug; if(typeof slug!=='string'||!['fern-atlas','river-walk'].includes(slug)) return res.status(400).json({error:'Invalid slug'}); res.setHeader('Cache-Control','no-store'); return res.status(200).json({slug,available:slug==='fern-atlas'});}Expected behavior: GET with a supported single slug returns the stock fixture. Invalid or repeated slugs produce 400. Unsupported methods produce 405 and Allow: GET.
How it works
The handler receives req.method, query, cookies, headers and parsed body as applicable; res provides status, json, setHeader and other Node-style response methods. Default body parsing can be configured per route through the exported config object, including api.bodyParser.sizeLimit. A raw body may be needed for verified signature workflows, but do not disable parsing without a bounded replacement. Browser CORS restrictions do not authenticate a caller; a direct HTTP client is not constrained by browser CORS. A request body can be missing or malformed, so TypeScript annotations alone do not validate it. A known GET-only public endpoint should return 405 and Allow for unsupported methods. A mutation requires server authentication, resource authorization, validation and a transactional write; none is provided by merely placing a file under pages/api. API Routes require a server runtime, not a pure static export. Pages ISR also exposes res.revalidate for on-demand regeneration of a known concrete page path; any endpoint that invokes it needs authentication and a fixed allowed path policy. Do not expose a public cache mutation endpoint. App Route Handlers use Web Request/Response and named method exports instead, which is covered next.
Design a protected ISR operation without exposing an endpoint
Verified server-side actor -> authorize catalog update -> commit data -> revalidate exact /catalog/fern-atlasUnauthorized actor -> deny before mutation or revalidationRegeneration failure -> report and use bounded recovery policyExpected behavior: This is a staged design; res.revalidate must be called in the Pages API response context, not a client component.
Common mistake
Do not assume req.body is trustworthy, omit method checking or revalidate an arbitrary path supplied by a caller. Never return raw internal exception details or session credentials.
Practice and verification
Write contract assertions for 200, 400, 405, Allow, Content-Type and no-store on successful stock responses. Verify negative authorization tests before introducing an administrative ISR endpoint.
Scope: Proposed Next.js 16.x, React 19, Node runtime. Root-mounted Pages and mixed-router fixtures with Cache Components disabled; the basePath example is separate. Framework examples are staged, not executed. Exact patched dependencies and platform contracts pending.