Route Handlers (APIs)
Building REST APIs and webhooks using route.ts.
Building APIs
Route Handlers allow you to create custom request handlers for a given route using the standard Web Request and Response APIs.
They are defined in a route.js or route.ts file.
Server-Side Endpoints
While Server Actions are great for mutations, sometimes you need a traditional API endpoint.
Route Handlers are perfect for webhooks (like receiving a Stripe payment success event), external API access, or standard REST endpoints.
HTTP Methods
You create endpoints by exporting functions named after HTTP methods.
Supported methods: GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS.
Example: export async function GET(request) { return Response.json({ data: 'hello' }); }
The REST Client
Use the mock REST client below to send different HTTP requests to the /api/users route handler. Watch how the correct exported function is triggered.
HTTP Client
/app/api/users/route.ts
return NextResponse.json(users);
const newId = await db.users.create(body);
return NextResponse.json({ success: true, userId: newId }, { status: 201 });
return NextResponse.json({ message: 'Deleted' });
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Route Handlers are defined in a
route.tsfile. - They export HTTP methods like
GET,POST, andDELETE. - They use the standard Web
RequestandResponseAPIs. - You cannot have a
route.tsandpage.tsxin the exact same folder.
Done with this concept?
Mark it complete to track your progress. No login needed.