Skip to main content
The Notion SDK provides utilities to handle paginated API responses efficiently. When working with endpoints that return lists of items (like blocks, pages, or users), you can use pagination helpers to iterate through results without loading everything into memory at once.

Pagination Helpers

The SDK exports two main pagination utilities from the helpers module:
  • iteratePaginatedAPI() - Returns an async iterator for memory-efficient streaming
  • collectPaginatedAPI() - Collects all results into an array

Using iteratePaginatedAPI

The iteratePaginatedAPI() function returns an async iterator that yields items one at a time. This is ideal for processing large datasets without loading everything into memory.

Function Signature

Parameters

  • listFn - A bound function on the Notion client that represents a paginated API (e.g., notion.blocks.children.list)
  • firstPageArgs - Arguments to pass to the API on each call. The next_cursor is automatically managed.

Example: Iterate Through Block Children

Example: Iterate Through Database Pages

The iterator automatically handles the start_cursor and next_cursor for you. Each iteration fetches the next page of results as needed.

Using collectPaginatedAPI

The collectPaginatedAPI() function fetches all pages and returns a complete array. This is convenient for smaller datasets where you need the full collection.

Function Signature

Parameters

Same as iteratePaginatedAPI() - the function signature is identical.

Example: Collect All Blocks

Example: Collect All Users

Use collectPaginatedAPI() with caution on large datasets, as it loads all results into memory. For large collections, prefer iteratePaginatedAPI().

Manual Pagination

If you need more control over pagination, you can manually handle the cursor:

Pagination with Filters and Sorts

When querying databases with filters or sorts, the pagination helpers work seamlessly:

Best Practices

When processing many items, use iteratePaginatedAPI() to avoid loading everything into memory. This is especially important for databases with thousands of pages.
For convenience with smaller datasets (< 1000 items), collectPaginatedAPI() is simpler and provides an array you can easily manipulate.
The Notion API has rate limits. The SDK automatically retries rate-limited requests, but avoid making unnecessary parallel pagination requests.
Wrap pagination code in try-catch blocks to handle network errors or API issues:

Type Safety

Both pagination helpers are fully typed. TypeScript will infer the correct item type based on the list function you provide: