Notion API, update a checkbox value in a database

By

Learn how to update a checkbox value in a Notion database with the API by calling notion.pages.update() with the page id and the new checkbox property value.

~~~

To update a checkbox value in a Notion database using the Notion API, you call notion.pages.update() passing the page id and the new value for the checkbox property.

This works because in a Notion database, each entry is considered to be a page. Updating a row in the database means updating that page’s properties.

So once you have that entry, let’s assume it to be the variable named page, you’ll have its id under page.id.

I’ll also assume you have the Notion client initialized:

import { Client } from '@notionhq/client'

//...

const notion = new Client({ auth: process.env.NOTION_API_KEY })

Getting the entry you want to update

If you don’t have the page object yet, you can query the database for it. Here I fetch all the entries where the “Ready” checkbox is still unchecked:

const response = await notion.databases.query({
  database_id: process.env.NOTION_DATABASE_ID,
  filter: {
    property: 'Ready',
    checkbox: {
      equals: false,
    },
  },
})

const page = response.results[0]

Updating the checkbox

Then you can do this to set the value of the checkbox named “Ready” to true:

await notion.pages.update({
  page_id: page.id,
  properties: {
    Ready: {
      checkbox: true,
    },
  },
})

Pass checkbox: false instead to uncheck it.

Notice that Ready is the name of the property as it appears in Notion, the column header of your database. It must match exactly, including the case. If your column is called “Done” and you write “done”, the API rejects the request with a validation error.

When the request fails

The most common error is object_not_found, telling you it could not find the page. Nine times out of ten this means the integration doesn’t have access to the database.

The fix: open the database in Notion, click the ... menu, go to the connections section and add your integration there. Access is not granted automatically when you create the integration, you have to connect it to each page or database you want it to touch.

The other one to watch for: the property in your update call must actually be a checkbox in the database schema. Sending checkbox: true to a property that’s a select or a text field also fails with a validation error.

Tagged: Tools · All topics
~~~

Related posts about tools: