google-apps-scripturlfetchnotion-api

Post data to table in Notion with Apps Script


I'm testing the notion API and I would like to push data into my Notion table from Apps Script. Here the code I got so far and the error below. I assume my json structure is not right, but I can't figure out how to fix it

function tryout_notion () {

  const url = "https://api.notion.com/v1/pages";
  //tried with : https://api.notion.com/v1/databases

  var options = {
                  'muteHttpExceptions': true,
                  "method" : "post",
                  "headers": {
                              Authorization: `Bearer secret_*****`,
                              "Content-Type": "application/json",
                              "Notion-Version": "2021-05-13",
                            },
                  "parent": {
                    "page_id": "*****"
                    //tried with : "database_id"
                  },
                  "properties": {
                    "Name": {
                      "title": [
                        {
                          "text": {
                            "content": "CREATE NEW LINE IN NOTION"
                          }
                        }
                      ]
                    }
                  }
                };
  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}
{"object":"error","status":400,"code":"validation_error","message":"body failed validation: body.parent should be defined, instead was `undefined`."}

Solution

  • Found it, here the solution for documentation :

    function tryout_notion () {
    
    const url = "https://api.notion.com/v1/pages";
    
    var payload = {
                    "parent": {
                      "type": "database_id",
                      "database_id": "*****"
                    },
                    "properties": {
                      "Name": {
                        "title": [
                          {
                            "text": {
                              "content": "CREATE NEW LINE IN NOTION"
                            }
                          }
                        ]
                      }
                    }
    
      }
    
      var options = {
                      'muteHttpExceptions': true,
                      "method" : "post",
                      "headers": {
                                  Authorization: `Bearer secret_*****`,
                                  "Content-Type": "application/json",
                                  "Notion-Version": "2021-05-13",
                                },
                      "payload": JSON.stringify(payload)
    
                    };
      const response = UrlFetchApp.fetch(url, options);
      Logger.log(response);
    }