Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created February 7, 2025 02:39
Show Gist options
  • Save jpalala/fc68126346f931397f7450af6870d92d to your computer and use it in GitHub Desktop.
Save jpalala/fc68126346f931397f7450af6870d92d to your computer and use it in GitHub Desktop.
Fetching todos from a notion database guide

To fetch todos from a Notion database list using the Notion API, you'll need to follow these steps:

  1. Create a Notion integration to get an API key
  2. Share your database with the integration
  3. Use the "Query a database" endpoint to fetch the list of tasks

Here's a step-by-step guide with code examples:

  1. Create a Notion integration: o to https://www.notion.so/my-integrations

  2. Click "New integration" and follow the prompts

  3. 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

Use the "Query a database" endpoint:

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:

  1. Initializes the Notion client with your API key
  2. Defines the database ID you want to query
  3. Uses the databases.query method to fetch todos
  4. Filters for items with a "Status" of "To Do"
  5. Sorts the results by "Priority" in descending order
  6. 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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment