API Design Basics
8 examples to get you started with API Design for Node.js backends - 6 basic and 2 intermediate.
Search across all documentation pages
8 examples to get you started with API Design for Node.js backends - 6 basic and 2 intermediate.
Express 5 or Fastify 5 with JSON body parsing and Zod installed.
npm install express@5 zodGET /v1/orders
POST /v1/orders
GET /v1/orders/:id
PATCH /v1/orders/:id
DELETE /v1/orders/:id/v1/orders/:id/items/createOrder RPC-style paths in public REST APIsRelated: Versioning & Deprecation -
/v1prefix
{ "data": { "id": "ord_1", "status": "pending" } }{ "error": { "code": "ORDER_NOT_FOUND", "message": "Order ord_99 not found" } }dataerror object with stable code for clientsdata as array plus pagination metaRelated: Error Response Standards - Problem Details
| Action | Code |
|---|---|
| Created resource | 201 |
| Success with body | 200 |
| Success no body | 204 |
| Validation failed | 400 |
| Missing auth | 401 |
| Forbidden | 403 |
| Not found | 404 |
| Conflict | 409 |
200 with { error: ... } in body422 acceptable for semantic validation if team standardizes itimport { z } from "zod";
import express from "express";
const createOrderBody = z.object({
customerId: z.string().uuid(),
sku: z.string().min(1),
qty: z.number().int().positive(),
});
const app = express();
app.use(express.json());
app.post("/v1/orders", (req, res) => {
const parsed = createOrderBody.safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({
error: { code: "VALIDATION_ERROR", details: parsed.error.flatten() },
});
}
res.status(201).json({ data: { id: crypto.randomUUID(), ...parsed.data } });
});PATCH /v1/orders/ord_1
{ "status": "shipped" }PATCH partial update; document allowed fieldsPUT full replacement when you support it - rare in B2B APIsIdempotency-Key header on POST payments (see intermediate)GET /v1/orders?limit=20&cursor=eyJpZCI6Im9yZF8xMjMifQRelated: Pagination & Filtering - cursor vs offset
app.use((req, res, next) => {
if (req.method === "POST" && !req.is("application/json")) {
return res.status(415).json({ error: { code: "UNSUPPORTED_MEDIA_TYPE" } });
}
next();
});application/jsonContent-Type and signature separatelyDocument routes as you ship them.
paths:
/v1/orders:
post:
summary: Create order
responses:
"201":
description: CreatedRelated: OpenAPI & Swagger - spec-first vs code-first
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.
Reviewed by Chris St. John·Last updated Jul 19, 2026