Pagination Helpers
The SDK exports two main pagination utilities from thehelpers module:
iteratePaginatedAPI()- Returns an async iterator for memory-efficient streamingcollectPaginatedAPI()- Collects all results into an array
Using iteratePaginatedAPI
TheiteratePaginatedAPI() 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. Thenext_cursoris 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
ThecollectPaginatedAPI() 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 asiteratePaginatedAPI() - the function signature is identical.
Example: Collect All Blocks
Example: Collect All Users
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
Use iteratePaginatedAPI for large datasets
Use iteratePaginatedAPI for large datasets
When processing many items, use
iteratePaginatedAPI() to avoid loading everything into memory. This is especially important for databases with thousands of pages.Use collectPaginatedAPI for small collections
Use collectPaginatedAPI for small collections
For convenience with smaller datasets (< 1000 items),
collectPaginatedAPI() is simpler and provides an array you can easily manipulate.Respect rate limits
Respect rate limits
The Notion API has rate limits. The SDK automatically retries rate-limited requests, but avoid making unnecessary parallel pagination requests.
Handle errors gracefully
Handle errors gracefully
Wrap pagination code in try-catch blocks to handle network errors or API issues: