LoginSignup
1
0

More than 1 year has passed since last update.

Notion APIでページの中身を取得してみるメモ #notion

Last updated at Posted at 2022-09-05

前回書いたNotion APIアクセスメモの続きです。

ページから情報を一括で取得できると嬉しかったのですが、ぱっと調べた感じだとそれっぽいものがなく、ページも一つのブロックとして捉えて、ブロックの中の要素を取得するみたいなやり方になるっぽい雰囲気でした。

ページID(BlockID)

ページIDといいつつブロックIDになるみたいです。

https://www.notion.so/xxxxxxxxxxxxxxxxxxxxxというURLの場合は最後のスラッシュ以下がページIDに

https://www.notion.so/API-xxxxxxxxxxxxxxxxxxなどアルファベットがURLに入ってる場合はハイフン以下がページIDになっている模様でした。

こんな感じでページタイトルに英数字が入ってるとその英数字がURLに反映される仕様っぽいですね。

スクリーンショット 2022-09-05 19.56.34.png

2段階でAPIアクセス

notion.blocks.children.list()でそのページの中の要素一覧とIDを取得して、otion.blocks.retrieve()でIDを指定して中身を取得するといった流れです。

const { Client } = require("@notionhq/client")

const notion = new Client({
    auth: ``,
})

const main = async () => {
    const pageId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    
    const response = await notion.blocks.children.list({
        block_id: pageId,
        page_size: 50,
    });
    console.log(response);

    const blockId2 = response.results[1].id;
    const response2 = await notion.blocks.retrieve({
        block_id: blockId2,
    });
    console.log(`----`)
    console.log(response2.paragraph);
}

main();

結果

//省略

{
  rich_text: [
    {
      type: 'text',
      text: [Object],
      annotations: [Object],
      plain_text: 'こんばんわ、よろしくお願いします。',
      href: null
    }
  ],
  color: 'default'
}

スクリーンショット 2022-09-05 19.56.34.png

無事にこんばんわ、よろしくお願いします。の文字が取得できています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0