In this lesson you will
- Create GET and POST endpoints with route.ts files
- Read and validate a JSON body and answer with the right status code
- Tell from the build table which handlers are static and which run per request
- Cache a handler and refresh it on demand with cacheTag and revalidateTag
Your first route handler
A route handler lives in the same folder tree as your pages, so its URL comes from its folders just like a page. The file app/api/emirates/route.ts answers at /api/emirates. The api folder is only a convention that keeps endpoints apart from pages; Next.js gives it no special meaning. Inside the file you export functions named after HTTP methods. Each one gets the standard web Request object and must return a Response. The helper Response.json() turns an object into JSON and sets the content-type header for you.
Look at the build table: /api/emirates is marked â—‹ Static. A GET handler follows the same rules as a page under Cache Components. This one reads nothing from the request and fetches nothing, so the build ran it once and saved the JSON as a file. Every request gets that file without running your code again, which is as fast as an API can be. Route handlers are not cached by default in any other sense: as soon as a handler reads the request body, headers or cookies, it runs for every request.
One folder can hold a page.tsx or a route.ts, never both, because both would answer the same URL. Keep pages and APIs apart: /shops is the page people read and /api/shops is the JSON other programs read. Both can call the same data function, so the logic stays in one place.
const EMIRATES = ["Abu Dhabi", "Dubai", "Sharjah", "Ajman", "Umm Al Quwain", "Ras Al Khaimah", "Fujairah"];
export async function GET() {
return Response.json({ count: EMIRATES.length, emirates: EMIRATES });
}├ ○ /api/emirates
├ ƒ /api/enquiry
├ ○ /api/rates 1h 1d
├ ƒ /api/refreshHTTP/1.1 200 OK
content-type: application/json
{"count":7,"emirates":["Abu Dhabi","Dubai","Sharjah","Ajman","Umm Al Quwain","Ras Al Khaimah","Fujairah"]}We ran the server on another port and removed the date, connection, cache and etag headers from the curl output; the status line, content type and body are exactly as printed.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Do I need Express or a separate backend with Next.js?
Not for most sites. Route handlers cover JSON APIs, form endpoints and webhooks inside the same app and deploy with it. A separate backend still makes sense for long running jobs, heavy processing or an API shared by many apps.
What is the difference between route handlers and API routes?
API Routes are the older Pages Router feature: files in pages/api that export one default function with req and res. Route handlers are the App Router version: route.ts files that export GET, POST and so on, using standard Request and Response.
How do I read query parameters in a route handler?
Create a URL from the request and read its search params, for example new URL(request.url).searchParams.get("city"). With NextRequest the shortcut is request.nextUrl.searchParams. Reading them makes the handler run per request.
Can a browser on another website call my route handler?
Browsers block reading the response from another origin unless you allow it with CORS headers such as Access-Control-Allow-Origin, and an OPTIONS handler for preflight requests. Servers, apps and curl are not limited by CORS, so protect private endpoints with keys or logins, not with CORS.
What is the difference between revalidateTag and revalidatePath?
revalidateTag refreshes cached data with a given tag on every page that uses it. revalidatePath refreshes one page or layout path, such as /shops. Tags are more precise when the same data appears on many pages.
Sources
Facts in this lesson were checked against these official pages on 26 November 2026.
- Next.js docs: Route Handlers
- Next.js docs: route.js API reference
- Next.js docs: revalidateTag
- MDN: HTTP response status codes
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.