To fetch todos from a Notion database list using the Notion API, you'll need to follow these steps:
- Create a Notion integration to get an API key
- Share your database with the integration
- Use the "Query a database" endpoint to fetch the list of tasks
Here's a step-by-step guide with code examples:
-
Create a Notion integration: o to https://www.notion.so/my-integrations
-
Click "New integration" and follow the prompts
-
Save the API key (secret token) securely
- Share your database with the integration: Open your Notion database
- Click "Share" in the top right corner
- Use the "@" symbol to invite your integration
Here's a JavaScript example using the Notion SDK:
//JAVASCRIPT
const { Client } = require('@notionhq/client');
// Initialize the Notion client
const notion = new Client({ auth: 'your_api_key_here' });
// Replace with your actual database ID
const databaseId = 'your_database_id_here';
async function getTodos() {
try {
const response = await notion.databases.query({
database_id: databaseId,
filter: {
property: 'Status',
status: {
equals: 'To Do'
}
},
sorts: [
{
property: 'Priority',
direction: 'descending'
}
]
});
console.log("Todos:");
response.results.forEach(page => {
console.log(page.properties.Name.title[0].plain_text);
});
} catch (error) {
console.error(error);
}
}
getTodos();
This script does the following:
- Initializes the Notion client with your API key
- Defines the database ID you want to query
- Uses the databases.query method to fetch todos
- Filters for items with a "Status" of "To Do"
- Sorts the results by "Priority" in descending order
- Logs the name of each todo item
Make sure to replace 'your_api_key_here' with your actual API key and 'your_database_id_here' with your actual database ID.
Also, adjust the property names ("Status", "Priority", "Name") to match the actual property names in your Notion database.
Remember to install the Notion SDK before running this script:
npm install @notionhq/client
This example provides a basic implementation. You can modify the filter and sort options to suit your specific needs, and process the returned data as required for your application. References: