Notion API database query pagination limits and result bounding
0 reputation · 09 Dec 2024, 05:45 UTC
0 reputation · 09 Dec 2024, 05:45 UTC
Bounding total results from a Notion database query for a large dataset requires understanding pagination behavior and result metadata. The query endpoint supports cursor pagination with page_size and start_cursor, and responses include has_more and next_cursor.
No total result count is returned in the documented response shape, so bounding total size would require client-side accumulation until has_more is false. Cursors are opaque tokens that must be used as returned, and stable pagination is described as requiring an explicit sort order.
Data mutations between paginated requests can affect which items appear on subsequent pages because cursors reflect a snapshot view rather than a fixed offset. The server enforces a maximum page_size per request.
Questions:
32775 reputation · 09 Dec 2024, 06:10 UTC
The Notion API does not provide a total result count in the response body of a database query. To determine the total size of a dataset, you must iteratively request pages and accumulate the results until the has_more property returns false.
page_size is 100 records per request. This limit is consistent across the database query endpoints.To bound or count your results, implement the following client-side logic:
POST request to /v1/databases/{database_id}/query with your desired page_size (up to 100).has_more boolean.has_more is true, extract the next_cursor and pass it as the start_cursor in the subsequent request.has_more is false or your specific bound (e.g., a maximum record limit) is reached.To verify your pagination logic is functioning correctly, you can use a small page_size to force multiple round-trips:
# Example: Requesting 1 record at a time to test cursor progression
curl -X POST https://api.notion.com/v1/databases/{database_id}/query \
-H "Authorization: Bearer {token}" \
-H "Notion-Version: 2022-06-28" \
-H "Content-Type: application/json" \
-d '{"page_size": 1}'
Confirm that the next_cursor from the first response successfully retrieves the second record when passed as the start_cursor.
Are you performing these queries against a database with frequent concurrent writes? If so, the risk of result instability increases, and you may need to implement a deduplication strategy based on the page ID.
Use comments to ask for clarification. Post a solution as an answer.
5,030 reputation · 09 Dec 2024, 12:40 UTC
If you omit the page_size parameter, the Notion API defaults to the maximum allowed value of 100 items per request. When has_more is false, the response includes next_cursor set to null (or omits the field), signalling the end of the result set.
Applying a filter object in the query payload can reduce the dataset before pagination, letting you obtain a bounded count or stop early once a desired number of matching records is reached, which avoids iterating through unrelated pages.