In this technical blog, we will discuss how to use the Notion API to select all subpages of a Notion page that use a specific emoji icon. We will provide a code example that demonstrates this functionality.
To begin with, you will need to have the Notion API key and the ID of the Notion page you want to work with. Make sure to include these in your code.
const notion = new Client({ auth: process.env.NOTION_API_KEY })
const pageId = process.env.NOTION_PAGE_ID
Next, we will define an asynchronous function called getAllSubpagesOfPage
that takes a page
object and the notion
client as parameters. This function will retrieve all the subpages of the given page.
async function getAllSubpagesOfPage(page, notion) {
const pages = []
const blocks = await notion.blocks.children.list({
block_id: page.id,
})
for (const block of blocks.results) {
if (block.type === 'child_page') {
const temp = await notion.pages.retrieve({ page_id: block.id })
block.icon = temp.icon
pages.push(block)
}
}
return pages
}
In the above code, we use the blocks.children.list
method to retrieve all the blocks within the given page. We iterate through these blocks and check if each block is a child page. If it is, we retrieve the page object using the pages.retrieve
method and add the icon
property to the block.
Finally, we call the getAllSubpagesOfPage
function and store the result in the pages
variable.
const pages = await getAllSubpagesOfPage(page, notion)
To further filter the pages based on a specific emoji icon, we can use the map
method and check the emoji
property of the icon
object.
pages.map(async (page) => {
if (page.icon?.emoji === '✅') {
// Page has the desired emoji icon
// Perform necessary actions here
}
})
This code snippet demonstrates how to utilize the map
method to iterate through the pages
array and check if each page’s icon emoji matches the desired emoji icon.
By following these steps and using the provided code example, you can select all subpages of a Notion page that contain a specific emoji icon using the Notion API.
Tags: Notion API, subpages, emoji icons, code example