Help

Re: Error with Post request to Notion API

Solved
Jump to Solution
11037 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Zack_S
8 - Airtable Astronomer
8 - Airtable Astronomer

Trying to make a post request to Notion to create a new page. Receiving an Error 400 Bad Request and can’t find the issue.

Here’s the documentation - Create a page
and script below

let payload = {
    'parent': {
        "type": "page_id",
        "page_id": "my page id"
    },
    'properties': {
        "Name": {
            "type": "title",
            "title": [{ "type": "text", "text": { "content": "My Page Title" } }]
        }
    }
};



let response = await remoteFetchAsync(postUrl, {
    method: 'POST',
    body: JSON.stringify(payload),

    headers: {
        'Notion-Version': '2022-06-28',
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`,
    },
});

output.text('**POST RESPONSE**' + ' - ' + response.status)
1 Solution

Accepted Solutions
Zack_S
8 - Airtable Astronomer
8 - Airtable Astronomer

Create a record in Notion Database and add data to the Page.


let apiKey = 'Bearer secret_XXX' 
const url = "https://api.notion.com/v1/pages";

let payload = {
    "parent": {
        "type": "database_id",
        "database_id": BASE_ID // Replace BASE_ID with your base id
    },
    "properties": {
        "Name": {
            "title": [
                {
                    "text": {
                        "content": "Title goes here"
                    }
                }
            ]
        },

        "Company": {
            "rich_text": [
                {
                    "type": "text",
                    "text": {
                        "content": "The Company Name"
                    }
                }
            ]
        }
    },
    "children": [
        {
            "type": "code",
            "code": {
                "rich_text": [{
                    "type": "text",
                    "text": {
                        "content": "Your Code goes here..."
                    }
                }],
                "language": "json"
            }
        },
    ]
}


let options = {
    'muteHttpExceptions': true,
    "method": "post",
    "headers": {
        Authorization: apiKey,
        "Content-Type": "application/json",
        "Notion-Version": "2022-06-28",
    },
    "body": JSON.stringify(payload)

};

let response = await remoteFetchAsync(url, options);
console.log(response.status);


See Solution in Thread

10 Replies 10

A quick peek at the docs leads me to think that your request is incorrect. In the request example, the parent item is shown as a string, not an object. To see this and other helpful info, click on the “Examples” dropdown on the right (next to “Request”), then choose “Request Example”. It will show an example request, including required types.

Agree. To me, the properties look odd considering Notion’s requirements. On the left is Notion’s docs; yours seems a bit dissimilar with the type element.

image

LeanZubrezki
5 - Automation Enthusiast
5 - Automation Enthusiast

Are you trying to create a page as part of a database or as a child page?

Originally was trying to create a page. I solved my problem by using a database instead. However, I did go back to see if I could create a page using the suggestions above but still no luck.

Solved the problem by creating a database.

I did go through these examples, trying to create a page to see if your suggestions worked. But no luck.

Appreciate the help anyway/

I tried making the parent item a string however, I received an error back

  1. status: 400

  2. code: “validation_error”

  3. message: "body failed validation: body.parent should be an object, instead was a string

Zack_S
8 - Airtable Astronomer
8 - Airtable Astronomer

Create a record in Notion Database and add data to the Page.


let apiKey = 'Bearer secret_XXX' 
const url = "https://api.notion.com/v1/pages";

let payload = {
    "parent": {
        "type": "database_id",
        "database_id": BASE_ID // Replace BASE_ID with your base id
    },
    "properties": {
        "Name": {
            "title": [
                {
                    "text": {
                        "content": "Title goes here"
                    }
                }
            ]
        },

        "Company": {
            "rich_text": [
                {
                    "type": "text",
                    "text": {
                        "content": "The Company Name"
                    }
                }
            ]
        }
    },
    "children": [
        {
            "type": "code",
            "code": {
                "rich_text": [{
                    "type": "text",
                    "text": {
                        "content": "Your Code goes here..."
                    }
                }],
                "language": "json"
            }
        },
    ]
}


let options = {
    'muteHttpExceptions': true,
    "method": "post",
    "headers": {
        Authorization: apiKey,
        "Content-Type": "application/json",
        "Notion-Version": "2022-06-28",
    },
    "body": JSON.stringify(payload)

};

let response = await remoteFetchAsync(url, options);
console.log(response.status);


My gut feeling on this is that the parent type should be “page” or “database”, not “page_id” or “database_id”; i.e. the parent page/database obviously has an ID property, but the ID itself isn’t the literal parent, so it’s even more confusing why your most recent example actually worked with “database_id” as the parent type.

Notion’s lack of clear examples for situations like this is more than a little frustrating. I recall encountering similar documentation issues when I last worked with their API (I was only reading from it, not writing to it). It also feels at times like they made their data structure a little too granular. I kinda-sorta get why they did it, but that didn’t make using it any easier.

I don’t have a lot of experience with code or scripting and am only self taught so I don’t have much to offer in the form of a solution. But I spent a lot of time really trying to understand the documentation and breaking it down into the most basic way to see where the issue was coming from but still no solution in creating a ‘page’. Only had success with a database.

The notion docs show the page parent to be as follows

{
        "type": "page_id",
        "page_id": "59833787-2cf9-4fdf-8782-e53db20768a5"
}

I keep receving the following error.

object: "error"
status: 400
code: "validation_error"
message: "body failed validation: body.parent.database_id should be defined, instead was `undefined`."

When using this as my script. Tried commenting out the properties and still receive the same error.

let payload = {
    "parent": {
        "type": "page_id",
        "page_id": "f26c0a4b-2feb-45ac-bd54-3c0917c5ac01"
    },
    // 'properties': {
    //     "Name": {
    //         "title": [
    //             {
    //                 "text": {
    //                     "content": "The title"
    //                 }
    //             }
    //         ]
    //     }
    // }
};

Thanks again Justin for your help.