How to retrieve child database IDs from a Notion page using API

I need help finding the database ID of a child database that’s embedded within a Notion page.

I’ve been using the Notion API to fetch the parent page data with a GET request. Here’s my current approach:

import requests
import json

# My Notion API token
TOKEN = "secret_ApiToken_123"

# Target page endpoint
api_endpoint = "https://api.notion.com/v1/pages/main_page_id_here"

# Request headers
request_headers = {
    "Authorization": f"Bearer {TOKEN}",
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28",
}

# Execute the request
api_response = requests.get(api_endpoint, headers=request_headers)

# Process response data
if api_response.status_code == 200:
    page_data = api_response.json()
    with open("page_output.json", "w") as output_file:
        json.dump(page_data, output_file)
else:
    print(f"Request failed with status: {api_response.status_code}")

The response I get looks like this:

{
    "object": "page",
    "id": "main_page_id",
    "created_time": "2023-09-14T02:02:00.000Z",
    "last_edited_time": "2023-09-14T10:53:00.000Z",
    "created_by": {
        "object": "user",
        "id": "user_id_here"
    },
    "last_edited_by": {
        "object": "user",
        "id": "editor_id"
    },
    "cover": null,
    "icon": { "type": "emoji", "emoji": "🔬" },
    "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-main_page_id",
    "public_url": null
}

I don’t see any field that contains information about child databases or blocks. Is there a different API endpoint or method I should use to access the embedded database IDs within a page?

ur just gettin the page data, not the contents. try the blocks endpoint: /v1/blocks/{page_id}/children to fetch all child blocks, including any nested dbs. check for blocks with type “child_database” for the id u need.

The page endpoint only gives you metadata, not the actual content. You’ll need to hit the blocks children endpoint separately: https://api.notion.com/v1/blocks/{page_id}/children. This returns all child blocks, including embedded databases. Look for blocks where type equals “child_database” - that’s where you’ll find the database ID. If there are lots of blocks, you might need to paginate through results using next_cursor.

You’re hitting the wrong API endpoint. The pages endpoint only grabs page properties and metadata - that’s why you’re not seeing any child database info. You need to query the page’s block children using the blocks API instead. The blocks endpoint returns an array of all child blocks in that page. Each block has a type field, and when you find a block with type “child_database”, that block object contains the database ID you need. Just heads up - if your page has tons of blocks, the response gets paginated. You’ll need multiple requests using the has_more and next_cursor fields to grab everything.