← 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!"}
Ready to go further?

These free tutorials are a preview, our Frontend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Frontend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account