Issue with Notion API database access after successful page creation

I’m working with the Notion API and running into a strange issue. I can successfully create pages using my integration, but when I try to add entries to a database on the same page, I get a 404 error saying the database isn’t found or shared with my integration.

Here’s my function for building new pages:

def build_notion_document(token, template_page_id, db_id):
    endpoint = "https://api.notion.com/v1/pages"
    request_headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json",
        "Notion-Version": "2022-06-28" 
    }
    payload = {
        "parent": {
            "type": "page_id",
            "page_id": template_page_id
        },
        "properties": {
            "title": [{
                "text": {
                    "content": "Study Cards Configuration"
                }
            }]
        },
        "children": [{
            "object": "block",
            "type": "paragraph",
            "paragraph": {
                "rich_text": [{
                    "type": "text",
                    "text": {
                        "content": "Keep these IDs secure to protect your study cards setup."
                    }
                }]
            }
        }]
    }
    result = requests.post(endpoint, headers=request_headers, json=payload)
    return result.status_code == 200

This works perfectly. But when I try to insert data into the database with this function:

def insert_database_entry(token, db_id, entry_properties):
    api_url = f"https://api.notion.com/v1/pages/"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json",
        "Notion-Version": "2022-06-28"
    }
    request_data = {
        "parent": {
            "type": "database_id",
            "database_id": db_id
        },
        "properties": entry_properties
    }
    response = requests.post(api_url, headers=headers, json=request_data)
    
    if response.status_code in [200, 201]:
        print("Entry created successfully!")
        return response.json()
    else:
        print(f"Failed to create entry: {response.text}")
        return response.json()

I get this error: {"object":"error","status":404,"code":"object_not_found","message":"Could not find database with ID: 583adfba-e506-43a8-b059-47f9c2d63a06. Make sure the relevant pages and databases are shared with your integration.","request_id":"4269100f-74d3-4b2a-8701-17b49ec9d43c"}

Since I can create the page successfully, shouldn’t the database on that same page be accessible to my integration as well? What am I missing here?

yeah this caught me off guard too when i started with notion api. even tho you created the page, the database inside needs seperate permissions. try going to your database view and explicitly add your integration under connections - its not automatic unfortunately

I ran into this exact problem a few months back and it had me scratching my head for hours. The core issue is that Notion treats databases as independent resources regardless of where they live. Even though your integration can create pages, it doesn’t inherit database permissions automatically. What worked for me was checking the database ID you’re using - make sure it’s correct first. Then you’ll need to share that specific database with your integration through the database settings. One thing that tripped me up initially was assuming that parent page permissions would cascade down, but Notion’s API doesn’t work that way. Each database maintains its own access control list. Also worth noting that if you’re creating the database programmatically as part of your page creation, you might need to wait a moment before the permissions fully propagate before attempting to write to it.

The issue you’re encountering is quite common with Notion integrations. Creating a page doesn’t automatically grant your integration access to databases within that page - these are separate permission entities in Notion’s API structure. When you create a page, the integration gets access to that specific page object, but any databases embedded within it require explicit sharing. To resolve this, you need to manually share the database with your integration through the Notion UI. Go to the database, click the three dots menu, select “Add connections” and choose your integration. Alternatively, you can share the entire parent page that contains the database with your integration, which should cascade permissions down to child databases. I’ve run into this exact scenario multiple times when building automated workflows. The key insight is that Notion’s permission model is granular - page creation permissions don’t automatically inherit to database manipulation permissions, even when they’re on the same page.