How To Use The Notion API For A Low-Code CMS In 5 Minutes (2023)

Notion is a popular all-in-one workspace that allows users to organize, collaborate, and manage their work across multiple devices. And since 2021, you can interact programmatically with Notion using their public API. The Notion API provides a powerful platform for building custom applications that can read and write data to a user's Notion workspace.

In this article, we will explore how to use the Notion API to build a low-code content management system from Rowy, in just 5 minutes.

1. Notion API key setup

It only takes a minute to get an API key:

  1. Navigate to https://www.notion.com/my-integrations
  2. Add a new integration
  3. Copy the Internal Integration Token (the API key)

0.webp

Once you have your API key, you only need your root page ID to get started. You can find your root page ID by navigating to your Notion workspace and copying the ID from the URL. For example, if your workspace is called "YOURWORKSPACENAME", your workspace URL will be in the form "https://www.notion.so/YOURWORKSPACENAME-id" and your root page ID is just the last part named "id".

2. Creating A Notion Page

We'll use Rowy to create a new page in Notion, programmatically. Rowy is a low-code backend development platform that us to build custom applications without having to worry about server configurations. Rowy's user interface is designed to be intuitive―it's just like using a spreadsheet. For this part, we'll use a column containing our page title, another containing the page's content in Markdown, the Notion page ID, and the parent page's ID:

1.webp

In this example, the parent page is the root page of our Notion workspace. We can then create an Action column to publish the page to Notion using the Notion API:

2.webp

First, we send a request to the endpoint POST /v1/pages to create the page, and then the endpoint PATCH /v1/blocks/{block_id}/children to append our Markdown content as Notion blocks. You can use a library like markedjs to parse the Markdown as a syntax tree and then convert it to Notion blocks.

Finally, our Notion page is live:

3.webp

This is useful for creating a blog, a documentation site, or a knowledge base, among other things. Notion's text editor is powerful, but it's not ideal for all use cases. From experience, block editors aren't great to copy/paste content, and writing code in Notion isn't optimal. Instead, you can use Rowy to keep things in Markdown, process images programmatically, and then publish to Notion to leverage their sharing feature.

3. Retrieving A Notion Page

You can retrieve Notion page properties using the endpoint GET /v1/pages/{page_id}:

const page = await fetch(`https://api.notion.com/v1/pages/${rowy.notionId}`, {
    headers: {
        'Content-Type': 'application/json',
        'Notion-Version': '2022-06-28',
        'Authorization': `Bearer ${api_key}`
    }
}).then(res => res.json())

console.log(page.properties.title)

To get the content of the page, you need to request the block children endpoint:

const res = await fetch(`https://api.notion.com/v1/blocks/${blockId}/children?page_size=100`, {
    headers: {
        'Content-Type': 'application/json',
        'Notion-Version': '2022-06-28',
        'Authorization': `Bearer ${api_key}`
    }
}).then(res => res.json())

console.log(res.results)

4. Creating & Syncing A Notion Database

It's interesting to note that Notion also has its own database system. You can use it to structure data and pages in a way that's easy to query and filter. Combined with Rowy, you can integrate data from any third-party tool using this feature, and publish it in a Notion page. For example, you can use Rowy to create a Notion database of your GitHub issues to have them published automatically in a Notion page.

Similar to the Pages API, creating a database is as simple as sending a request to the endpoint POST /v1/databases. You just need to specify a parent page and the new database will automatically be appended to it.

You might also want to let colleagues edit the database. Notion doesn't provide a webhook system to notify you when a database is updated, but you can use Rowy webhooks to run calls to the GET /v1/databases/{database_id}/query endpoint to periodically check for changes via scheduled job and sync your Rowy database accordingly.

Join Us On Discord

Notion's API makes it an underrated CMS solution you can hook up with any software tool to publish content for free. Whether it's databases or pages, you can use the Notion API to keep your team wiki updated at all times.

If you liked this article, you'll probably enjoy our Discord community as well. We help each other make things with less code, and we're always happy to welcome new members!

Get started with Rowy in minutes

Continue reading

Browse all