Pagination with Notion API and PowerShell

As I have written in this Post Work with NOTION API and PowerShell – TechGuy, I recently started with Notion as my central Planning and Note Tool. So I immediately reached a lot of ToDos in my Database.

As you can read in this Notion Documentation Pagination (notion.com), the Result Size is always 100 for each API Query. So as I have more than 100 Tasks in my Database, I had to deal with the Paging in Notion API and my PowerShell Script.

The Notion PowerShell Script

So, Notion sends “has_more” and “next_cursor” when the query returns more than 100 Results. So See the Script on how we deal with that.


$NotionAPIKey="Your Secret"
$DatabaseID="Your Database ID"

$Notionheaders = @{
    "Authorization"  = "Bearer $($NotionAPIKey)"
    "Content-type"   = "application/json"
    "Notion-Version" = "2021-08-16"
}


$Cursor = $False
$AllNotionTasks = @()

do {
    if (!($Cursor)) {
        $Result = Invoke-RestMethod -Uri "https://api.notion.com/v1/databases/$DatabaseID/query" -Method Post -Headers $Notionheaders
    }
    else {
        $JsonBody = @"
            {"start_cursor":"$Cursor"}
"@
        $Result = Invoke-RestMethod -Uri "https://api.notion.com/v1/databases/$DatabaseID/query" -Method Post -Headers $Notionheaders -Body $JsonBody
    }
    
    if ($Result.has_more) { $Cursor = $Result.next_cursor }

    $AllNotionTasks += $Result
    $AllNotionTasks.results.count
    
} until (!($Result.has_more))

$AllNotionTasks.results




$Result.results

Github

All Notion examples can be found in my GitHub Repo: Seidlm/NOTION-PowerShell: PowerShell Scripts to work with NOTION (github.com)

Michael Seidl, aka Techguy
au2mate, everything

1 thought on “Pagination with Notion API and PowerShell”

  1. Pingback: Add a Page to a Notion Database with PowerShell - TechGuy

Leave a Comment

Your email address will not be published.

*

%d bloggers like this: