Filtering Notion database entries by creation date

I’m having trouble filtering my Notion database based on when entries were created. I’ve tried different filter options but keep getting errors. Sorting works fine though.

Here’s what I’ve attempted:

const result = await notionClient.databases.query({
  database_id: MY_DB_ID,
  filter: {
    dateCreated: 'created_time',
    created_time: { after: cutoffDate },
  },
  sort: [
    {
      dateCreated: 'created_time',
      order: 'asc',
    },
  ],
});

The error message says something about missing properties in the filter. I’ve also tried using property: 'created_time' and date: {after: cutoffDate} but no luck. Any ideas on how to correctly filter by creation date? Thanks!

I’d like to add a bit to what’s been said. In my experience, the Notion API can be quite particular about how you structure your queries. Here’s an alternative approach that’s worked well for me:

const result = await notionClient.databases.query({
  database_id: MY_DB_ID,
  filter: {
    and: [{
      timestamp: 'created_time',
      created_time: { after: cutoffDate.toISOString() }
    }]
  },
  sorts: [{ timestamp: 'created_time', direction: 'ascending' }]
});

This method uses an ‘and’ array in the filter, which can be useful if you need to add more complex filtering later. It’s also worth noting that Notion’s API documentation is regularly updated, so it’s always a good idea to check there for the most current syntax. Hope this helps you resolve your issue, Zack!

hey zack, i had the same issue. try changing ur filter like this:

filter: {
  property: 'Created time',
  date: { after: cutoffDate }
}

make sure ‘Created time’ matches exactly whats in ur db. hope this helps!

I’ve dealt with this exact problem before, and it can be frustrating. The key is understanding how Notion’s API handles date filters. Here’s what worked for me:

const result = await notionClient.databases.query({
  database_id: MY_DB_ID,
  filter: {
    timestamp: 'created_time',
    created_time: {
      after: cutoffDate.toISOString()
    }
  },
  sorts: [
    {
      timestamp: 'created_time',
      direction: 'ascending'
    }
  ]
});

The main things to note:

  • Use ‘timestamp’ instead of ‘property’ for created_time
  • Ensure cutoffDate is in ISO format
  • For sorting, use ‘direction’ instead of ‘order’

This approach should resolve your filtering issues. Let me know if you need any clarification!