Skip to content
Backend & APIChapter 23 / 57Intermediate7 min read

API Handler Shape

REST API handler best practices. Every route handler runs in the same order, from auth to response, and keeps business logic in a service.

On this page

1. What is this practice?

By handler we mean a Next.js route handler (app/api/.../route.ts), a server action, or a controller method in a backend framework (like AdonisJS). It is the first place where an HTTP request touches your code.

Think of the handler as a traffic officer, not a kitchen. It sees who came, what they brought, and hands the job to the right department. Calculating a price, deciding a status or building a query is not its job.

A good rule of thumb: if a handler grows past 40 lines, it is hiding logic that belongs in a service.

2. Why does it matter?

Imagine a 150 line handler. The auth check is on line 60, and a query has already run before it. Tomorrow a developer adds an early return on line 30 and the endpoint quietly becomes public. Nobody spots it in review because the handler is hard to read.

Another real problem: the discount calculation is inside the handler. Then a cron job needs the same calculation, so the code gets copied. A bug is fixed in one copy and not the other, and customers get different bills.

Without the ruleWith the rule
The auth check is somewhere in the middle, and sometimes forgottenAuth is always the first line, visible at a glance in review
Logic is tied to HTTP, testing needs a serverA service function is unit tested without HTTP
Cron or a script has to copy the logicCron, scripts and the handler all call one function
Every endpoint is written in its own styleEvery endpoint looks the same, a new developer understands it right away
Expensive work happens before the rate limitThe rate limit applies before the body is parsed

3. When to follow it

LevelHow importantWhy
Learning projectA good habit, but not strictKeeping the order even in two or three endpoints pays off later
Production codeA mustMissing security checks get caught here
Senior levelEnforce it across the teamIn review, move logic from a long handler into a service

When to learn it: once you have written your first route handler or server action and understand the idea of a Service Layer.

4. Bad vs good code

Bad: everything inside the handler

ts
export async function POST(request: Request) {
  const body = await request.json();
  const product = await db.query("select * from products where id = $1", [body.productId]);

  let price = product.rows[0].price * body.quantity;
  if (body.quantity > 10) {
    price = price * 0.9;
  }

  const user = await getCurrentUser();
  if (!user) {
    return Response.json({ message: "please log in" }, { status: 401 });
  }

  const order = await db.query(
    "insert into orders (user_id, product_id, total) values ($1, $2, $3) returning *",
    [user.id, body.productId, price],
  );

  return Response.json(order.rows[0]);
}

Here the query runs before auth, the body is not validated, the discount rule is stuck inside the handler, and the whole database row goes to the client.

Good: the handler only handles the order of steps

File app/api/orders/route.ts:

ts
import { NextResponse } from "next/server";

import { getCurrentUser } from "@/lib/auth/auth";
import { createOrder } from "@/lib/orders/orders";
import { OutOfStockError } from "@/lib/orders/orders.errors";
import { createOrderSchema, ORDER_MESSAGES } from "@/lib/orders/orders.types";
import { limitByKey } from "@/lib/rate-limit/rate-limit";

export async function POST(request: Request) {
  const user = await getCurrentUser();
  if (!user) {
    return NextResponse.json({ error: ORDER_MESSAGES.unauthorized }, { status: 401 });
  }

  const limit = await limitByKey(`orders:create:${user.id}`);
  if (!limit.allowed) {
    return NextResponse.json({ error: ORDER_MESSAGES.rateLimited }, { status: 429 });
  }

  const parsed = createOrderSchema.safeParse(await request.json().catch(() => null));
  if (!parsed.success) {
    return NextResponse.json({ error: ORDER_MESSAGES.invalidInput }, { status: 422 });
  }

  try {
    const order = await createOrder(user.id, parsed.data);
    return NextResponse.json({ order }, { status: 201 });
  } catch (error) {
    if (error instanceof OutOfStockError) {
      return NextResponse.json({ error: ORDER_MESSAGES.outOfStock }, { status: 409 });
    }
    throw error;
  }
}

The discount and the insert now live in lib/orders/orders.ts:

ts
export async function createOrder(userId: string, input: CreateOrderInput) {
  const product = await findProductById(input.productId);
  if (product.stock < input.quantity) {
    throw new OutOfStockError(product.id);
  }

  const total = calculateTotal(product.price, input.quantity);

  return insertOrder({ userId, productId: product.id, total });
}

What is the difference?

ThingBad codeGood code
AuthAfter the queryFirst of all
Rate limitNoneBefore the body is parsed
ValidationNone, body.quantity could be anythingA Zod schema, 422 on failure
Business logicIn the handlerIn the createOrder service
ErrorsA crash or a 500Known errors mapped to 409
ResponseThe whole DB rowOnly the order from the contract

5. How to apply it

The order of every handler
Authenticate and authorize (with a shared helper)
Rate limit (before parsing the body)
Parse and validate the input (400 or 422)
Call one service function
Respond with the contract, or map a known error to a status

A checklist when writing a new endpoint:

  • The first line resolves the caller, or the endpoint clearly states that it is public.
  • No select, insert or domain if in the handler.
  • One service call. If you need to combine two or three services, that combining is the service's job.
  • For a server action the order is the same, only a return value replaces the response.

6. Exceptions and common mistakes

Some endpoints skip some steps. A health check is public and needs little rate limiting. A webhook has no session, so authentication there means verifying the signature. But the order stays the same, only the shape of the step changes.

Mistake: Applying the rate limit after validation

Why? An attacker sends a large body and makes you pay for parsing and validation, and the server tires out before the limit is even reached.

Better: Apply the rate limit as soon as you resolve the caller, then parse the body.

Mistake: Writing helper functions in the route file

Why? The handler file turns into a mini service again, and the same logic cannot be reused elsewhere.

Better: Keep the helper in the service module or its library file, and let the handler only import it.

Mistake: Orchestrating three services in the handler

Why? Create the order, send the email, reduce inventory: this flow cannot be reused from cron, and partial failure is not handled.

Better: Let one service function run the whole flow, and let the handler only call it.

Mistake: Catching every error and returning 500

Why? Even an expected case like out of stock looks like a server error, and the client cannot show the right message.

Better: Create specific classes for known errors and map them to 404, 409 or 422, and let the rest bubble up.

7. What to learn before and after

Final quick revision

CheckQuestion
Auth firstDoes the first line resolve the caller?
Rate limitIs it applied before the body is parsed?
ValidationIs the input parsed into a typed value with a schema?
One service callIs there any query or domain if in the handler?
ResponseAre only the fields from the contract going back?
LengthIs the handler within 40 lines?