0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NotionのコンテンツをAPI経由で更新する

Posted at

前回のC#のEntityからER図作ってみたの続きです。
せっかくC#のコードをパースしてER図を作ったので、どうせなら全工程自動で公開したいと思いNotionAPIを調べてみました。
今回は出力したER図(Mermaid記法)をNotionにAPI経由で公開するための下調べです。

Notion APIについて

調査内容

  • NotionAPIを扱うにはインテグレーション設定を追加しアクセストークンを取得する必要があり。
  • NothionでER図を扱うにはページにコードブロックを追加しコードタイプをMermaidにする。
  • ページやブロックには固有のIDがありどうやらAPIではこのIDを指定して色々操作が可能なよう。
  • 今回はあらかじめ作成したブロックをAPIで更新する方針とする。

手順

  1. Notionのインテグレーション追加を行い、アクセストークンを取得する。
    ※ここは色々なページで語られているので省略

  2. ER図用のページを作成しMermaid用のコードブロックを追加する。
    image.png

  3. ER図用のブロックのIDを取得する。
    ※コードブロックを選択し右上のメニューから”ブロックへのリンク”をクリックします。
    ※#以降がidになります。
    image.png

  4. ページをAPIからアクセス可能にする。
    ※右上のページメニューからコネクトの追加を選択し1で追加したAPIを追加します。
    image.png

  5. Notion APIでER図を更新する。
    ※アクセストークンは手順1で取得した値
    ※ブロックIDは手順3で取得した値
    ※JsonのcontentがMermaid記法のMarkdown
    ※HTTPメソッドは更新なのでPATCH

    $ curl -X PATCH 'https://api.notion.com/v1/blocks/<ブロックID>' \
      -H 'Authorization: Bearer <アクセストークン>' \
      -H 'Content-Type: application/json' \
      -H 'Notion-Version: 2022-06-28' \
      --data '{
        "code": {
          "rich_text": [
            {
              "text": {
                "content": "erDiagram Company{int ID \"ID\" string Name \"名前\"}"
              }
            }   
          ]
        }
      }'
    
  6. 更新を確認する。
    image.png

最後に

コードブロックの更新は比較的簡単にできそうです。
次回は前回作成したプログラムを拡張してC#のプログラムからNotionの更新までやりたいと思います。
SIerさん、納品物の自動作成で楽しません?
(お仕事まってます!)

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?