Update page relation
Tags: #notion #update #page #relation #requests #api
Last update: 2023-04-24 (Created: 2023-04-24)
Description: This notebook will show how to update page relation using requests. It is usefull for organization to link different database in Notion and keep track of their data.
References:
import requests
import naas
from pprint import pprint
notion_token
: Notion token shared with your databasepage_id
: Notion page URL or IDrelation_property_name
: Relation property name in your pagerelation_id
: Page URL or ID to be linked to master page as relation
notion_token = naas.secret.get("NOTION_TOKEN") or "YOUR_TOKEN"
page_id = "https://www.notion.so/xxxxxxxxx/xxxxxxxxxxxxxx?pvs=4"
relation_property_name = "<relation_property_name>"
relation_id = "https://www.notion.so/xxxxxxxxx/xxxxxxxxxxxxxx?pvs=4"
# Get page ID
page_id = page_id.split("?")[0].split("-")[-1]
# Get properties from page
url = f"https://api.notion.com/v1/pages/{page_id}"
headers = {
'Authorization': f'Bearer {notion_token}',
"accept": "application/json",
"Notion-Version": "2022-06-28"
}
response = requests.get(url, headers=headers)
properties = response.json().get("properties")
pprint(properties)
# Get relation ID
relation_id = relation_id.split("?")[0].split("-")[-1]
# Update properties from page
properties[relation_property_name]["relation"] = [{'id': relation_id}]
res = requests.patch(url, headers=headers, json={"properties": properties})
if res.status_code == 200:
print("Relation updated for page:", page_id)
Last modified 1mo ago