Skip to content
Guest
Next.js · Course Syllabus Show lessons
Data & Streaming · 9 min read · Intermediate

Route Handlers (route.ts) & JSON APIs

Build REST endpoints, webhooks, and health checks using app/api/.../route.ts with standard Web Request and Response objects.

What you'll learn

  • Export named HTTP verb handlers (GET, POST, PUT, DELETE) in route.ts
  • Validate request JSON and return proper 400/401/404 status codes
  • Keep webhook signature verification strictly on the server

Overview

Build REST endpoints, webhooks, and health checks using app/api/…/route.ts with standard Web Request and Response objects.

Next.js App Router unifies server rendering, streaming, type-safe mutations, and edge-ready caching inside a single React + TypeScript codebase.

Production Code Example

// app/api/health/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  return NextResponse.json({ ok: true, timestamp: Date.now() }, { status: 200 });
}

Key Takeaways

  • Export named HTTP verb handlers (GET, POST, PUT, DELETE) in route.ts
  • Validate request JSON and return proper 400/401/404 status codes
  • Keep webhook signature verification strictly on the server

Quick Check

Check Your Understanding

Score: 0 / 1

1. Can a single folder in Next.js App Router contain both `page.tsx` and `route.ts` at the exact same URL path?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind