When to still write a real API route

Server Actions are the right tool when your own Next.js app is the only client. The moment you need to expose an endpoint to someone else, Server Actions are the wrong fit. You want a traditional API route with a published contract, CORS headers, and clear versioning.

Three cases where you still reach for an API route. First, when a mobile app or a third party needs to call your backend. Second, when the endpoint needs a public URL you can stamp in documentation. Third, when you need custom headers, streaming, or anything else Server Actions do not support yet.

app/api/books/route.ts
tsx
import { NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { books } from '@/lib/schema';

export async function GET() {
  const data = await db.select().from(books);
  return NextResponse.json({ data });
}

export async function POST(request: Request) {
  const body = await request.json();
  await db.insert(books).values(body);
  return NextResponse.json({ ok: true }, { status: 201 });
}

A classic API route for cases where Server Actions do not fit.

Quiz: Quiz

Loading practice…