Building a Table via the Notion API

Attempting to create a table block using the Notion API results in a validation error regarding undefined nested children. Below is an alternative example:

import requests

auth_headers = {
    'Authorization': f'Bearer {secret_val}',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Notion-Version': '2022-02-28'
}

payload = {
    'children': [
        {
            'object': 'block',
            'type': 'table',
            'table': {'columns_count': 4}
        }
    ]
}

response = requests.patch('https://api.notion.com/v1/blocks/abc123-def456/children', headers=auth_headers, json=payload)
print(response.text)

hey, maybe try addin explicit row data inside the table block block; notion sometimes expects nested cell and row details. check if passin full nested objects fixes the device. might be the key to avoid undefined child errors

Based on my own experimentation with the Notion API, I found that creating a table block requires more than simply specifying the columns count. The API expects a detailed structure that includes definitions for the rows and cells within the table. I had to simulate the full table layout, including nested objects for child rows that in turn contained cell definitions. Without this more verbose structure, the request would lead to similar validation errors. Checking the detailed block schema in the latest API documentation helped me resolve these issues.

I had a similar struggle when attempting to build a table block using the Notion API. After spending a fair amount of time testing various payload structures, I discovered that the API needs a fully defined data structure even for what appears to be a simple table. It was essential to include not just the column count, but also detailed row and cell definitions that mimicked the intended layout. The documentation can be a bit terse on these specifics, so verifying each nested object helped me bypass the validation errors.

Hello ! I struggle a lot with it…
Here is your answer !
=> Table width has to match the number of element in your row.

{
  "children": [
    {
      "type": "table",
      "table": {
        "table_width": 2,
        "children": [
          {
            "type": "table_row",
            "table_row": {
              "cells": [
                [
                  {
                    "type": "text",
                    "text": {
                      "content": "test1"
                    }
                  }
                ],
                [
                  {
                    "type": "text",
                    "text": {
                      "content": "test2"
                    }
                  }
                ]
              ]
            }
          }
        ]
      }
    }
  ]
}