← Next.js tutorial

Next.js

API Routes

A route.js file under app/api/... becomes a real backend endpoint.

Example: a tiny API endpoint

// app/api/hello/route.js
export async function GET() {
  return Response.json({ message: "Hello from the API!" });
}

Export a function named after the HTTP method it should handle (GET, POST, and so on — the same methods from the backend track's requests-and-responses lesson), and return a Response. A request to /api/hello now returns real JSON, no separate backend server or separate deployment required — this small project can keep its frontend and backend together in one codebase, which is exactly why API routes are popular for small apps and prototypes.

Example
// app/api/hello/route.js
export async function GET() {
  return Response.json({ message: "Hello from the API!" });
}
Output
A GET request to /api/hello returns: {"message": "Hello from the API!"}