Adding pagination to SDKs
Customize pagination rules for each API operation using the x-speakeasy-pagination
extension.
Adding pagination to an SDK enhances the developer experience by providing a structured way to handle paginated API responses.
The next()
function returns nil, nil
when there are no more pages to retrieve, indicating the end of pagination rather than an error
Configuring pagination
To configure pagination, add the x-speakeasy-pagination
extension to the OpenAPI description.
The x-speakeasy-pagination
configuration supports offsetLimit
, cursor
, and url
implementations of pagination.
Offset and limit pagination
For type: offsetLimit pagination
, specify at least one of the following inputs
: offset
or page
.
At least one response object must have the following structure:
If inputs.limit
is defined in the pagination configuration, next()
will return null
when $.data.resultArray
has a length of less than the inputs.limit
value. If inputs.limit
is omitted, next()
will return null
when the length of $.data.resultArray
is zero.
When using the page input, output.numPages
can be used instead of output.results
to determine when the pages for the operation are exhausted.
If output.numPages
is provided, next()
returns null
when the incremented page number is greater than the numPages
value.
At least one response object must have the following structure:
For example, in the following inputs.offset
configuration, inputs.limit
has the same effect as in the inputs.page
example.
Cursor-based pagination
For type: cursor pagination
, configure the nextCursor
output. This pagination type uses a cursor value from the previous response.
The following is an example inputs.cursor
configuration.
Because the input above is in
the requestBody
, this operation must take a request body with at least the following structure:
At least one response object must have the following structure:
The [-1]
syntax in outputs.nextCursor
indicates the last value in an array using JSONPath negative indexing. Ensure the type of requestBody.since
matches the type of outputs.nextCursor
.
URL-based pagination
When an API returns a URL for the next page, you can use the url
type in x-speakeasy-pagination
. Here’s an example configuration:
The x-speakeasy-pagination
configuration specifies the type as url
and uses a JSONPath expression to extract the nextUrl
from the response.
The response object for the URL-based pagination should have the following structure:
Inputs
name
With in: parameters
, this is the name of the parameter to use as the input value.
With in: requestBody
, this is the name of the request-body property to use as the input value.
in
Indicates whether the input should be passed into the operation as a path or query parameter (in: parameters
) or in the request body (in: requestBody
). Only simple objects are permitted as values in the request body.
type
Type | Description |
---|---|
page | The variable that will be incremented on calling . |
The variable that will be incremented by the number of results returned by the previous execution. Note: Requires . | |
When provided, returns (or equivalent) when the number of results returned by the previous execution is less than the value provided. | |
cursor | The variable that will be populated with the value from when calling . Note: Required for pagination. |
Outputs
All the outputs are expected to be strings adhering to the JSONPath schema.
Key | Description |
---|---|
When provided, returns if the input value exceeds the value found at the provided JSON path. Note: Requires input. | |
When provided, returns if the array found at the provided JSON path is empty. Note: Required by input. | |
Populates with the value found at the provided JSON path when calling . Note: Required by . |
If the JSONPath value provided for an output does not match the response returned, next()
returns null
because pagination cannot be continued.