'Notion API - filter database by created_time?
I'm trying to filter database entries based on the creation time, but tried many different combinations of filter and I always get an error. Sorting works fine.
I've also tried property: 'created_time' and date: {after:since} but it always complains that there's a property missing from the filter.
const response = await notion.databases.query({
database_id: DATABASE_ID,
filter: {
timestamp: 'created_time',
created_time: {after: since},
},
sorts: [
{
timestamp: 'created_time',
direction: 'ascending',
},
],
});
error:
@notionhq/client warn: request fail {
code: 'validation_error',
message: 'body failed validation. Fix one: body.filter.or should be defined, instead was `undefined`. body.filter.and should be defined, instead was `undefined`. body.filter.title should be defined, instead was `undefined`. body.filter.text should be defined, instead was `undefined`. body.filter.rich_text should be defined, instead was `undefined`. body.filter.number should be defined, instead was `undefined`. body.filter.checkbox should be defined, instead was `undefined`. body.filter.select should be defined, instead was `undefined`. body.filter.multi_select should be defined, instead was `undefined`. body.filter.date should be defined, instead was `undefined`. body.filter.people should be defined, instead was `undefined`. body.filter.files should be defined, instead was `undefined`. body.filter.url should be defined, instead was `undefined`. body.filter.email should be defined, instead was `undefined`. body.filter.phone should be defined, instead was `undefined`. body.filter.phone_number should be defined, instead was `undefined`. body.filter.relation should be defined, instead was `undefined`. body.filter.created_by should be defined, instead was `undefined`. body.filter.property should be defined, instead was `undefined`. body.filter.last_edited_by should be defined, instead was `undefined`. body.filter.last_edited_time should be defined, instead was `undefined`. body.filter.formula should be defined, instead was `undefined`. body.filter.rollup should be defined, instead was `undefined`.'
}
Solution 1:[1]
filter should be:
{
"filter": {
"timestamp": "created_time",
"created_time": {
"after": "2022-04-11T07:04:00"
}
}
}
Looks close, other issue could be using an outdated api version that doesn't support the date filter object, make sure to add most recent Notion-Version header: https://developers.notion.com/reference/versioning
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Avraham |
