Suggest an editImprove this articleRefine the answer for “What are API Routes?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**API Routes** are a way to create **server-side API endpoints right inside Next.js**, without a separate backend server. **Key point:** Next.js can be **both the frontend and the backend at the same time**.Shown above the full answer for quick recall.Answer (EN)Image**API Routes** are a way to create **server-side API endpoints right inside Next.js**, without a separate backend server. In other words: Next.js can be **both the frontend and the backend at the same time**. --- ## The basic idea You describe a route, and Next.js turns it into an API: - an HTTP request arrives (GET, POST, etc.) - the code runs **on the server** - JSON or another response is returned - the browser or a server component gets the result In this setup: - the code **never reaches the client** - you can work with the DB, tokens, secrets - you don't need a separate Express / Nest / Fastify --- ## Where API Routes exist ### Pages Router (the classic option) - routes live in the `/pages/api` folder - each file is a separate API endpoint Example logic: - `/pages/api/posts` → `/api/posts` - `/pages/api/user` → `/api/user` --- ### App Router (the modern option) In the App Router, API Routes are called **Route Handlers**. - they live next to pages - they look like `route.ts` / `route.js` - they explicitly support HTTP methods (`GET`, `POST`, `PUT`, `DELETE`) In essence, it's the same API, but: - closer to the application's file structure - better integrated with Server Components and caching --- ## What you can do in API Routes You can: - access a database - work with authorization - proxy requests to external APIs - validate data - hide API keys - manage the cache You cannot: - use `window` or the DOM - store state between requests (the server can be stateless) --- ## Why they are needed API Routes solve several tasks: ### 1. Security - API keys stay on the server - the browser never sees them ### 2. Simplifying the architecture - no separate backend is needed - less infrastructure - simpler deployment ### 3. A proxy between the client and external APIs - client → API Route → external service - data can be normalized - responses can be cached ### 4. Working with forms and actions - submitting forms - saving data - server-side validation --- ## API Routes and caching API Routes: - can **use the cache** - can be **dynamic** - can take part in ISR - can run on the Edge or Node.js runtime This makes them part of Next.js's overall data-handling system. --- ## What API Routes are NOT Important not to confuse: - they are not a replacement for a full microservice backend - not suited for long-running tasks - do not store state between requests They are ideal for: - CRUD operations - integrations - logic "next to the UI" --- ## In short API Routes are: - server functions inside Next.js - accessible over HTTP - secure - tightly connected to rendering and caching They let you keep **data, logic, and UI in one project**, without turning the application into a monolith.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.