How to create a table using the Notion API?

I’m struggling to use the Notion API to make a table. The official docs aren’t helping much. My code looks like this:

headers = {
    'Auth': f'Token {secret_key}',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Notion-Version': '2022-06-28'
}

payload = {'items': [{'type': 'block',
        'object': 'table',
        'table': {'columns': 4}}]}

result = requests.post('https://api.notion.com/v1/pages/abc123/blocks', headers=headers, json=payload)

But I get this error:

{'error': 'validation_error', 'details': 'payload.items[0].table.rows should be defined, but was not found.'}

Adding a simple heading works fine though:

payload = {'items': [{'type': 'block',
        'object': 'heading_2',
        'heading_2': {'text': [{'type': 'text',
        'content': 'Hello World'}]}}]}

I’ve searched everywhere but can’t find a solution. Any ideas what I’m doing wrong?

yo, i had same prob. try this:

payload = {
    'children': [{
        'object': 'block',
        'type': 'table',
        'table': {
            'table_width': 4,
            'has_column_header': True,
            'children': [{'type': 'table_row', 'table_row': {'cells': [['Col1'], ['Col2'], ['Col3'], ['Col4']]}}]
        }
    }]
}

should work. dont forget to use ‘Bearer’ in ur auth header too

I’ve encountered similar issues with the Notion API before. From my experience, including the ‘rows’ parameter in your table definition—even if initially empty—is essential. I modified the payload as follows:

payload = {
    'children': [{
        'object': 'block',
        'type': 'table',
        'table': {
            'table_width': 4,
            'has_column_header': True,
            'has_row_header': False,
            'children': [
                {
                    'type': 'table_row',
                    'table_row': {
                        'cells': [
                            [{'type': 'text', 'text': {'content': 'Header 1'}}],
                            [{'type': 'text', 'text': {'content': 'Header 2'}}],
                            [{'type': 'text', 'text': {'content': 'Header 3'}}],
                            [{'type': 'text', 'text': {'content': 'Header 4'}}]
                        ]
                    }
                }
            ]
        }
    }]
}

This payload creates a table with headers. You can add more rows by appending new objects to the ‘children’ list within the table object. Hope this helps!

I’ve worked with the Notion API extensively, and tables can be tricky. Your issue stems from the payload structure. The API expects a specific format for tables. Here’s a corrected version:

payload = {
    'children': [{
        'object': 'block',
        'type': 'table',
        'table': {
            'table_width': 4,
            'has_column_header': True,
            'children': [
                {
                    'type': 'table_row',
                    'table_row': {
                        'cells': [['Column 1'], ['Column 2'], ['Column 3'], ['Column 4']]
                    }
                }
            ]
        }
    }]
}

This creates a basic table with headers. You can add more rows by appending to the ‘children’ list. Remember to adjust the URL to ‘/blocks’ instead of ‘/pages/abc123/blocks’. Also, ensure your authorization header uses ‘Bearer’ instead of ‘Token’. These small details often cause issues with the Notion API.