Skip to content
Watch the complete Next.js 15 course on YouTube

GET request handlers

This page explains how to implement GET request handlers in Next.js route handlers.

Understanding route handlers

Route handlers allow you to create custom API endpoints in your Next.js application. The GET handler is one of the most common types, used to retrieve data from your server.

Creating a GET handler

To create a GET request handler:

  1. Create a route.ts file in your route segment
  2. Export an async function named GET
  3. Return a Response object
app/api/comments/route.ts
export async function GET() {
const comments = [
{ id: 1, text: "First comment" },
{ id: 2, text: "Second comment" },
];
return Response.json(comments);
}

Response formats

The handler can return data in different formats:

// JSON response
return Response.json(data);
// Text response
return new Response("Hello");
// HTML response
return new Response("<h1>Hello</h1>", {
headers: { "Content-Type": "text/html" },
});

Good to know

  • Route handlers only work in the app directory
  • The function name must match the HTTP method (GET, POST, etc.)
  • Handlers receive the Request object as their first parameter