Skip to content

Next.js Route Handlers: Build a JSON API with GET, POST, Status Codes and revalidateTag

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

A website often needs more than pages. A mobile app wants your product list as JSON, a contact form needs somewhere to send its data, and a payment provider calls a webhook when an order is paid. In the App Router you answer all of these with route handlers: a route.ts file that exports one function per HTTP method and returns a standard web Response. No separate Express server is needed. In this lesson we build four small endpoints for the Souq Guide app from the earlier lessons, run them with npm run start, and show the real responses that curl printed with Next.js 16.4.6.

Quick answer

In Next.js, create an API endpoint by adding route.ts inside the app folder, for example app/api/emirates/route.ts, and export an async function named after the HTTP method: GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS. It receives the Request and returns a Response, usually Response.json(data, { status }). Methods you do not export get 405 automatically, and a folder cannot have both route.ts and page.tsx.

40 min read and practice · Content checked on 26 November 2026

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.

Example: A static GET handler
app/api/emirates/route.ts
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 });
}
Real output: terminal, npm run build (the API lines)
├ ○ /api/emirates
├ ƒ /api/enquiry
├ ○ /api/rates                1h      1d
├ ƒ /api/refresh
Real output: terminal, curl -i http://localhost:3000/api/emirates
HTTP/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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.