Data Fetching
API Routes
Learn legacy Pages Router API Routes, how they compare with App Router Route Handlers, and when you will still encounter them.
1. Learning Objectives
By the end of this lesson, you will be able to:
pages/api handler.Difficulty: Beginner.
2. Prerequisites
3. Overview
API Routes are the Pages Router way to create backend endpoints in Next.js. They live under pages/api and use Node-style request and response objects instead of App Router route.ts files.
4. Why This Topic Matters
New App Router projects should usually use Route Handlers, but many existing Next.js apps still use API Routes. You need to recognize them during maintenance, migrations, and interviews.
5. Real-World Analogy
API Routes are like the older service window at a building. It still works and many people use it, but new construction may use a newer entrance with different conventions.
6. Core Concepts
| Concept | Meaning |
|---|---|
pages/api | Folder for Pages Router API endpoints. |
NextApiRequest | Request object used by API Routes. |
NextApiResponse | Response object used by API Routes. |
| Route Handler | App Router replacement for most new endpoint work. |
| Migration | Moving endpoint logic from pages/api to app/api/**/route.ts. |
7. Syntax & API Reference
Equivalent App Router style:
8. Visual Diagram
9. Live Example - Full Working Code
What just happened? The endpoint handles POST /api/contact in the legacy Pages Router style.
10. Interactive Playground
Try this:
pages/api/health.ts endpoint in a Pages Router project.app/api/health/route.ts.11. Common Mistakes
| Mistake | Why It Happens | Correct Approach |
|---|---|---|
Mixing API Route syntax in route.ts | Both create endpoints. | Use Web Request/Response in Route Handlers. |
| Adding API Routes to new App Router code by habit | Older tutorials use pages/api. | Prefer Route Handlers for new App Router endpoints. |
| Skipping method checks | API Route handler receives all methods. | Check req.method or split by App Router method exports. |
12. Best Practices
13. Browser Compatibility
| Feature | Browser Impact | Notes |
|---|---|---|
| API Route endpoint | Standard HTTP | Any client can call it. |
| JSON response | Broad support | Same response shape as other APIs. |
| Server-only logic | No browser bundle impact | Runs on server. |
14. Interview Questions
Easy: Where do API Routes live?Answer: Under pages/api.
Answer: Route Handlers using app/api//route.ts.
Answer: It may be a stable Pages Router or hybrid app where migrating all endpoints at once is unnecessary or risky.
15. Debugging Exercise
Bug report: "My app/api/health/route.ts imports NextApiRequest and does not work."
Solution
That is API Route syntax. Route Handlers use Web Request and Response objects and named method exports like GET.
16. Practice Exercises
pages/api/health.ts handler.POST API Route with method validation to route.ts.17. Scenario-Based Challenge
Your codebase has both pages/api/orders.ts and app/api/orders/route.ts. What should you check?
Walkthrough
Check for route conflicts, duplicated business logic, inconsistent validation, and which endpoint clients actually call. Consolidate when safe.
18. Quick Quiz
1. Are API Routes App Router files? Answer: No.
2. What folder contains API Routes? Answer: pages/api.
3. What file creates a Route Handler? Answer: route.ts.
4. Should new App Router apps prefer Route Handlers? Answer: Yes.
5. Do API Routes use NextApiRequest? Answer: Yes.
19. Summary & Key Takeaways
pages/api.20. Cheat Sheet
| Endpoint Type | Location | Request/Response Style |
|---|---|---|
| API Route | pages/api/*.ts | NextApiRequest, NextApiResponse |
| Route Handler | app/api/**/route.ts | Web Request, Response |
| Server Action | Any server action module | Function call/form action |
21. Further Reading
22. Next Lesson Preview
Chapter 4 is complete. Next, you will move into authentication patterns in Next.js.