Track 06 · Lesson 3 of 8APIs and servers

Track 06 · APIs and servers

Designing good APIs

Lesson 3 of 8· 7 min

A good API stays correct when data changes under it, and predictable when things go wrong. Pagination and error formats are where most APIs quietly let their clients down.

Page through a live feed

Load a page, add posts, load the next one

The next request

GET /posts?offset=0&limit=5
  1. Load a page, let new posts arrive, then load the next one.

Offsets and cursors#

Offset pagination (?offset=20&limit=10) counts positions. It’s simple and lets people jump to page 7, but when items are added or removed between requests, everything shifts: people see repeats or miss items. Cursor pagination (?after=post_16&limit=10) remembers which item you stopped at, so new items at the top don’t disturb the next page. Feeds, logs and anything that changes often should use cursors. Return the next cursor in the response so clients never build it themselves.

One shape for every error#

Pick one error format and use it everywhere, with a stable machine-readable code and a human message. The standard one, called Problem Details (RFC 9457), looks like this:

{
  "type": "https://api.pizza.example/errors/validation",
  "title": "Your request has invalid fields",
  "status": 422,
  "errors": [{ "field": "size", "message": "must be small, medium or large" }]
}

Never leak stack traces or database errors; they help attackers more than users.

Small things that matter#

  • Limit page sizes on the server, whatever the client asks for.
  • Use consistent names and formats: one casing style, ISO 8601 dates, amounts in cents.
  • Let clients filter and sort with query parameters instead of inventing new endpoints.

Check yourself

A feed uses ?offset=10&limit=10. Three new posts arrive between page 1 and page 2. What does the reader see?