route.ts in Next.js — Building Custom APIs with Web Request and Response

Route Handlers in Next.js allow you to define custom request logic using Web APIs. With support for all major HTTP methods, cookies, headers, query parameters, dynamic segments, streaming, and webhooks, route.ts files offer a powerful way to build flexible, server-side functionality directly within your app.

route.tsNextRequestWeb APIStreamingWebhooks

~3 min read · Updated Oct 29, 2025

1. What Is route.ts?


route.ts files in Next.js let you define custom request handlers for specific routes using the Web Request and Response APIs. These handlers run on the server and support all major HTTP methods.


2. Supported HTTP Methods


  • GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
export async function GET(request: Request) {}
export async function POST(request: Request) {}
export async function OPTIONS(request: Request) {}

3. Accessing Request Data


Use NextRequest to access cookies, headers, and query parameters:

import { type NextRequest } from 'next/server'

export async function GET(request: NextRequest) {
  const token = request.cookies.get('token')
  const referer = request.headers.get('referer')
  const query = request.nextUrl.searchParams.get('query')
}

4. Dynamic Route Parameters


Use params to access dynamic segments:

// app/items/[slug]/route.ts
export async function GET(request, { params }) {
  const { slug } = await params
}

5. Using RouteContext


For strong typing, use RouteContext:

export async function GET(_req: NextRequest, ctx: RouteContext<'/users/[id]'>) {
  const { id } = await ctx.params
  return Response.json({ id })
}

6. Cookies


Read and set cookies using cookies() from next/headers:

const cookieStore = await cookies()
cookieStore.set('token', 'abc123')

7. Headers


Read headers with headers() or request.headers. To set headers, return a new Response:

return new Response('Hello', {
  headers: { 'X-Custom': 'value' },
})

8. Reading Request Body


const json = await request.json()
const formData = await request.formData()

9. Query Parameters


const query = request.nextUrl.searchParams.get('query')

10. Revalidating Cached Data


export const revalidate = 60

11. Redirects


import { redirect } from 'next/navigation'
redirect('https://nextjs.org/')

12. Streaming Responses


Use ReadableStream or AI SDK for streaming content:

async function* makeIterator() {
  yield encoder.encode('<p>One</p>')
  await sleep(200)
  yield encoder.encode('<p>Two</p>')
}

const stream = iteratorToStream(makeIterator())
return new Response(stream)

13. CORS Headers


return new Response('Hello', {
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET, POST',
    'Access-Control-Allow-Headers': 'Content-Type',
  },
})

14. Webhooks


export async function POST(request: Request) {
  const payload = await request.text()
  return new Response('Success!', { status: 200 })
}

15. Non-UI Responses


Serve XML, icons, or metadata directly:

// app/rss.xml/route.ts
return new Response(`<rss>...</rss>`, {
  headers: { 'Content-Type': 'text/xml' },
})

Conclusion


route.ts in Next.js gives you full control over server-side logic. Whether you're building APIs, handling cookies, streaming data, or responding to webhooks, Route Handlers offer a clean and powerful way to extend your app’s backend capabilities.


Written & researched by Dr. Shahin Siami

Related Articles

Advanced Client-Side Routing and Performance Hooks in Next.js

Next.js provides a rich set of client-side hooks and caching utilities that empower developers to build dynamic, responsive, and secure applications. From reading route parameters to tracking navigation state and reporting performance metrics, this guide walks you through the most important tools available in the App Router.

Continue

Handling Authorization and Caching in Next.js: A Developer’s Guide

Next.js introduces powerful experimental features for access control and smart caching. This guide covers the unauthorized() function for custom 401 handling, unstable_cache for persistent memoization, updateTag for instant cache invalidation, and useLinkStatus for inline navigation feedback. Learn how to use these tools to build secure, performant, and responsive applications.

Continue

redirect and refresh in Next.js — Smart Redirects and Client Refreshing via Server Actions

The redirect function in Next.js allows you to navigate users to a new route, returning either a 307 or 303 HTTP response depending on context. It works in Server Components, Client Components, Route Handlers, and Server Actions. The refresh function is used exclusively within Server Actions to refresh the client router. This article explains how both functions work, with practical examples and key considerations.

Continue

NextRequest and NextResponse in Next.js — Managing Cookies, Headers, Redirects, and Rewrites

Next.js extends the native Web Request and Response APIs with NextRequest and NextResponse, offering powerful tools for managing cookies, headers, redirects, rewrites, and JSON responses. These utilities simplify server-side logic and improve control over routing, personalization, and security. This guide walks through their capabilities with practical examples and best practices.

Continue

headers, ImageResponse, notFound, and permanentRedirect in Next.js — Request Handling, Dynamic Images, Errors, and Redirects

Next.js offers powerful tools for handling HTTP requests and responses in Server Components. The headers function lets you read incoming request headers. ImageResponse allows you to generate dynamic images using JSX and CSS. The notFound function renders a custom 404 page, and permanentRedirect enables permanent redirection to another route. This article explains how to use each feature with practical examples.

Continue

A Complete Guide to Using metadata and generateMetadata in Next.js

In modern versions of Next.js, managing page metadata is more powerful and intuitive than ever. Metadata is automatically injected into the <head> of your pages and plays a vital role in SEO, social sharing, and user experience. This guide explains the two main ways to define metadata: using the static metadata object and the dynamic generateMetadata function.

Continue