python-3.xpython-requestsrequestnotion-apinotion

Adding to a database creates a new database within page in notion


Here's my current code:

import json

import requests


def createPage(database_id, page_id, headers, url):
    newPageData = {
        "parent": {
            "database_id": database_id,
            "page_id": page_id,
        },
        "properties": {
            "Name": {"title": {"text": "HI THERE"}},
        },
    }

    data = json.dumps(newPageData)

    res = requests.request("POST", url, headers=headers, data=data)

    print(res.status_code)
    print(res.text)


database_id = "ea28de8e9cca4f62b4c4da3522869d03"
page_id = "697fd88570b3420aaa928fa28d0bf230"
url = "https://api.notion.com/v1/databases/"
key = "KEY"

payload = {}
headers = {
    "Authorization": f"Bearer {key}",
    "accept": "application/json",
    "Notion-Version": "2021-05-11",
    "content-type": "application/json",
}

createPage(database_id, page_id, headers, url)

But everytime I run this, it appears like I keep getting new databases within the page. This is before running the script: enter image description here

This is after running the script: enter image description here

I would like it to be like this after running the script: enter image description here

How can that be achieved?


Solution

  • It looks as you're calling the API URL that creates a new Database, and not the one that creates a new page. This URL: https://api.notion.com/v1/databases/ is for creating new databases, and not for creating pages.

    In order to create a new page within a database, use the following URL: https://api.notion.com/v1/pages

    Where you'll need to provide the previously created database id, among other identifiers

    More detailed documentation can be found here https://developers.notion.com/reference/post-page