Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Aug 06, 2022 09:14 AM
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)
Solved! Go to Solution.
Aug 08, 2022 11:49 AM
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);
Aug 06, 2022 06:04 PM
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.
Aug 07, 2022 01:38 PM
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.
Aug 07, 2022 04:20 PM
Are you trying to create a page as part of a database or as a child page?
Aug 08, 2022 11:39 AM
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.
Aug 08, 2022 11:40 AM
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/
Aug 08, 2022 11:42 AM
I tried making the parent item a string however, I received an error back
status: 400
code: “validation_error”
message: "body failed validation: body.parent should be an object, instead was a string
Aug 08, 2022 11:49 AM
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);
Aug 08, 2022 09:27 PM
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.
Aug 09, 2022 09:13 AM
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.