admin管理员组文章数量:1391991
I'm able to create a new page using the Notion API, and as part of that page I want to create & include a database table. I'm able to create the page successfully, but when trying to create the database using the child_database block, I am getting a status 400 error. Sample code below. Any help appreciated.
let payload = {
'parent': {'database_id': database_id_variable},
'properties': {
'Name': {
'title': [
{'text': {'content': 'My Grocery List Page'}}
]
}
},
'children': [
{
'object': 'block',
'type': 'child_database',
'child_database': {
'title': 'Grocery List',
'properties': {
'Grocery item': {
'type': 'title',
'title': [{ 'type': 'text', 'text': { 'content': 'Tomatoes' } }]
},
}
}
},
]
}
let response = await fetch('', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Authorization' : 'Bearer secret_XXX',
'Content-Type': 'application/json',
'Notion-Version': '2021-08-16',
},
});
console.log(await response.json());
}
I'm able to create a new page using the Notion API, and as part of that page I want to create & include a database table. I'm able to create the page successfully, but when trying to create the database using the child_database block, I am getting a status 400 error. Sample code below. Any help appreciated.
let payload = {
'parent': {'database_id': database_id_variable},
'properties': {
'Name': {
'title': [
{'text': {'content': 'My Grocery List Page'}}
]
}
},
'children': [
{
'object': 'block',
'type': 'child_database',
'child_database': {
'title': 'Grocery List',
'properties': {
'Grocery item': {
'type': 'title',
'title': [{ 'type': 'text', 'text': { 'content': 'Tomatoes' } }]
},
}
}
},
]
}
let response = await fetch('https://api.notion./v1/pages', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Authorization' : 'Bearer secret_XXX',
'Content-Type': 'application/json',
'Notion-Version': '2021-08-16',
},
});
console.log(await response.json());
}
Share
Improve this question
asked Dec 23, 2021 at 21:42
JoshJosh
911 silver badge5 bronze badges
3
- have figured something out, other than using a regular table? – l30c0d35 Commented Jan 23, 2022 at 22:41
- No, I don't think this is possible currently. – Josh Commented Jan 24, 2022 at 23:21
- yea, you are right, I just had it confirmed by the support. – l30c0d35 Commented Jan 25, 2022 at 11:45
3 Answers
Reset to default 4This is now possible by making 2 API calls, using the newer Notion API version 2022-06-28
.
Steps:
- Create the page as a child of the database, and return the new page's
ID
. - Create a new database as a child of the new page.
The 2 calls are, as far as I understand, required, since they cannot be bined in a single call (with a database as a block in the new page).
To create or update child_database type blocks, use the Create a database and the Update a database endpoints, specifying the ID of the parent page in the parent body param.
From https://developers.notion./reference/block#child-database
Create the page as a child of the database, and return the new page's ID
Using the Notion API, it's possible to create a new page in an existing database (of which you have the ID).
This can be done by making a POST
request to the /pages
endpoint with the following body:
{
parent: { type: 'database_id', database_id: 'parent_database_id' },
properties: {
Name: { // Name of the database's title property
title: [
{
text: { content: 'My Grocery List page' }
}
]
}
}
}
This in turn returns an object which has, among others, the id
property, which holds the ID of the newly created page.
- API docs on creating a page: https://developers.notion./reference/post-page
Create a new database as a child of the new page
To create a new database in a page, make a POST
request to the /databases
endpoint with the following body:
{
parent: { type: 'page_id', page_id: 'parent_page_id' },
title: [
{
text: { content: 'Grocery List' }
}
],
is_inline: true, // This shows the database INLINE in the parent page
properties: {
'Grocery item': {
title: {}
}
}
};
Please note the is_inline
property, set to true
(as stated in another answer). When true
, the database appears in the parent page as an inline block. When false
or omitted, the database appears as a child page of the parent page.
- API docs on creating a database: https://developers.notion./reference/create-a-database
- API docs on database properties (includes information about the
is_inline
property): https://developers.notion./reference/database
Complete (documented) code sample
(runnable via Node.js after changing the values for the constants defined on top)
// Environment constants
const API_URL = 'https://api.notion./v1';
const API_SECRET = 'my_api_secret';
const PARENT_DATABASE_ID = 'my_parent_database_id';
/**
* Do an API request to the Notion API.
*
* @param {string} path The API path.
* @param {string} method The HTTP method (get, post, put, patch, delete)
* @param {object} [body] The optional payload to send with the request.
*
* @returns {Promise.<object>} A promise which resolves into the parsed API response.
*/
const doRequest = async (path, method, body = null) => {
const opts = {
method: method.toUpperCase(),
headers: {
'Authorization': `Bearer ${API_SECRET}`,
'Content-Type': 'application/json',
'Notion-Version': '2022-06-28' // Note the newer API version in use
}
};
if (body) opts.body = JSON.stringify(body);
const response = await fetch(`${API_URL}/${path}`, opts);
return await response.json();
}
/**
* Create a new Notion page in an existing Notion database.
*
* @param {string} parentId The parent database's ID.
*
* @returns {Promise.<String>} A promise which resolves into the ID of the newly created page.
*/
const createNewPageInDatabase = async (parentId) => {
const payload = {
parent: { type: 'database_id', database_id: parentId },
properties: {
Name: {
title: [
{
text: { content: 'My Grocery List page' }
}
]
}
}
};
const response = await doRequest('pages', 'post', payload);
return response.id;
};
/**
* Create a new Notion DB with a title prop 'Grocery item' in an existing Notion page.
*
* @param {string} parentId The parent page's ID.
*
* @returns {Promise.<String>} A promise which resolves into the ID of the newly created database.
*/
const createNewDatabaseInPage = async (parentId) => {
const payload = {
parent: { type: 'page_id', page_id: parentId },
title: [
{
text: { content: 'Grocery List' }
}
],
is_inline: true, // This creates the database inline in the parent page
properties: {
'Grocery item': {
title: {}
}
}
};
const response = await doRequest('databases', 'post', payload);
return response.id;
}
/**
* Run the program.
*/
const init = async () => {
const newPageId = await createNewPageInDatabase(PARENT_DATABASE_ID);
const newSubDatabaseId = await createNewDatabaseInPage(newPageId);
console.log(`Created new DB ${newSubDatabaseId} in new page ${newPageId} in existing DB ${PARENT_DATABASE_ID}.`);
};
init();
While not a child database, I was able to resolve my issue by creating a simple table instead since Notion just announced the API would now support simple tables:
https://developers.notion./reference/block#table-blocks
I know it's been a while but I just stumbled upon this. As of today, there is the is_inline
property for databases. With it, you can simply create a database via the databases endpoint (Create a database), specify it's parent and set the is_inline
flag to true.
本文标签: javascriptCreating a child database in a new page in Notion APIStack Overflow
版权声明:本文标题:javascript - Creating a child database in a new page in Notion API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744661408a2618267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论