Skip to content

Pagination with a NextPageToken in .NET

The NextPageToken method provides more reliable pagination compared to the method of using NumberOfResults and SkipNumberOfResults, especially when updates occur between page requests. Unlike the skip/take method, which could miss or duplicate products when the dataset changes, the NextPageToken keeps track of the exact position in the data sequence, providing consistent results.

csharp
var client = new DataAccessor(
  new Guid("00000000-0000-0000-0000-000000000001"), 
  "your api key", 
  "server-url");
var products = new List<ProductResultDetails>();
Guid? nextPageToken = null;

Product List - An empty list to store the retrieved products.
NextPageToken - A nullable GUID used for pagination, initially set to null.

Product Query Loop

The following loop retrieves product details page by page until there are no more pages available.

csharp
while (true)
// Create a ProductQuery Object
{
    var productQuery = new ProductQuery(
        new FilterCollection(),
        excludeProductsWithNoVariants: false,
        pageSize: 3,
        nextPageToken: nextPageToken
    );

    // Next, send the query to the API
    ProductDetailsCollectionResponse response = client.Query(productQuery);
    // Retrieves the token for the next page from the response
    nextPageToken = response.NextPageToken;
    
    // Adds the retrieved products to the list
    products.AddRange(response.Products);
    
    // If nextPageToken is null, no more pages are available, and the loop breaks
    if (!nextPageToken.HasValue)
        break;
}

Loop Breakdown

FilterCollection - Filters to apply (empty in this example).
excludeProductsWithNoVariants - Set to false.
pageSize - Number of products per page (24 in this example). Note that the maximum page size is 1000.
nextPageToken - Token for the next page, starting with null.

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com