Topic 5 of 5 · 4 min read
Pagination, limits and OpenAPI contracts
Goal: Review an OpenAPI spec for unbounded lists and missing limits and choose a pagination scheme that suits the data.
Every endpoint that returns a list needs a maximum size, a stable order and a way to get the next page, and the OpenAPI spec should state all three so tests can check them. A generated GET /orders often runs SELECT * and returns everything. It works with 20 rows in development and takes the server down with 2 million in production.
Check yourself · 1 of 3
A generated GET /orders returns every order in one array. What is the main risk?
Why it matters
An unbounded list is a memory, bandwidth and latency risk, and an easy denial-of-service for anyone who can call it. It is also a contract problem: once clients depend on getting "all of them", adding a limit later is a breaking change.
Offset or cursor
- Offset (
?limit=50&offset=100): simple and lets users jump to page 7. It gets slower as the offset grows, and it skips or repeats rows when data is inserted between requests. - Cursor (
?limit=50&after=eyJpZCI6MTAwfQ): the server returns an opaque token pointing after the last row. It stays fast and stable on large, changing data, but cannot jump to an arbitrary page.
Use offset for small, mostly static lists with page numbers. Use cursors for feeds, logs and anything large or fast-changing. Either way, sort by a unique, stable key (such as created_at plus id), or pages overlap.
What to require
- A default
limitand a hard maximum, such as 50 and 200, enforced on the server. An out-of-range value gets a clamp or a400. - A documented sort order.
- A response with items and a
next_cursor(or null at the end). - Filters on indexed fields only, with a bounded set of values.
The spec is the contract
OpenAPI describes paths, parameters, schemas and responses. Review it as you review code: maximum on the limit parameter, required fields, a schema for every error response, and no additionalProperties: true by accident. Then run tests or a contract checker that compare real responses with the spec, so the document stays true.
Before and after
/orders:
get:
responses:
"200":
content:
application/json:
schema: { type: array, items: { $ref: "#/components/schemas/Order" } }
A bare array with no limit and no next page. Reviewed:
/orders:
get:
parameters:
- { name: limit, in: query, schema: { type: integer, minimum: 1, maximum: 200, default: 50 } }
- { name: after, in: query, schema: { type: string } }
The response becomes an object with items and next_cursor, which also leaves room for new fields later.
What goes wrong
- A bare array response. It cannot carry a cursor without a breaking change.
- Offset on a busy table. Rows move between pages while the client reads.
- A spec that drifts. Nobody tests it, so it describes an API that no longer exists.
Examples
The unbounded list
app.get("/orders", async (req, res) => {
res.json(await db.orders.findMany());
});
No limit, no order, bare array. Fix: parse limit with default 50 and maximum 200, sort by created_at, id, fetch limit + 1 rows to know whether a next page exists, and return { items, next_cursor }.
A spec with no bound
- name: limit
in: query
schema: { type: integer }
A client can ask for limit=10000000. Add minimum, maximum and default, and enforce the same values in the handler, because the spec alone does not stop a request.
Key points
- Every list endpoint needs a default limit, a hard maximum enforced by the server and a stable sort order.
- Use cursor pagination for large or fast-changing data and offset only for small, mostly static lists.
- Return an object with items and a next cursor rather than a bare array, so the response can grow without breaking clients.
- Sort by a unique key, or pages will repeat or skip rows.
- Treat the OpenAPI spec as the contract: review it, and run tests that compare real responses with it.
Common mistakes
- Shipping a list endpoint with no maximum page size.
- Paginating without a stable, unique sort order.
- Returning a bare JSON array that cannot hold a cursor.
- Writing a limit into the spec but never enforcing it in the handler.
Check yourself · 2 of 3
A feed table gets thousands of inserts per minute, and clients page through it. Which scheme fits best?
Check yourself · 3 of 3
Why should a list response be { "items": [...], "next_cursor": ... } rather than a bare array?
Practice items, not counted towards your mastery.
Start here
- Google API Improvement Proposals (AIP) (opens in a new tab)DocsFree
A large, consistent rulebook for resource naming, standard methods, pagination, errors and compatibility.
- RFC 9457: Problem Details for HTTP APIs (opens in a new tab)StandardFree
The standard JSON error shape with type, title, status, detail and instance members.
- Idempotent requests (Stripe API reference) (opens in a new tab)DocsFree
A widely copied idempotency-key design: key reuse, parameter mismatch and key expiry.
More for this topic
- OpenAPI Specification (opens in a new tab)StandardFree
The contract format for paths, schemas, parameters and responses that tools and tests read.