Where are API Routes created?
API Routes in Next.js are created inside the project, and the specific location depends on which router is used.
Pages Router
If the Pages Router is used, API Routes are created in the folder:
/pages/api
How this works:
- each file is a separate API endpoint
- the file name becomes part of the URL
Example:
/pages/api/users.ts → /api/users
/pages/api/posts.ts → /api/postsApp Router (Route Handlers)
In the App Router, API Routes are created in the folder:
/app/api
Inside it, a file is used:
route.ts (or route.js)
Example structure:
/app/api/posts/route.ts → /api/posts
/app/api/users/[id]/route.ts → /api/users/123Main difference between the approaches
- Pages Router:
- simpler and older
- works only through
/pages/api
- App Router:
- the modern approach
- better integrated with Server Components
- supports HTTP methods (
GET,POST,PUT,DELETE, etc.)
Where the API Routes code runs
Regardless of the approach:
- API Routes always run on the server
- do not end up in client-side JavaScript
- can safely work with a DB and secrets
Short summary
- In the Pages Router, API Routes are located in
/pages/api - In the App Router, API Routes are located in
/app/api/**/route.ts - Each file is a separate API endpoint
- All API Routes code runs only on the server
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.