How to implement Pagination and Page Size with Azure Cosmos DB

Posted by

How to implement Pagination and Page Size with Azure Cosmos DB


This article describes how to implement pagination with Azure Cosmos DB REST service using max item count and continuation token to handle cases where the query result spans multiple pages.

The query engine can use the continuation token to continue the query without having to repeat previous executions and retrieve the next page according to the given page size. 


This article proposes a possible pagination implementation example with Azure Cosmos DB and focuses only on pagination-related aspects, excluding other types of configuration or explanations.

Problem Statement

At the current state Azure Cosmos DB Connector 1.0 does not support pagination handling externally (outside its connectors). According to the connector’s documentation, connectors such as List Documents and Query Documents, obtain a list of existing documents based on the specified query, with pagination support handled internally by the connector.

As a result, the connectors will always return the entire result without the possibility of determining the number of pages to retrieve or fetch the next pages.


To achieve a successful pagination implementation it is necessary to use HTTP Request Connector to call the Azure Cosmos DB REST service instead of using Azure Cosmos DB Connectors for operations where pagination is available, namely List Documents and Query Documents.

When calling Azure Cosmos DB REST service using HTTP Request Connector it is possible to pass relevant headers for the pagination implementation. Table 1 shows the minimal required headers necessary to query Azure Cosmos DB REST API as well as pagination-related headers such as “x-ms-continuation” and “x-ms-max-item-count”.

More information about request headers to query the REST API can be found in the official documentation of Azure Cosmos DB.








The authorization token for the request.


Required (on PUT, PATCH, and POST)


For POST on query operations, it must be application/query+json. 

For PATCH operations, it must be application/json_patch+json.




Token to retrieve the next page of results by resubmitting the request with this token.




An integer indicating the maximum number of items to be returned per page.




The date of the request per RFC 1123 date format expressed in UTC time.




Required for operations against documents and attachments when the collection definition includes a partition key definition.




The version of the Cosmos DB REST service.

Table 1: Small List of Azure Cosmos DB REST Request Headers 


This section describes one way to implement pagination and page size with Azure Cosmos DB.

Figure 1 depicts an interface example composed of an HTTP Listener, two Set Variable Components, a Transform Message, an HTTP Request, and a Logger. 

Figure 1: Basic Interface Example
On the interface, we preemptively allocate a variable to store the continuation token used to retrieve the next page of a given request. The continuation token is present in Cosmos DB REST API’s response after the first call if the result spans multiple pages. In that case, the continuation token present in Cosmos DB REST API’s response can be retrieved as follow:
Note: To retrieve the next page using the continuation token it is important to resent the same request including the previously retrieved continuation token which means the query and parameter must be identical.
The header name to tell the page size to Azure Cosmos DB is named "x-ms-max-item-count".
 Figure 2: Transform Message with Header definitions
Our implementation example contains a “Transform Message” component in charge of setting the required headers as well as pagination-related headers. The headers’ names must comply with those described above in Table 1. All the headers are stored in a target variable, namely “request_headers” and must be provided in the “header” section in the HTTP Request Connector as shown in Figure 2.

Note: That skipNullOn=”everywhere” declaration is used in case no continuation token is provided in the incoming request. In that manner, the field for the continuation token will be excluded and this header will not be present on the request.


Figure 3: HTTP Request with Custom Headers