Notion API, select all pages with a specific emoji
By Flavio Copes
Learn how to find Notion child pages with a specific emoji icon, including pagination and the extra page request needed to retrieve each icon.
~~~
Here’s something I used to select all subpages of a Notion page that used a specific emoji icon:
import { Client } from '@notionhq/client'
const notion = new Client({ auth: process.env.NOTION_API_KEY })
async function getChildPages(pageId) {
const pages = []
let cursor
do {
const response = await notion.blocks.children.list({
block_id: pageId,
start_cursor: cursor,
page_size: 100
})
for (const block of response.results) {
if (block.type !== 'child_page') continue
const page = await notion.pages.retrieve({
page_id: block.id
})
pages.push(page)
}
cursor = response.next_cursor ?? undefined
} while (cursor)
return pages
}
const pages = await getChildPages(process.env.NOTION_PAGE_ID)
const matchingPages = pages.filter((page) => {
return page.icon?.type === 'emoji' && page.icon.emoji === '✅'
})
The child block tells you that the item is a page, but it does not include the page icon. That is why the code retrieves each matching page separately.
This simple version makes those requests one at a time. For a large tree, add limited concurrency and retry handling instead of sending every request at once.
~~~
Related posts about tools: