11
1

More than 3 years have passed since last update.

Notion の APIライブラリ「notion_ruby」の使い方

Last updated at Posted at 2021-07-12

Notion APIが公開されたので非公式のRubyクライアント「notion_ruby」をオープンソースで公開しました。

notion_ruby

ソースコードは公開しているので Issue / Pull Request をお待ちしております。

基本的な使い方

ライブラリのインストール

Bundler を使っている場合は Gemfile に追加して bundle install します

gem 'notion_ruby'

使っていない場合は gem install notion_ruby します。

Notion の Tokenを発行する

My Integrations からTokenを作成します

notion_my_integrations.png

作成すると Token を取得できるのでメモしておきます。

notion_secrets.png

Token を使って以下のようにクライアントを作成します。

notion = NotionRuby.new({ access_token: '作成したToken' }) 

使いたいページ/Databaseを API から呼び出せるようにする

Token だけでは Notion のページを操作することができないので、Share から作成した Integration を招待します。

notion_share.png

これでライブラリを使う準備が整いました。

Notionから情報を取得する

GitHub の README にAPIの呼び出しの方法が説明されています。
例えばDatabaseの取得であれば以下のようにできます。

notion = NotionRuby.new({ access_token: '作成したToken' }) 
databases = notion.databases # => Notionのデータベースを一覧で取得する

取得したオブジェクトの属性にアクセスする

notion_rubyで取得したデータはHashのようにアクセスできます。

databases["object"] # => "list"

FaradayMiddleware::Mashify を利用すれば取得したデータをオブジェクトのように扱うことができます

notion = NotionRuby.new(access_token: NOTION_TOKEN) do |builder|
  builder.use FaradayMiddleware::Mashify
end
databases = notion.databases
databases.object # => "list"

最後に

普段から Notion を使っており何か貢献できればいいなと思いRubyのAPIクライアントを作りました。
Notionでの作業の自動化をしたいという方は利用してみてください

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