1

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.

Pluto
  • 4,177
  • 1
  • 3
  • 25
  • Maybe the API documentation is out of date? Can you link to the documentation you're reading? – Andy Jul 02 '23 at 14:08
  • @Andy https://developers.notion.com/reference/page-property-values – Jack O'Connor Jul 02 '23 at 14:10
  • Note: I've never used the NotionAPI but at a glance: ["If the new page is a child of an existing database, the keys of the properties object body param must match the parent database's properties."](https://developers.notion.com/reference/post-page) - it sounds like the db is enforcing the validation not the API, and you're missing property values that your db is expecting? – Andy Jul 02 '23 at 14:19
  • 1
    @Andy thank you for your help! I retrieved my database properties from the API and more strictly followed it and it worked (for anyone who may be experiencing similar issues: biggest difference was that each property received an id as specified by the database.properties attributes) – Jack O'Connor Jul 02 '23 at 14:34
  • You should add that as a (detailed) answer, and then mark it correct to give yourself the reward :) – Andy Jul 02 '23 at 14:47

1 Answers1

0

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!