API RoutesBeginner
1Question 1 of 1
typescriptHow do API Routes and Route Handlers work in Next.js?
Explanation & Concept
Route Handlers (`route.ts`) define custom request handlers for a given route using the Web Request and Response APIs. They support standard HTTP verbs: `GET`, `POST`, `PUT`, `DELETE`, `PATCH`.
Code Example
typescript
// app/api/orders/route.ts
import { NextResponse } from "next/server";
export async function POST(request: Request) {
const body = await request.json();
if (!body.itemId) {
return NextResponse.json({ error: "itemId required" }, { status: 400 });
}
// Process order in database
return NextResponse.json({ success: true, orderId: "ord_101" }, { status: 201 });
}How It Works & Key Takeaways
Route Handlers run in Node.js or Edge runtime environments, providing complete serverless API endpoints within your Next.js application.
Mock Test · 1 Questions · Optional
Ready for Middleware?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions