GET と PATCH メソッドでブロック取得・更新する

開示?
開示?
原題
Retrieve and update blocks with GET and PATCH /v1/blocks/:id
日付
Aug 6, 2021
Number
14
前のリリース日(最新)
リリース間隔(日)
作成日
Nov 10, 2021 02:27 AM
タグ
ADDED
経過日数
Notion API でブロックが取得・更新できるようになった。PATCHコマンドで更新できるのは、現在、paragraph, heading_1,  heading_2,  heading_3,  bulleted_list_item,  numbered_list_item,  toggle ,  to_do のブロックとのこと。
 
テスト用に以下のブロックを用意した。ここには箇条書きがあり、「変更前のブロック」という文字が書いておいた。このブロックの URL を取得すると`https://www.notion.so/hkob/GET-PATCH-132b1776fa484432acc67b50ae6c0ef3#bb78a5a99a964fbebf5a6655a10a6516`となる。後ろのbb78から始まる部分がブロック id になるので、これを使って検証する。
  • 変更後のブロック

ブロックの取得

 Retrieve a Block 機能を使うと、 ブロックオブジェクトが取得できる。
#!/bin/sh curl 'https://api.notion.com/v1/blocks/bb78a5a99a964fbebf5a6655a10a6516' \ -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \ -H 'Notion-Version: 2021-08-16'
取得したブロックはこちら。箇条書きなので「bulleted_list_item」になっていることがわかる。
{ "object": "block", "id": "bb78a5a9-9a96-4fbe-bf5a-6655a10a6516", "created_time": "2021-11-10T03:11:00.000Z", "last_edited_time": "2021-11-10T03:11:00.000Z", "has_children": false, "archived": false, "type": "bulleted_list_item", "bulleted_list_item": { "text": [ { "type": "text", "text": { "content": "変更前のブロック", "link": null }, "annotations": { "bold": false, "italic": false, "strikethrough": false, "underline": false, "code": false, "color": "default" }, "plain_text": "変更前のブロック", "href": null } ] } }
 

ブロックの更新

新しい PATCH /v1/blocks/:id を使うと、更新した ブロックオブジェクト が返却される。詳細については、こちらに記載されている。→  Update a Block
文字だけの変更だけだと面白くないので、太字にして文字色を赤にしてみた。
#!/bin/sh curl https://api.notion.com/v1/blocks/bb78a5a99a964fbebf5a6655a10a6516\ -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \ -H "Content-Type: application/json" \ -H "Notion-Version: 2021-08-16" \ -X PATCH \ --data '{ "bulleted_list_item": { "text": [{ "text": { "content": "変更後のブロック" }, "annotations": { "bold": true, "color": "red" } }] } }'
返却された JSON データをみると、確かに内容が変更されているのがわかる。上に用意したブロックもちゃんと太字になり、文字色も赤く変わっている。
{ "object": "block", "id": "bb78a5a9-9a96-4fbe-bf5a-6655a10a6516", "created_time": "2021-11-10T03:11:00.000Z", "last_edited_time": "2021-11-10T03:34:00.000Z", "has_children": false, "archived": false, "type": "bulleted_list_item", "bulleted_list_item": { "text": [ { "type": "text", "text": { "content": "変更後のブロック", "link": null }, "annotations": { "bold": true, "italic": false, "strikethrough": false, "underline": false, "code": false, "color": "red" }, "plain_text": "変更後のブロック", "href": null } ] } }

🖇️
Notion API Changelog まとめ