Suggest an editImprove this articleRefine the answer for “Where are API Routes created?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)API Routes in Next.js are created inside the project: in the **Pages Router**, in the `/pages/api` folder; in the **App Router**, in the `/app/api` folder in `route.ts` files. **Key point:** all API Routes code runs only on the server and never ends up in client-side JavaScript.Shown above the full answer for quick recall.Answer (EN)ImageAPI 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/posts ``` --- ## App 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/123 ``` --- ## Main 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 serverFor the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.