<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a5a1e3ce-00fa-46e4-9a75-58c2aed9e8ce/Notion_Fundamentals_with_Thomas_Frank_-_Avatar_2021.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a5a1e3ce-00fa-46e4-9a75-58c2aed9e8ce/Notion_Fundamentals_with_Thomas_Frank_-_Avatar_2021.png" width="40px" /> This example page shows how you can use the Notion API to query a database that has more than 100 rows. The database here contains a row for each of the first 251 Pokémon.

When querying a database via the API, you’re limited to 100 results per query. However, if the query would fit more than 100 rows, a pagination token is included in the response. You can then query the database again, passing the pagination token in order to have the query start at the row it points to.

In this way, you can make as many queries as you need in order to fully query a large database.

This script was built in Pipedream; my favorite code-friendly automation platform.

Created by Thomas Frank | Notion Basics | Learn the Notion API | Templates | Twitter

</aside>

Pokémon Stats

My script queries the entire database, then updates the blocks below to display the Pokémon that have the highest HP, height, weight, etc.

See my tutorial on Get Highest Number Value from a Database for an in-depth explanation on how this part works. The tutorial below will primarily focus on sending multiple queries using pagination tokens.


Example Database

Pokédex

Video Tutorial

https://www.loom.com/share/d2a5d6bf6d164ffd864cf433cd5bb281

Pipedream Code

Pipedream setup:

import { Client } from "@notionhq/client"
export default defineComponent({
  props: {
    notion: {
      type: "app",
      app: "notion",
    }
  },
  async run({steps, $}) {

    const notion = new Client({auth: this.notion.$auth.oauth_access_token});

    const rows = []

    let hasMore = undefined
    let token = undefined

    while (hasMore == undefined || hasMore == true) {
      let resp
      const db_id = "8cc631e2fabd4f5db230a235d64775a5";
      if (token == undefined) {
        resp = await notion.databases.query({
          database_id: db_id,
          page_size: 100
        });
        rows.push(resp.results);
      } else {
        resp = await notion.databases.query({
          database_id: db_id,
          page_size: 100,
          start_cursor: token
        });
        rows.push(resp.results);
      }
      hasMore = resp.has_more
      if (resp.next_cursor) {
        token = resp.next_cursor
      }
    }

    const flatRows = rows.flat()

    const stats = {
      hp: finder("HP"),
      height: finder("Height"),
      weight: finder("Weight")
    }

    function finder(key) {
      const value = flatRows.find((number) => {
        return number.properties[key].number === Math.max(...flatRows.map(o => o.properties[key].number))
      })
      return value
    }
    
    const blocks = {
      total: {
        id: "44c093ed13ac4c0f8336c045ea76dd4f",
        val: [
          {
            type: "text",
            text: {
              content: `There are currently ${flatRows.length} Pokémon in this database.`
            }
          }
        ]
      },
      hp: {
        id: "7bda141e6cc8479ca96f7911d65a2d22",
        val: [
          {
            type: "text",
            text: {
              content: `Highest Base HP: `
            }
          },
          {
            type: "mention",
            mention: {
              type: "page",
              page: {
                id: stats.hp.id.replace(/-/g,"")
              }
            }
          },
          {
            type: "text",
            text: {
              content: ` – ${stats.hp.properties.HP.number} HP`
            }
          }
        ]
      },
      tallest: {
        id: "7dec4ee58bec4ea69463f272cfdb233f",
        val: [
          {
            type: "text",
            text: {
              content: `Tallest Pokémon: `
            }
          },
          {
            type: "mention",
            mention: {
              type: "page",
              page: {
                id: stats.height.id.replace(/-/g,"")
              }
            }
          },
          {
            type: "text",
            text: {
              content: ` – ${stats.height.properties.Height.number/10} m`
            }
          }
        ]
      },
      heaviest: {
        id: "78db44a9a6be4460bcc220b16798e554",
        val: [
          {
            type: "text",
            text: {
              content: `Heaviest Pokémon: `
            }
          },
          {
            type: "mention",
            mention: {
              type: "page",
              page: {
                id: stats.weight.id.replace(/-/g,"")
              }
            }
          },
          {
            type: "text",
            text: {
              content: ` – ${stats.weight.properties.Weight.number/10} kg`
            }
          }
        ]
      }
    }

    let blockArr = Object.values(blocks)

    console.log(blockArr)
    
    for (let block of blockArr) {
      const blockID = block.id
      const response = await notion.blocks.update({
        "block_id": blockID,
        "bulleted_list_item": {
          "rich_text": block.val
        }
      })
      console.log(response)
    }

  },
})