Modifying Notion database entries with API: How to update field values?

I’m working on a project where I need to change values in a Notion database using their API. Right now, I can only change the column names (header row) but not the actual data in the cells. When I fetch a database by its ID, I only get the column names, not the content.

Here’s what I’ve tried so far:

update_payload = {
    "properties": {
        "Task Number": {"name": "Updated Task Number"},
    },
}

api_headers = {
    "Authorization": "Bearer " + api_key,
    "Accept": "application/json",
    "Notion-Version": "2022-02-22",
    "Content-Type": "application/json"
}

def modify_db():
    result = requests.patch(db_url, json=update_payload, headers=api_headers)
    print(result.status_code)
    print(result.text)

This code only updates the column name. How can I fetch and update the actual data inside the database cells? Any help would be appreciated!

yo alex, i get ur struggle. to change the actual data, u gotta target individual pages in the db. use the /pages endpoint with the page ID for each row. ur payload should look like this:

update_payload = {
    'properties': {
        'Task Number': {'number': 123},
        'Status': {'select': {'name': 'Done'}},
    }
}

then use requests.patch() with the page URL. that should do it!

hey alex, i’ve had similar issues. to update cell values, you need to target specific pages (rows) in the db. try using the /pages endpoint instead of /databases. you’ll need the page ID for each row you wanna update. then use the properties object to change values. hope this helps!

To modify the actual data in your Notion database, you’ll need to approach it differently. Instead of updating the database structure, focus on individual pages within the database. Here’s a general approach:

  1. Retrieve the page IDs for the rows you want to update.
  2. Use the /pages/{page_id} endpoint to update specific pages.
  3. In your update payload, specify the property values you want to change.

Your update_payload should look something like this:

update_payload = {
    'properties': {
        'Task Number': {'number': 42},
        'Status': {'select': {'name': 'In Progress'}},
        # Add other properties as needed
    }
}

Then use requests.patch() with the page-specific URL. This method should allow you to update the actual cell values in your Notion database.

Modifying the actual data in a Notion database requires you to update individual rows rather than altering the database structure. You need to retrieve the specific page IDs for each row you want to change and then send a patch request to the corresponding page endpoint. In my experience, it is important to structure the payload correctly by matching the property types, such as using ‘number’ for numerical values or ‘select’ for choice fields. This approach also means you should be prepared to handle any API rate limits or errors that might occur.

Hey Alex, I’ve dealt with this exact issue before. Here’s what worked for me:

Instead of modifying the database structure, you need to update individual pages (rows) within the database. First, fetch all the pages in your database using the /databases/{database_id}/query endpoint, which will give you the page IDs.

Then, for each page you want to update, use the /pages/{page_id} endpoint with a PATCH request. Your payload should look something like this:

update_payload = {
    'properties': {
        'Task Number': {'number': 5},
        'Status': {'select': {'name': 'In Progress'}},
        'Description': {'rich_text': [{'text': {'content': 'Updated description'}}]}
    }
}

Make sure to match the property types exactly as they appear in your database. Also, be mindful of API rate limits; you might need to add delays if you’re updating many pages at once.

Hope this helps! Let me know if you need further clarification.