Open6

NotionAPIを試す

Ryo24Ryo24

todoの内容を変更する

https://developers.notion.com/changelog/retrieve-and-update-blocks-with-get-and-patch-v1blocksid

block_id

URL
https://www.notion.so/test-110f7ad437cf4b83893cfe238d040747#f810f2def1ec40f598c633cc4d88b887
block_id
f810f2def1ec40f598c633cc4d88b887

URLの末尾

update todo block

curl https://api.notion.com/v1/blocks/f810f2def1ec40f598c633cc4d88b887 \
  -H 'Authorization: Bearer 'API_KEY'' \
  -H "Content-Type: application/json" \
  -H "Notion-Version: 2021-05-13" \
  -X PATCH \
  --data '{
  "to_do": {
    "text": [{
      "text": { "content": "Lacinato kale" }
      }],
    "checked": false
  }
}'
Ryo24Ryo24

DBに要素を追加part1

page_id

ページ全体を共有した時のURL
https://treasure-actress-21c.notion.site/aef790e2fe5a48a8838f0e837d7eb8fc?v=4811bdb2872e439f9515bfb16750a6af
page_id
aef790e2fe5a48a8838f0e837d7eb8fc

notion.site/から?v=までの文字列。

DBにタイトルを追加

DBにタイトルを追加
curl -X POST https://api.notion.com/v1/pages \
  -H "Authorization: Bearer API_KEY" \
  -H "Content-Type: application/json" \
  -H "Notion-Version: 2021-05-13" \
  --data '{
    "parent": { "database_id": "aef790e2fe5a48a8838f0e837d7eb8fc" },
    "properties": {
      "Name": {
        "title": [
          {
            "text": {
              "content": "これであなたもNotion API"
            }
          }
        ]
      }
    }
  }'

参考文献

https://zenn.dev/nbr41to/articles/8b571eb03486b0787993

Ryo24Ryo24
curl -X POST https://api.notion.com/v1/pages \
  -H "Authorization: Bearer xxx" \
  -H "Content-Type: application/json" \
  -H "Notion-Version: 2021-05-13" \
  --data '{
    "parent": { "database_id": "aef790e2fe5a48a8838f0e837d7eb8fc" },
  "icon": {
       "type": "emoji", 
          "emoji": "🎉"
      },
    "properties": {
      "Name": {
        "title": [
          {
            "text": {
              "content": "Notion API"
            }
          }
        ]
      }
    }
  }'

Ryo24Ryo24

DBを作成する

page_id

ページ全体を共有
https://treasure-actress-21c.notion.site/test-c2870b80bcf7406499d685d3c7f21d1a
page_id
c2870b80bcf7406499d685d3c7f21d1a

真っ白なページを共有した時のURLは「...notion.site/+test(ページ名)+-+c2870b80bcf7406499d685d3c7f21d1a(page_id)」で構成されている。

DBを作成

DBを新規作成
curl --location --request POST 'https://api.notion.com/v1/databases/' \
--header 'Authorization: Bearer 'API_KEY'' \
--header 'Content-Type: application/json' \
--header 'Notion-Version: 2021-08-16' \
--data '{
    "parent": {
        "type": "page_id",
        "page_id": "c2870b80bcf7406499d685d3c7f21d1a"
    },
    "title": [
        {
            "type": "text",
            "text": {
                "content": "Grocery List",
                "link": null
            }
        }
    ],
    "properties": {
        "Name": {
            "title": {}
        },
        "Description": {
            "rich_text": {}
        },
        "In stock": {
            "checkbox": {}
        },
        "Food group": {
            "select": {
                "options": [
                    {
                        "name": "🥦Vegetable",
                        "color": "green"
                    },
                    {
                        "name": "🍎Fruit",
                        "color": "red"
                    },
                    {
                        "name": "💪Protein",
                        "color": "yellow"
                    }
                ]
            }
        },
        "Price": {
            "number": {
                "format": "dollar"
            }
        },
        "Last ordered": {
            "date": {}
        },
        "Store availability": {
            "type": "multi_select",
            "multi_select": {
                "options": [
                    {
                        "name": "Duc Loi Market",
                        "color": "blue"
                    },
                    {
                        "name": "Rainbow Grocery",
                        "color": "gray"
                    },
                    {
                        "name": "Nijiya Market",
                        "color": "purple"
                    },
                    {
                        "name": "Gus'\''s Community Market",
                        "color": "yellow"
                    }
                ]
            }
        },
        "+1": {
            "people": {}
        },
        "Photo": {
            "files": {}
        }
    }
}'

実行結果


参考文献

https://developers.notion.com/reference/create-a-database

Ryo24Ryo24

https://developers.notion.com/docs/working-with-page-content

curl -X POST https://api.notion.com/v1/pages \
  -H 'Authorization: Bearer '"$NOTION_API_KEY"'' \
  -H "Content-Type: application/json" \
  -H "Notion-Version: 2022-02-22" \
  --data '{
    "parent": { "database_id": "494c87d0-72c4-4cf6-960f-55f8427f7692" },
    "properties": {
        "title": {
      "title": [{ "type": "text", "text": { "content": "A note from your pals at Notion" } }]
        }
    },
    "children": [
    {
      "object": "block",
      "type": "paragraph",
      "paragraph": {
        "rich_text": [{ "type": "text", "text": { "content": "You made this page using the Notion API. Pretty cool, huh? We hope you enjoy building with us." } }]
      }
    }
  ]
}'

既存のDBにPage (レコード)を追加