Notion API Pagination Issue in Google Apps Script for Task Completion Tracking

Hey everyone! I’m stuck with a problem in my Google Sheets project. I’ve got this Apps Script that’s supposed to update a chart every hour. The chart shows data from a Notion database.

Here’s the thing: it works fine when there are less than 100 items, but it gets weird with more. For example, with 115 items:

  • The script says there’s more data even after the second fetch
  • It keeps returning 100 items instead of the last 15
  • Some items don’t match my filter in the second batch

I’ve tried adding the filter to the second fetch, but then it just keeps giving me the same 100 items over and over.

Here’s a snippet of my code:

function fetchNotionData() {
  const apiUrl = 'https://api.notion.com/v1/databases/mydb/query'
  const apiKey = 'my_secret_key'
  
  let hasMore = true
  let cursor = null
  
  while (hasMore) {
    const response = UrlFetchApp.fetch(apiUrl, {
      method: 'post',
      headers: {
        'Authorization': `Bearer ${apiKey}`,
        'Notion-Version': '2022-06-28'
      },
      payload: JSON.stringify({
        start_cursor: cursor,
        filter: { /* my filter here */ }
      })
    })
    
    const data = JSON.parse(response.getContentText())
    hasMore = data.has_more
    cursor = data.next_cursor
    
    // Process data here
  }
}

Any ideas what I’m doing wrong? Thanks!

I’ve encountered similar pagination issues with the Notion API. The problem likely stems from how Notion handles cursors and filters across paginated requests. Here’s what worked for me:

Instead of relying on the ‘has_more’ flag, try setting a fixed number of iterations based on your expected maximum item count. For example, if you expect up to 500 items, set a loop to run 5 times (500/100).

Also, ensure you’re passing the ‘next_cursor’ value correctly in subsequent requests. Sometimes, the API behaves unexpectedly when combining filters with pagination.

If the issue persists, consider breaking down your filter into smaller chunks and making separate API calls for each. This approach, while less efficient, often bypasses pagination quirks.

Lastly, double-check your filter syntax. Notion’s filtering can be finicky, especially with complex conditions across paginated requests.

I’ve dealt with this exact issue in my own projects. The Notion API can be a bit finicky with pagination, especially when filters are involved. Here’s a trick that worked for me:

Instead of using the ‘has_more’ flag, I switched to using the ‘results’ array length. If it’s less than 100, you know you’ve hit the last page. Something like this:

let allResults = [];
let cursor = null;

do {
  // Your existing API call code here
  
  allResults = allResults.concat(data.results);
  cursor = data.next_cursor;
} while (data.results.length === 100);

This approach ensures you get all the data, even if it’s not exactly what you expect. Also, make sure you’re not modifying your filter between requests - that can cause inconsistencies.

If you’re still having trouble, you might want to log the raw API responses to see exactly what’s coming back. Sometimes the devil’s in the details with these APIs.