Skip to main content

Pagination

Many APIs return large datasets split across multiple pages. APIMatic supports pagination in generated SDKs through a flexible configuration mechanism in the OpenAPI specification. Developers can use a simple and consistent SDK interface to navigate through paginated results, regardless of the underlying pagination style used by the API.

Pagination Configuration in OpenAPI

Pagination in APIMatic is configured using the pagination OpenAPI extension. This extension allows specifying how to send pagination inputs in the request and where to extract results or tokens from the response. All pagination configuration fields accept JSON Pointers, enabling fine-grained customization of how request parameters and response structures are interpreted.

Example Usage

Once the OpenAPI pagination configuration is in place, the generated SDKs provide an easy-to-use interface for consuming paginated responses. The SDKs abstract away the logic of computing page tokens, building follow-up requests, and iterating over datasets.

For asynchronous API calls, the endpoint function will return an instance of PagedFlux.

Integer page = 1;
Integer size = 25;
PagedFlux<Item, PagedResponse<Item, Page>> result = controller.fetchDataAsync(page, size);

// Iterating over items in all the pages.
result.subscribe(
item -> System.out.println(item),
error -> _error.printStackTrace());

// Iterating over all the pages.
result.pages().subscribe(
pagedResponse -> {
// Iterating over items in the current page.
pagedResponse.getItems().forEach(item -> System.out.println(item));

// Extracting paged response body.
System.out.println(pagedResponse.getResult());

// Extracting paged response headers.
System.out.println(pagedResponse.getHeaders().asSimpleMap());

// Extracting paged response status code.
System.out.println(pagedResponse.getStatusCode());
},
error -> _error.printStackTrace());

In this example:

  • result: The paginated response from the endpoint, behaves as an Flux<Item> if subscribed directly.
  • result.pages(): An Flux for subscribing pages directly. Returns Flux<PagedResponse<Item, Page>>.
  • pagedResponse.getItems(): Returns the list of items in the current page.
  • pagedResponse.getResult(): The actual instance of the current page with type Page.
  • pagedResponse.getHeaders(): The Http headers returned along with each page.
  • pagedResponse.getStatusCode(): The Http status code returned along with each page.
  • Error Handling: Provides the error handler lambda function for errors like ApiException and IOException.

Learn more about Flux.

Page Meta Data

When pagination is enabled for an SDK, each page of the API response will include metadata in addition to the actual response data. This metadata helps developers understand which request parameters were responsible for generating that specific page of results.

To include this metadata, the response is wrapped in a PagedResponse type, which may take one of the following forms:

  1. CursorPagedResponse
  2. LinkPagedResponse
  3. NumberPagedResponse
  4. OffsetPagedResponse

The following code samples illustrate how to retrieve metadata from each variant of PagedResponse, processing it page by page.

String cursor = "id_123";
Integer limit = 25;
PagedFlux<Item, CursorPagedResponse<Item, Page>> result = controller.fetchDataAsync(cursor, limit);

// Iterating over all the pages and extracting cursor value that's used to fetch each page.
result.pages().subscribe(
pagedResponse -> {
System.out.println(pagedResponse.getNextCursor());
});

In this example:

  • pagedResponse.getNextCursor(): The next cursor from the previous response used to fetch the current page.

Benefits

  • Uniform SDK Experience: Same interface regardless of pagination strategy.
  • No Extra Logic Needed: No need to parse tokens or handle loop termination manually.
  • OpenAPI-Driven: Automatically applied based on x-pagination configuration.
  • Customizability: Flexible placement of input and output using JSON Pointers.