Setting up the Notion API

  1. See Getting Started: Step 1 for how to create an Integration

  2. Get the NOTION_KEY

    1. Go to notion.so/my-integrations
    2. Click [Name of Integration]
    3. Copy Internal Integration Token
  3. Get the blockId

    1. Go to the page you want to share and copy the the link
    2. With url https://www.notion.so/odi-dev-zk/Notion-as-a-CMS-35f53c7f1b7c401ab0943c70d9051dd7 , 35f53c7f1b7c401ab0943c70d9051dd7is the blockID
  4. Create project, install notion client.

    mkdir notion-example  
    cd notion-example
    export NOTION_KEY=secret_***
    npm init #follow prompts
    npm i @notionhq/client
    touch index.js
    
  5. Add index.js

    index.js

    import { Client } from '@notionhq/client';
    
    const notion = new Client({ auth: process.env.NOTION_KEY });
    
    (async () => {
      // For these purposes the "block" is the page with all the content.
      const blockId = '[35f53c7f1b7c401ab0943c70d9051dd7](<https://www.notion.so/odi-dev-zk/Notion-as-a-CMS-35f53c7f1b7c401ab0943c70d9051dd7>)';
      const response = await notion.blocks.children.list({
        block_id: blockId,
        page_size: 50,
      });
      console.log(response);
    })();
    
  6. Run command

    # Output all first level block objects on the page to the console.
    node index.js
    

This page’s output:

{
  object: 'list',
  results: [
    {
      object: 'block',
      id: '04fc709a-cf30-411f-a4a2-ff61c5ed5bec',
      created_time: '2022-02-28T23:58:00.000Z',
      last_edited_time: '2022-02-28T23:58:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: false,
      archived: false,
      type: 'paragraph',
      paragraph: [Object]
    },
    {
      object: 'block',
      id: 'b162edf3-39ba-4ab9-852c-f348d750211a',
      created_time: '2022-02-28T23:58:00.000Z',
      last_edited_time: '2022-03-01T00:15:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: false,
      archived: false,
      type: 'numbered_list_item',
      numbered_list_item: [Object]
    },
    {
      object: 'block',
      id: '8e47e12a-207c-48cb-aad1-f68582fb67ea',
      created_time: '2022-03-01T00:00:00.000Z',
      last_edited_time: '2022-03-01T00:15:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: true,
      archived: false,
      type: 'numbered_list_item',
      numbered_list_item: [Object]
    },
    {
      object: 'block',
      id: 'bb7f4dde-38be-4f83-a75f-b7569b02fd30',
      created_time: '2022-03-01T00:07:00.000Z',
      last_edited_time: '2022-03-01T00:08:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: true,
      archived: false,
      type: 'numbered_list_item',
      numbered_list_item: [Object]
    },
    {
      object: 'block',
      id: '8c30ae4a-f7c7-4500-9504-fe207e3d8370',
      created_time: '2022-03-01T00:04:00.000Z',
      last_edited_time: '2022-03-01T00:17:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: true,
      archived: false,
      type: 'numbered_list_item',
      numbered_list_item: [Object]
    },
    {
      object: 'block',
      id: '235dd926-d79b-43b7-998b-b074bac2fa96',
      created_time: '2022-02-28T23:58:00.000Z',
      last_edited_time: '2022-03-01T00:17:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: true,
      archived: false,
      type: 'numbered_list_item',
      numbered_list_item: [Object]
    },
    {
      object: 'block',
      id: '05e6ed71-4a71-47a8-94b1-8794fd9c8773',
      created_time: '2022-03-01T00:16:00.000Z',
      last_edited_time: '2022-03-01T00:18:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: true,
      archived: false,
      type: 'numbered_list_item',
      numbered_list_item: [Object]
    },
    {
      object: 'block',
      id: '0debebf9-bfb5-42ff-8c2e-af61d2e3bb84',
      created_time: '2022-03-01T00:11:00.000Z',
      last_edited_time: '2022-03-01T00:11:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: false,
      archived: false,
      type: 'paragraph',
      paragraph: [Object]
    },
    {
      object: 'block',
      id: '545af654-6781-4a3d-8256-8f683c60fb34',
      created_time: '2022-03-01T00:14:00.000Z',
      last_edited_time: '2022-03-01T00:14:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: false,
      archived: false,
      type: 'paragraph',
      paragraph: [Object]
    },
    {
      object: 'block',
      id: 'f2146ee7-1354-4cf0-87c6-e5565a311900',
      created_time: '2022-03-01T00:14:00.000Z',
      last_edited_time: '2022-03-01T00:14:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: false,
      archived: false,
      type: 'paragraph',
      paragraph: [Object]
    },
    {
      object: 'block',
      id: '415a631a-07b9-48f5-a344-abd26039f64a',
      created_time: '2022-03-01T00:14:00.000Z',
      last_edited_time: '2022-03-01T00:14:00.000Z',
      created_by: [Object],
      last_edited_by: [Object],
      has_children: false,
      archived: false,
      type: 'paragraph',
      paragraph: [Object]
    }
  ],
  next_cursor: null,
  has_more: false
}

Summary

Notion API is designed as a simple backend structured data source. Since there's no simple way to access an entire page's contents, I don't think this viable solution for a rich-content backend like we had imagined.