Update page relation

Tags: #notion #update #page #relation #requests #api
Author: Florent Ravenel
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.


Import libraries

import requests
import naas
from pprint import pprint

Setup Variables

  • notion_token: Notion token shared with your database
  • page_id: Notion page URL or ID
  • relation_property_name: Relation property name in your page
  • relation_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 = ""
relation_property_name = "<relation_property_name>"
relation_id = ""


Get page

# Get page ID
page_id = page_id.split("?")[0].split("-")[-1]
# Get properties from page
url = f"{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")


Update page relation

# 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)