56
loading...
This website collects cookies to deliver better user experience
Integrations don't have access to any pages (or databases) in the workspace at first. A user must share specific pages with an integration in order for those pages to be accessed using the API
/database
and selecting Table - Full Page
.Copy link
. Paste this somewhere to identify the ID of the newly created database.https://www.notion.so/0664caf380ec482d92b74b01730480f9?v=8b0f9cba5e72499380fcb251369f5b4b
Open the Hasura console by clicking on the button "Launch console".
Head to the Data tab and click on Create Heroku Database
to create a new Postgres database. Note that this is an optional step. We will not use the database in this example since we are writing an Action to create a GraphQL wrapper over Notion.
Now let's create an Action. Head to Actions
tab from the top menu and click on Create
to create a new action.
We will add a custom mutation to add a new item to the DB.
type Mutation {
addNotionItem (
databaseId: String!
properties: jsonb!
children: jsonb
): ItemOutput
}
ItemOutput
would look something like this:type ItemOutput {
id : String!
object : String!
created_time : timestamptz!
last_edited_time : timestamptz!
}
Authorization: Bearer <integration_token>
{
"parent": { "database_id": <database_id> },
"properties": {
"Name": {
"title": [
{
"text": {
"content": "iPhone 12"
}
}
]
}
}
}
Name
field in your table.Try on glitch
. This will create an app on Glitch platform with the Node.js boilerplate. We need to add an env
variable in the .env
file. Add the env NOTION_TOKEN
and paste the Integration token that we obtained at the beginning of this post.app.post('/addNotionItem', async (req, res) => {
// get request input
const { databaseId, properties, children } = req.body.input;
// run some business logic
const NOTION_TOKEN = process.env.NOTION_TOKEN;
const NOTION_PAGES_ENDPOINT = "https://api.notion.com/v1/pages";
const headers = {
"Authorization": `Bearer ${NOTION_TOKEN}`,
"Content-Type": "application/json"
};
const reqBody = {
"parent": { "database_id": databaseId },
"properties": properties
}
if(children) {
reqBody["children"] = children;
}
const options = {
method: 'POST',
headers: headers,
body: JSON.stringify(reqBody)
}
const fetchResponse = await fetch(NOTION_PAGES_ENDPOINT, options);
const responseJson = await fetchResponse.json();
console.log(responseJson);
/*
// In case of errors:
return res.status(400).json({
message: "error happened"
})
*/
// success
return res.json({
id: responseJson.id,
object: responseJson.object,
created_time: responseJson.created_time,
last_edited_time: responseJson.last_edited_time
})
});
/addNotionItem
to that and modify the Action URL to point to the glitch endpoint.https://lovely-bustling-environment.glitch.me/addNotionItem
mutation addNotionItem ($databaseId: String!, $properties: jsonb!) {
addNotionItem(databaseId: $databaseId, properties: $properties) {
id
object
created_time
last_edited_time
}
}
{
"databaseId": "<database_id>",
"properties": {
"Name": {
"title": [
{
"text": {
"content": "iPhone 12"
}
}
]
}
}
}
<database_id>
with your own database ID.{
"data": {
"addNotionItem": {
"id": "4bf24dc3-8a98-4247-856c-ba15500082cc",
"object": "page",
"created_time": "2021-05-21T12:57:46.751Z",
"last_edited_time": "2021-05-21T12:57:46.751Z"
}
}
}
Permissions
tab of the created Action and allow access for specific roles.Create a page
API. Now of course you can repeat this for any of the Notion's REST API by defining custom types appropriately and writing the webhook handler.