route-handlers

Create and manage Next.js App Router API route handlers in route.ts files.

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/dr-code/beacon --skill route-handlers-dr-code
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: route-handlers
Source: https://github.com/dr-code/beacon/tree/main/plugins/nextjs-expert/skills/route-handlers
Command: npx skills add https://github.com/dr-code/beacon --skill route-handlers-dr-code

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Route handlers provide a consistent, server-side way to expose API endpoints in Next.js App Router applications, removing confusion about where and how to implement HTTP endpoints, streaming, and server-side behaviors.

Core Features & Use Cases

  • HTTP Method Exports: Define GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS as exported functions in route.ts to handle RESTful operations.
  • Request and Response Handling: Read JSON, form data, text, headers, cookies, and query parameters, and return structured JSON responses, redirects, or streamed data.
  • Advanced Patterns: Support dynamic route parameters, server-sent events and text streaming, file uploads and downloads with range requests, CORS preflight handling, caching controls, and integration points for databases, authentication, rate limiting, and error utilities.
  • Use Case: Build a paginated CRUD posts API that validates input, enforces authentication, streams large exports, and returns proper cache and CORS headers for public and authenticated consumers.

Quick Start

Create a POST route at /api/posts that validates incoming JSON, authenticates the user, creates a database record, and returns the new resource with status 201.

Frequently Asked Questions about route-handlers

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create REST API endpoints in Next.js App Router?

To create REST API endpoints in Next.js App Router, you export HTTP methods like GET, POST, PUT, PATCH, and DELETE as functions within a route.ts file. This approach handles request parsing, headers, cookies, and JSON responses for server-side operations.

Can I stream responses and use server-sent events with Next.js route handlers?

Yes, Next.js route handlers support streaming responses and server-sent events directly within route.ts files. You can return a ReadableStream to stream text, handle large data exports, and manage range requests for file downloads efficiently.

What is the best way to handle CORS and preflight requests in Next.js route handlers?

Handling CORS in Next.js route handlers involves managing OPTIONS preflight requests and returning appropriate CORS headers. You configure these headers within your route.ts file to enable cross-origin resource sharing for public and authenticated API consumers.

How do I parse request bodies, dynamic parameters, and cookies in Next.js App Router?

Next.js route handlers parse request bodies, dynamic route parameters, and cookies by utilizing the native Request object. You extract JSON or form data, read query parameters, and manage cookies directly inside your exported HTTP method functions.

Does Next.js route.ts support integrating database operations and authentication?

Yes, route.ts files fully support integrating databases and authentication. You can enforce auth checks, validate input, create database records, apply rate limiting, and implement robust error handling before returning structured JSON responses.

Why use Next.js route handlers instead of pages API for building APIs?

Next.js route handlers provide a consistent Web API standard for exposing endpoints, replacing the older pages API. They natively support streaming, caching controls, and dynamic params within the App Router, eliminating confusion around server-side HTTP behaviors.