Hey everyone, I’m having a weird issue with the Notion API. I can create pages just fine, but when I try to add info to a database, it’s not working. Here’s what’s happening: I can create a new page using the API with no problems, but adding a row to a database on that page returns a 404 error. It seems like the API can access the page but not the linked database, despite using the same credentials. I’ve simplified my code for adding data:
def add_to_database(token, db_id, data):
url = 'https://api.notion.com/v1/pages'
headers = {
'Authorization': f'Bearer {token}',
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json'
}
payload = {
'parent': {'database_id': db_id},
'properties': data
}
response = requests.post(url, json=payload, headers=headers)
return response.json()
result = add_to_database(my_token, my_db_id, my_data)
print(result)
Has anyone encountered this before and know what might be causing the error?
hey i’ve run into this before. make sure ur database ID is correct and the integration has access to it. sometimes notion’s weird about permissions. also double-check ur payload structure matches the database schema exactly. if that don’t work, try refreshing ur token. good luck!
I’ve encountered similar issues and resolved them by revisiting the environment setup. Sometimes the database ID might change or be misconfigured, so I always verify the ID by copying the link directly from the Notion database and extracting the correct string. It can also help to review the API version; an update might be necessary to avoid compatibility issues. In cases of a 404 error, I recommend performing a GET request to the database endpoint to ensure it’s accessible, which often clarifies if it’s a permissions or configuration problem.
I’ve dealt with this exact issue before. The problem likely lies in how you’re accessing the database. Instead of using the general pages endpoint, try using the specific database endpoint for adding items. Here’s a quick modification to your code that should work:
def add_to_database(token, db_id, data):
url = f'https://api.notion.com/v1/databases/{db_id}/query'
headers = {
'Authorization': f'Bearer {token}',
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json'
}
payload = {'properties': data}
response = requests.post(url, json=payload, headers=headers)
return response.json()
This approach directly queries the database, which should resolve the 404 error. Also, ensure your integration has the correct permissions for the specific database you’re trying to access. If issues persist, try regenerating your integration token.