Resource-Oriented REST APIs

Design JSON-over-HTTP APIs around resources, methods and status codes that clients can predict.

Beginner · about 8 hours · difficulty 2/5

Learn first: HTTP in Depth

REST is an architectural style where your API exposes resources (users, orders, invoices) at URLs and clients act on them with standard HTTP methods. Most public and internal web APIs are "RESTful" JSON APIs, even if few follow every constraint of the original definition.

Designing resources

  • Use nouns, plural collections: GET /orders, GET /orders/42, POST /orders, PATCH /orders/42, DELETE /orders/42.
  • Nest only for clear ownership: GET /users/7/orders. Avoid deep nesting.
  • For actions that do not map to CRUD, use a sub-resource: POST /orders/42/cancel.
  • Return the right status: 201 with a Location header after create, 204 for no content, 404 for missing, 409 for conflicts, 422 for validation errors.

Consistent payloads

Pick a JSON style (camelCase or snake_case) and stick to it. Use ISO 8601 timestamps. Return errors in one shape everywhere, for example { "error": { "code": "VALIDATION_FAILED", "message": "...", "details": [...] } }, or the standard Problem Details format (RFC 9457).

Beyond the basics

  • Statelessness: each request carries everything needed (for example an auth token), which makes horizontal scaling easy.
  • HATEOAS: responses include links to related actions. Rarely used in full, but useful to know.
  • JSON:API is one opinionated convention for payload shape if your team wants a standard.

Pitfalls

  • Verbs in URLs (/getUser) and inconsistent naming across endpoints.
  • Leaking database structure directly; your API is a contract, your schema is an implementation detail.

You are ready to move on when you can design the endpoints for a small e-commerce API on paper and defend every method and status code choice.

Key concepts

  • Resources and URL design
  • Methods and status codes in practice
  • JSON conventions
  • Error format and Problem Details
  • Statelessness
  • JSON:API specification
  • HATEOAS

Mini project

Build a REST API for a recipe app with collections, nested ingredients, a custom action endpoint and one consistent error format across all failures.

Check yourself

A client submits an order with an invalid quantity. Which status code do you return and what does the body look like?

Free resources