Recently is started to work with Notion, as OneNote was too limited, and with Notion, I can connect Notes with ToDos and many more. So, of course, I immediately started working with the API of Notion to Sync with Microsoft ToDo. This will be a future Blog Post. Today, we will begin with an easy example of working with Notion API and PowerShell.
Notion Documentation
The Documentation is always an excellent point to start with an API: Notion API
Next is the Notion API References to see some examples.: Introduction (notion.com)
API Authentication
The first thing is always the correct Authentication to run the Scripts without any User prompts. Notion is using API Keys, so we need to get some.
- Login to the Developers part in Notion and see “My Integrations” here: My integrations | Notion Developers
- Click “+ New Integrations”
- Fill in a Name and click Save
- At the next Page Copy the Secret Key, that Key will be used in our PowerShell Script
First Notion API Call with PowerShell
In our example, we want to query a Notion database with PowerShell and its content. I have created a Database with all My Tasks, projects, and Epics in Notion. This Database will be used for this.
When you look at the example herE: Query a database (notion.com), you note that we need the Database ID. So open the Database in Notion and check the Brwoeder URL for Id.
So the URL will look like this
https://www.notion.so/contoso/ae000000efd0000e000cf000bf000b0c?v=cf00000f0d000c0eaba0000d0ac000c0
So the first GUID is our Database ID, so in our example, it is “ae000000efd0000e000cf000bf000b0c”
Before you can query the Database, you need to share the Database with the API Integration we created a few minutes before.
Click on the “Share” on the top right and ensure your Integration has EDIT Permissions.
The PowerShell Script to query Notion Database
Now we have all Information and Perperations, so let’s run the Script
$NotionAPIKey="Your Secret"
$Notionheaders = @{
"Authorization" = "Bearer $($NotionAPIKey)"
"Content-type" = "application/json"
"Notion-Version" = "2021-08-16"
}
$DatabaseID="Your Databse ID"
$Result = Invoke-RestMethod -Uri "https://api.notion.com/v1/databases/$DatabaseID/query" -Method Post -Headers $Notionheaders
$Result.results
The Result then show each DB Entry in your Database
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
Pingback: Pagination with Notion API and PowerShell - TechGuy
Pingback: Add a Page to a Notion Database with PowerShell - TechGuy