javascriptpostnotion-apinotion-js

Confusing NotionAPI Validation Error (Javascript)


I am submitting the following object with my request to notion.pages.create():

{
    parent: {
        type: 'database_id',
        database_id: databaseID
    },
    properties: {
        Name: { 
            title: [ {
                    type: 'text',
                    text: {
                        content: 'Expense1',
                        link: null
                    },
                    annotations: {
                        bold: false,
                        italic: false,
                        strikethrough: false,
                        underline: false,
                        code: false,
                        color: 'default'
                    },
                    plain_text: 'Expense1',
                    href: null
            } ] 
        },
        Date: { date: { start: '2023-06-23' } },
        Amount: { number: 9.56 },
        Subcategory: []
    }
}

The Name is my title property, Date is a date, Amount is a dollar number, and Subcategory is a relation (Category is a rollup so I am omitting it in the request).

I get this error on my request:

"@notionhq/client warn: request fail {
  code: 'validation_error',
  message: 'body failed validation. Fix one:\n' +
    'body.properties.Name.id should be defined, instead was `undefined`.\n' +
    'body.properties.Name.name should be defined, instead was `undefined`.\n' +
    'body.properties.Name.start should be defined, instead was `undefined`.'
}

This is confusing to me as it conflicts with the way that the API docs describe how to fill in each property type. Also, when I change the order of the properties, the same error comes up (for example, it says 'body.properties.Amount.id should be defined...' instead of body.properties.Name). Anyone have any insight?

I tried changing the order of the objects and checking over my syntax to match the API docs several times, but to no avail.


Solution

  • So as an update for anyone who's having similar issues:

    First of all, the suggestions in the Notion API's error messages were not helpful for me. I ended up resolving the problem by querying my database for some of the existing rows and mimic'ing their structure. Basically like this:

    import { Client } from "@notionhq/client"
    const notion = new Client({ auth: process.env.NOTION_KEY })
    
    const result2 = await notion.databases.query({ database_id: expensesID });
    console.log(result2.results[0].properties);

    {
      parent: {
        type: 'database_id',
        database_id: databaseID
      },
      properties: {
        Date: { id: '%5CINz', type: 'date', date: { start: '2023-06-23', end: null, time_zone: null } },
        Amount: { id: 'T%3AsA', type: 'number', number: 9.56 },
        Subcategory: {
          id: 'vxSu',
          type: 'relation',
          relation: [Array],
          has_more: false
        },
        Name: { id: 'title', type: 'title', title: [
      {
        type: 'text',
        text: {
          content: 'Expense1',
          link: null
        },
        annotations: {
          bold: false,
          italic: false,
          strikethrough: false,
          underline: false,
          code: false,
          color: 'default'
        },
        plain_text: 'Expense1',
        href: null
      }
    ] }
      }
    }

    Note that the main difference is that each property has an 'id' prop and a 'type' prop. The order of the properties did not matter. Hope this helps someone!