<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 and get the database row whose Age property is the highest.

This example does not require relating each database entry to a common “aggregator” row in order to create a Rollup displaying the highest value. Instead, JavaScript is used to find the highest number value.

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>

👇 The text block below will dynamically link to the oldest member listed in the Ages database.

The oldest member is Brooke, who is 89 years old.

Ages

Video Tutorial

https://www.loom.com/share/2ae48badcba54841b70bc74cdbfa3dbb

Pipedream Code

Pipedream steps:

import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    notion: {
      type: "app",
      app: "notion",
    }
  },
  async run({steps, $}) {
      
      /* Create a variable with the ID of the database to be queried */
      const dbID = "9fb56470678345bf972fa4188c3b9414"
      
      /* Call the Notion API and query the database.
      *  Note that in this example, we are not paginating, so we'll get 100 rows max.
      */
      const response = await axios($, {
        method: 'POST',
        url: `https://api.notion.com/v1/databases/${dbID}/query`,
        headers: {
          Authorization: `Bearer ${this.notion.$auth.oauth_access_token}`,
          "Notion-Version": `2021-08-16`,
        },
        data: {page_size: 100}
      }).then((resp) => {
        const results = resp.results
        return results
      }).catch((error) => {
        console.log(error)
      })

      /* Map through the array of database entries and find the max value of the Age
      *  property values. Store that raw number in the highestNum variable.
      */
      const highestNum = Math.max(...response.map(o => o.properties.Age.number))

      /* Search through the array of database entries and return the element for which
      *  the Age property number matches the highest Num variable.
      */
      const maxRow = response.find((number) => {
        return number.properties.Age.number == highestNum
      })

      /* Define a variable with the block ID for the text block that we will update
      *  to report the database row with the highest number.
      */
      const block_id = "6346efb4372c41e99e64151e406b367b"

      /* Remove the dash characters from the page ID of maxRow so we can construct
      *  a valid URL out of it.
      */
      const page_id = maxRow.id.replace(/-/g,"")
      console.log(page_id)

      /* Call the Notion API once more, using the Update a Block endpoint to entirely
      *  replace the contents of the specified block_id with a sentence reporting on the
      *  member who has the highest age.
      */
      const sender = await axios($, {
        method: 'PATCH',
        url: `https://api.notion.com/v1/blocks/${block_id}`,
        headers: {
          Authorization: `Bearer ${this.notion.$auth.oauth_access_token}`,
          "Notion-Version": `2021-08-16`,
        },
        data: {
          paragraph: {
            rich_text: [
              {
                type: "text",
                text: {
                  content: `The oldest member is `
                }
              },
              {
                type: "mention",
                mention: {
                  type: "page",
                  page: {
                    id: page_id
                  }
                }
              },
              {
                type: "text",
                text: {
                  content: `, who is ${highestNum} years old.`
                }
              }
            ]
          }
        }
      }).then((resp) => {
        console.log(resp)
        return resp
      }).catch((error) => {
        console.log(error)
      })

      return sender

  },
})

Learn the Notion API

To learn the basics of the Notion API – and learn some beginner/intermediate JavaScript code – I highly recommend going through my free Notion API crash course:

The Complete Notion API Crash Course for Beginners