Hey everyone, I’m stuck with a problem while trying to add a new page to my Notion database using their API. I keep getting this weird error about page_id
even though I’m not using it.
Here’s a simplified version of what I’m trying:
payload = {
"parent": {"database_id": "my_database_id"},
"properties": {
"Title": {"title": [{"text": {"content": "New Task"}}]},
"Status": {"select": {"name": "To Do"}},
"Due": {"date": {"start": "2023-05-20"}}
}
}
response = requests.post("https://api.notion.com/v1/pages", json=payload, headers=my_headers)
print(response.json())
But I’m getting this error:
{
"object": "error",
"status": 400,
"code": "validation_error",
"message": "body failed validation: body.parent.page_id should be not present or a string, instead was null."
}
I’ve double-checked the Notion docs, and they say we should use either database_id
or page_id
in the parent field. I’m using database_id
, so I’m not sure why it’s complaining about page_id
.
Has anyone run into this before? Any ideas on what I might be doing wrong? Thanks in advance for any help!
hey mikezhang, sounds like a tricky one! have you tried removing the ‘parent’ key and using ‘database_id’ directly? for example:
payload = {
"database_id": "my_database_id",
"properties": {
# ... rest of your properties
}
}
give it a go and see if that clears the error!
I’ve encountered a similar issue before, and it turned out to be related to the API version I was using. Make sure you’re using the latest version of the Notion API (2022-06-28 as of now) in your request headers. You can set it like this:
headers = {
'Authorization': 'Bearer YOUR_INTEGRATION_TOKEN',
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json'
}
If that doesn’t solve it, double-check your database ID. Sometimes, copying from Notion’s interface can include extra characters. Try manually typing out the ID or using the .strip() method to remove any potential whitespace.
Lastly, ensure your integration has the necessary permissions to create pages in the database. You can check this in the Notion integrations settings page.
Let me know if any of these suggestions help!
I’ve run into this issue before, and it’s a bit of a quirk with the Notion API. The error message is misleading, as it’s not actually about the ‘page_id’ at all. The problem is likely with the structure of your ‘parent’ object.
Try modifying your payload like this:
payload = {
'parent': {'database_id': 'my_database_id'},
'properties': {
'Title': {'title': [{'text': {'content': 'New Task'}}]},
'Status': {'select': {'name': 'To Do'}},
'Due': {'date': {'start': '2023-05-20'}}
}
}
Notice the single quotes instead of double quotes. Sometimes, Python’s JSON handling can be finicky with nested objects. If this doesn’t work, you might want to use the json module to ensure proper serialization:
import json
payload = json.dumps({
'parent': {'database_id': 'my_database_id'},
# ... rest of your payload
})
response = requests.post('https://api.notion.com/v1/pages', data=payload, headers=my_headers)
This approach should resolve the issue. Let me know if you need further assistance!