LoginSignup
2
1

More than 1 year has passed since last update.

NotionのAPIにNode.jsからアクセスしてみるメモ #Notion

Posted at

Notionを最近触っていますが、APIおよびJavaScript SDKがある模様なので触ってみました。

インテグレーションを作成する

https://www.notion.so/my-integrations
スクリーンショット 2022-08-29 17.55.05.png

名前などを適当に入れて進めてみます。試しに、Node.jsテストとしておきました。

スクリーンショット 2022-08-29 17.55.41.png

トークンが発行されました。

スクリーンショット 2022-08-29 17.56.05.png

Notionでデータベースを作っておき、Shareからユーザーを招待的な操作をします。
このときに作ったNode.jsテストなどの候補が出てくるので追加しましょう。

スクリーンショット 2022-08-29 18.10.06.png

Google系のAPI利用をするときのサービスアカウントに近いイメージな気がします。

SDK経由でNode.jsから触ってみる

npmに書いてある手順でSDKを触ってみます。

https://www.npmjs.com/package/@notionhq/client
https://github.com/makenotion/notion-sdk-js

まずはインストールから

$ npm install @notionhq/client

以下のコードのトークン部分を先ほど取得したトークンに書き換えます。

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

// Initializing a client
const main = async () => {
    const notion = new Client({
        auth: `トークンを記載`,
    })
      
    const listUsersResponse = await notion.users.list({})
    console.log(listUsersResponse)
}

main();

実行してみます。

$ node app.js
{
  object: 'list',
  results: [
    {
      object: 'user',
      id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      name: 'のびすけ 菅原',
      avatar_url: 'https://lh3.googleusercontent.com/a-/xxxxxxxxxxxxxxxxx',
      type: 'person',
      person: [Object]
    },

・
・
・省略

Notionに登録しているユーザー情報が取得できました!

notion.users.list({})のメソッドがそうみたいですね。

データベースの値の取得

URLのこの辺りがデータベースIDらしいですね。

https://www.notion.so/myworkspace/a8aec43384f447ed84390e8e42c2e089?v=...
                                  |--------- Database ID --------|

https://developers.notion.com/docs

こんな感じに雑に作っていたテーブル(データベース)を利用します。

スクリーンショット 2022-08-29 18.19.21.png

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

//自身の利用するデータベースID
const databaseId = '13d4d5bcxxxxxxxxxxxxxxxxxxx'; //間にハイフンがなくて大丈夫

// Initializing a client
const main = async () => {
    const notion = new Client({
		auth: `トークンを記載`,
    })
      
    const response = await notion.databases.retrieve({ database_id: databaseId });
    console.log(response); 
}

main();

実行してみます。

$ node app.js
{
  object: 'database',
  id: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
  cover: null,
  icon: null,
  created_time: '2022-08-27T02:02:00.000Z',
  created_by: { object: 'user', id: 'yyyyyyyyyyyyyyyyyyyyy' },
  last_edited_by: { object: 'user', id: 'yyyyyyyyyyyyyyyyyyyyy' },
  last_edited_time: '2022-08-29T09:11:00.000Z',
  title: [
    {
      type: 'text',
      text: [Object],
      annotations: [Object],
      plain_text: 'obnizデータ',
      href: null
    }
  ],
  description: [],
  is_inline: true,
  properties: {
    Tags: {
      id: 'wQwD',
      name: 'Tags',
      type: 'multi_select',
      multi_select: [Object]
    },
    Name: { id: 'title', name: 'Name', type: 'title', title: {} }
  },
  parent: { type: 'page_id', page_id: 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' },
  url: 'https://www.notion.so/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  archived: false
}

情報が取れました。

使ったのはRetrieve a databaseの項目です。

https://api.notion.com/v1/databases/{database_id}
https://developers.notion.com/reference/retrieve-a-database

所感

インテグレーション追加のところと、 **データベースIDってどこで調べるんだ?**ってところが少しはじめ戸惑った印象です。

慣れてしまえばこんなもんだよなって話なのですが(笑)

APIドキュメントもちゃんと整備されていて色々とやれそうですね。
他のメソッドも触って何か連携させてみたいですね。

2
1
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
2
1