Hey there, fellow developer! Ready to dive into the world of Notion API integrations? You're in for a treat. We'll be using the @notionhq/client
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get you set up with Notion:
Time to get our hands dirty:
mkdir notion-api-project cd notion-api-project npm init -y npm install @notionhq/client
Easy peasy, right?
Let's write some code:
const { Client } = require('@notionhq/client'); const notion = new Client({ auth: 'your-api-key-here' });
Boom! You're connected. Feel the power!
async function getPage(pageId) { const response = await notion.pages.retrieve({ page_id: pageId }); console.log(response); }
async function createPage(parentId, title) { const response = await notion.pages.create({ parent: { database_id: parentId }, properties: { title: { title: [{ text: { content: title } }] } } }); console.log(response); }
async function updatePage(pageId, newTitle) { const response = await notion.pages.update({ page_id: pageId, properties: { title: { title: [{ text: { content: newTitle } }] } } }); console.log(response); }
async function deletePage(pageId) { const response = await notion.pages.update({ page_id: pageId, archived: true }); console.log(response); }
async function queryDatabase(databaseId) { const response = await notion.databases.query({ database_id: databaseId }); console.log(response); }
async function addDatabaseItem(databaseId, title) { const response = await notion.pages.create({ parent: { database_id: databaseId }, properties: { Name: { title: [{ text: { content: title } }] } } }); console.log(response); }
async function updateDatabaseItem(pageId, newTitle) { const response = await notion.pages.update({ page_id: pageId, properties: { Name: { title: [{ text: { content: newTitle } }] } } }); console.log(response); }
async function advancedQuery(databaseId) { const response = await notion.databases.query({ database_id: databaseId, filter: { property: 'Status', select: { equals: 'Done' } }, sorts: [ { property: 'Date', direction: 'descending' } ] }); console.log(response); }
async function addBlock(pageId) { const response = await notion.blocks.children.append({ block_id: pageId, children: [ { object: 'block', type: 'paragraph', paragraph: { text: [{ type: 'text', text: { content: 'Hello, World!' } }] } } ] }); console.log(response); }
Always wrap your API calls in try-catch blocks:
try { await notion.pages.retrieve({ page_id: 'non-existent-id' }); } catch (error) { console.error(error.body); }
And don't forget about rate limits! Be kind to the API:
const { promisify } = require('util'); const sleep = promisify(setTimeout); async function rateLimitedOperation() { try { await notion.databases.query({ database_id: 'your-database-id' }); await sleep(334); // Roughly 3 requests per second } catch (error) { if (error.code === 'rate_limited') { console.log('Rate limited! Waiting before retry...'); await sleep(error.retryAfter * 1000); } else { throw error; } } }
And there you have it! You're now equipped to build some awesome Notion integrations. Remember, this is just scratching the surface – there's so much more you can do with the Notion API. Keep exploring, keep building, and most importantly, have fun!
For more in-depth info, check out the official Notion API documentation. Now go forth and create something amazing!