Notion API
Start building with the Notion API
開始之前
Step 1: 建立整合
到 https://www.notion.com/my-integrations
點擊 + New integration
或 + Create new integration
設定integration 完成後點擊Submit
看到這個畫面 表示密鑰成功產生了
Step 2: 工作區和整合連接
點擊工作區右上角的 •••
滑動到Add connections 輸入建立的Integration名稱並選取
開始用API
URL格式
沒有資料庫 <https://www.notion.so/{workspace_name}-{page_id}>
有資料庫 <https://www.notion.so/{database_id}?v={view_id}>
新增Block
POST
<https://api.notion.com/v1/blocks/{page_id}/children>
Key Value Authorization Integration的密鑰 Notion-Version 2022-06-28
{
"children": [
{
"object": "block", // 物件類別
"type": "heading_2", // 物件型態
"heading_2": { // 物件屬性
"rich_text": [{ "type": "text", "text": { "content": "Lacinato kale" } }]
}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {
"content": "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
"link": { "url": "<https://en.wikipedia.org/wiki/Lacinato_kale>" }
}
}
]
}
}
]
}
<aside> 💡 type值可為: "paragraph"
, "heading_1"
, "heading_2"
, "heading_3"
, "bulleted_list_item"
, "numbered_list_item"
, "to_do"
, "toggle"
, "child_page"
,"child_database"
, "embed"
, "image"
, "video"
, "file"
, "pdf"
, "bookmark"
, "callout"
, "quote"
, "equation"
, "divider"
, "table_of_contents"
, "column"
, "column_list"
, "link_preview"
, "synced_block"
, "template"
, "link_to_page"
, "table"
, "table_row"
, "unsupported"
詳閱:https://developers.notion.com/reference/block
</aside>
新增Database
POST
<https://api.notion.com/v1/databases>
Key Value Authorization Integration的密鑰 Notion-Version 2022-06-28
{
"parent": {
"type": "page_id",
"page_id": "{{page_id}}"
},
"is_inline": true,
"title": [ // 定義資料庫Title
{
"text": {
"content": "EJ's code"
}
}
],
"properties": { // 定義資料庫欄位
"Title": { // 欄位名稱
"title": {} // 欄位屬性
},
"Difficulty": { // 欄位名稱
"select": { // 欄位屬性
"options": [
{
"name": "Easy",
"color": "green"
},
{
"name": "Medium",
"color": "yellow"
},
{
"name": "Hard",
"color": "red"
}
]
}
},
"EJ's date": { // 欄位名稱
"date": {} // 欄位屬性
},
"User1's date": {
"date": {}
},
"User2's date": {
"date": {}
},
"Completed": {
"people": {}
},
"Link": {
"url": {}
}
}
}
新增Page
POST
<https://api.notion.com/v1/pages>
Key Value Authorization Integration的密鑰 Notion-Version Notion-Version
{
"parent": {
"database_id": "{database_id}"
},
"properties": { // 定義Page屬性
"Title": {
"title": [
{
"text": {
"content": "TEST"
}
}
]
},
"Difficulty": {
"multi_select": [
{
"name": "TEST"
}
]
},
"Link": {
"url": "TEST"
}
},
"children": [ // 定義Page內容
{
"object": "block",
"type": "heading_2",
"heading_2": {
"rich_text": [{ "type": "text", "text": { "content": "Lacinato kale" } }]
}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {
"content": "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
"link": { "url": "<https://en.wikipedia.org/wiki/Lacinato_kale>" }
}
}
]
}
}
]
}
更新Page
PATCH
<https://api.notion.com/v1/pages/{page_id}>
Key Value Authorization Integration的密鑰 Notion-Version 2022-06-28
{
"properties": {
"EJ's date": {
"date": {
"start": "2023-02-05"
}
},
"Completed": {
"people": [
{
"id": "1a1e8d57-a450-4dcf-abdb-e6384415bd37",
"name": "EJ",
"type": "person",
"person": {
"email": "k0970133227@gmail.com"
}
}
]
}
}
}
EJ’s code