Skip to main content
Solved

Error with Post request to Notion API


Zack_S
Forum|alt.badge.img+15

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)

Best answer by Zack_S

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);


View original
Did this topic help you find an answer to your question?

10 replies

Justin_Barrett
Forum|alt.badge.img+20

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.


Forum|alt.badge.img+19
  • Inspiring
  • 3264 replies
  • August 7, 2022
Justin_Barrett wrote:

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.


LeanZubrezki
Forum|alt.badge.img+4
  • Participating Frequently
  • 9 replies
  • August 7, 2022

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


Zack_S
Forum|alt.badge.img+15
  • Author
  • Inspiring
  • 95 replies
  • August 8, 2022
LeanZubrezki wrote:

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.


Zack_S
Forum|alt.badge.img+15
  • Author
  • Inspiring
  • 95 replies
  • August 8, 2022
Bill_French wrote:

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.


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/


Zack_S
Forum|alt.badge.img+15
  • Author
  • Inspiring
  • 95 replies
  • August 8, 2022
Justin_Barrett wrote:

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.


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
Forum|alt.badge.img+15
  • Author
  • Inspiring
  • 95 replies
  • Answer
  • August 8, 2022

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);



Justin_Barrett
Forum|alt.badge.img+20
Zack_S wrote:

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.


Zack_S
Forum|alt.badge.img+15
  • Author
  • Inspiring
  • 95 replies
  • August 9, 2022
Justin_Barrett wrote:

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.


Justin_Barrett
Forum|alt.badge.img+20
Zack_S wrote:

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.


Thanks for the update. I guess I’ve just got my own issues with Notion’s wording. :winking_face:

As for the issue you encountered, there might be some deeper reason why a database ID worked where a page ID didn’t. Without knowing a lot more about your specific setup, though, it’s tough to say why.


Reply