liveBook · Manning

Pagination

As explained in API Design Patterns

Pagination is the division of a collection of results into separately retrievable pages.

Pagination divides a large collection—or a large single resource—into manageable chunks retrieved through multiple API requests. Each request returns results plus a cursor for continuing, avoiding oversized responses and excessive server transfers. section 21.0 section 21.2

Use pagination deliberately and follow these rules:

  • Accept a maximum page size rather than promising an exact size; the service may return fewer results before the page is full. section 21.7
  • Return a nextPageToken when more data exists, and treat its absence—not an empty results array—as the completion signal. section 21.7
  • Use opaque page tokens instead of page numbers; consumers needing a particular position should combine filters with ordering. section 21.2 section 21.4.2
  • Design the fields from the beginning, because changing an all-results response into paginated responses can break clients that expect one complete response. section 2.4.2

Listing messages with cursor pagination

A messages endpoint can accept parent, pageToken, and optional maxPageSize, then return results and nextPageToken. The client repeats the request with the returned token until no nextPageToken remains. The same design can page through large resource content by returning chunks, a field mask, and a continuation token. section 2.4.2 section 21.3.4

Listing 21.4 Final API definition
abstract class ChatRoomApi {
  @get("/chatRooms")
  ListChatRooms(req: ListChatRoomsRequest): ListChatRoomsResponse;
 
  @get("/{id=chatRooms/*/messages/*/attachments/*}:read")
  ReadAttachment(req: ReadAttachmentRequest): ReadAttachmentResponse;
}
 
interface ListChatRoomsRequest {
  pageToken: string;
  maxPageSize: number;
}
 
interface ListChatRoomsResponse {
  results: ChatRoom[];
  nextPageToken: string;
}
interface ReadAttachmentRequest {
  id: string;
  pageToken: string;
  maxBytes: number;
}
interface ReadAttachmentResponse {
  chunk: Attachment;
  fieldMask: FieldMask;
  nextPageToken: string;
}

Inspect the consolidated API definition covering collection pagination and paging inside a single large resource. section 21.3.5

How API Design Patterns covers it

How deep it goes
Defines cursors, page sizing, completion, tokens, and resource paging, but stops short of token-format implementation.
Examples
Demonstrates paginated Twapi messages, chat-room collections, and large attachment resources.
What it assumes
Assumes basic API request and response design while teaching chunked retrieval and cursor-based iteration directly.
What you can do after
Build it from scratch by defining page tokens, maximum sizes, results, and completion behavior.
How it connects
Connects cursors, continuation tokens, filtering, ordering, field masks, and backward compatibility.

Also explained in

Compare how 11 books explain pagination →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.