Skip to content

REST API design

Introduction

Design APIs with a shared convention: nouns for things, verbs for actions, status codes for outcomes, so callers can guess most of it without reading the docs.

Why use it

  • Guessable: GET /orders/123 clearly means “fetch order 123”.
  • Easier collaboration: frontend, mobile, and third parties share one convention.
  • Easier debugging: the status code tells you whether the client or the server was wrong.

How to do it

1. Paths use nouns (plural), not verbs

Avoid Prefer
POST /createOrder POST /orders
GET /getOrder?id=1 GET /orders/1
POST /deleteOrder DELETE /orders/1

2. Use HTTP verbs for the action

Verb Meaning Idempotent?*
GET Read, no data change Yes
POST Create No
PUT Full replace Yes
PATCH Partial update Usually not guaranteed
DELETE Delete Yes

*Idempotent: repeating the call has the same result as calling once; see Idempotency.

3. Status codes tell the truth

Status When
200 OK Read or update succeeded
201 Created Create succeeded
204 No Content Success with no body (e.g. delete)
400 Bad Request Bad request format or parameters
401 Unauthorized Not signed in
403 Forbidden Signed in but not allowed
404 Not Found Resource does not exist
409 Conflict Conflicts with existing data (e.g. duplicate)
429 Too Many Requests Rate limited
500 The server itself failed

4. Nesting for ownership: GET /users/5/orders means user 5’s orders. Keep nesting to at most two levels.

Component comparison

REST

Hand-rolledOpen sourceOpsLow

The most common API style: URLs represent resources, actions such as GET and POST represent operations, and JSON is returned.

Best forThe vast majority of web apps, mini-program backends, publicly exposed APIs.

Avoid whenFrontend page data shapes vary enormously, or services need high-performance internal communication.

Pros

The most universal: frontends, mini-programs and third parties can call it directly

The most tools and documentation, and it can be debugged right in the browser

Can use HTTP caching, with simple and easy-to-understand design rules

Cons

When the data a frontend wants is spread across several endpoints, multiple requests are needed

Endpoint response fields are fixed, so it is easy to return too much or too little data

No unified strong constraints; without team agreement, styles become mixed

GraphQL

Self-hosted open sourceOpen sourceOpsMedium

The frontend writes "which fields I want" itself, and a single endpoint returns exactly that, no more and no less.

Best forProjects with complex pages and many data relationships, where the frontend team wants to control data fetching.

Avoid whenSimple APIs, small teams, or when it must be opened to third parties for quick integration.

Pros

One request can bring back several related pieces of data

The client picks the fields it needs and sends less unused data

The type description comes with it, which helps generate docs and tools

Cons

One query can easily fan out into a large number of database requests

Caching, rate limits, and permissions are all harder than with REST

The team has to learn an extra query language and its tools

Official docs ↗

gRPC

Self-hosted open sourceOpen sourceOpsMedium

A high-performance way for services to communicate: both sides first agree on the interface in a shared file, then exchange compact binary data.

Best forHigh-frequency calls among internal services, or scenarios that need streaming.

Avoid whenFrontends or mini-programs must call it directly, or the API must be opened externally.

Pros

Small payloads and high speed, suited to frequent calls between services

The interface definition file can generate code in many languages; both C# and Python are supported

Supports bidirectional streaming

Cons

Browsers cannot call it directly; an extra translation layer is needed

Data is binary and hard to inspect and debug with ordinary tools

When opened to third parties, integration is harder for them than with REST

Official docs ↗

Checklist

  • Paths use plural nouns and contain no verbs
  • Verbs match the action; GET never modifies data
  • Creates return 201, deletes return 204, missing resources return 404
  • Distinguish 401 (not signed in) from 403 (no permission)
  • Errors are never returned as 200
  • List endpoints are paginated