pythongetnotion-api

Getting the ID of an inline child database in a parent page using the Notion API


I'm trying to get the database ID of an inline child database on a Notion page.

I tried retrieving the parent page using a GET method. (shown in the code below)

import requests, json

# Define my Notion API API_KEY
API_KEY = "secret_MyKey_:)"

# Define the Notion page URL
get_url = "https://api.notion.com/v1/pages/parent_page_id_:)"

# Set up headers for the API request
headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28",
}

# Making the API request
response = requests.get(page_url, headers=headers)

# Saving the results to a JSON file if the request is successful
if response.status_code == 200:
    data = response.json()
    with open("test.json", "w") as f:
        json.dump(data, f)
else:
    print(f"Failed to fetch child elements. Status code: {response.status_code}")

The resulting JSON file is shown below.

{
    "object": "page",
    "id": "page_id",
    "created_time": "2023-09-14T02:02:00.000Z",
    "last_edited_time": "2023-09-14T10:53:00.000Z",
    "created_by": {
        "object": "user",
        "id": "my_id_:)"
    },
    "last_edited_by": {
        "object": "user",
        "id": "me :)"
    },
    "cover": null,
    "icon": { "type": "emoji", "emoji": "\ud83d\udd2c" },
    "archived": false,
    "properties": {
        "title": {
            "id": "title",
            "type": "title",
            "title": [
                {
                    "type": "text",
                    "text": { "content": "Testing Zone", "link": null },
                    "annotations": {
                        "bold": false,
                        "italic": false,
                        "strikethrough": false,
                        "underline": false,
                        "code": false,
                        "color": "default"
                    },
                    "plain_text": "Testing Zone",
                    "href": null
                }
            ]
        }
    },
    "url": "https://www.notion.so/Testing-Zone-page_id",
    "public_url": null
}

Apparently, there is no key for child elements. I wonder whether there is another way to specifically access the child IDs of a page. I'd be delighted if you could help me! 😇


Solution

  • https://api.notion.com/v1/pages/<page_id>) - returns only properties of a page, not the page content. More on this here.

    https://api.notion.com/v1/blocks/<page_id>/children - Use this to retrive contents of the page, which will have the inline database ID as well. More on this here

    A page of a database, is also a block. Hence, we use the block-children API to retrieve children of the page(block).