python-3.xnotion-apinotion

Notion API Filter query construction


I can not get this working but need to apply 2 filters in my query - the and (probably core filter) I can not get to work. Included picture is of the working filter in the Notion.

I am using Python and various Notion packages and straight requests calls too. I have a working query, en example for "created after", the filter works and looks like this:

filter = {
  "filter": {
    "timestamp": "created_time",
    "created_time": {
      "after": "2023-04-01T07:04:00"
    }
  }
}

Again, above works, but any variation of what I think needs to happen returns the classic error (shortened):

notion_client.errors.APIResponseError: body failed validation. Fix one:
body.filter.and[1].title should be defined, instead was `undefined`.

...and so on...

The above error came via this filter:

filter = {
    "filter": {
        "and": [
            {
                "timestamp": "created_time",
                "created_time": {
                    "after": "2023-04-01T07:04:00"
                }
            },
            {
                "properties": "Project",
                "id": {
                    "equals": "a211ab3d-a71b-49b9-a7a4-7b6f11e48b0c"
                }
            },
            {
                "properties": "Status",
                "id": {
                    "equals": "25350d5b-a927-4036-aba7-f509b0e390d4"
                }
            }
        ]
    }
}

Other variations, such as using the name or items other than the UUID return the same error. At least 20 different combinations tried, same result.

If the properties block is taken out of the and and straight queried the same happens.

I suspect the query is wrong, but I am at a loss. My work is modeled after the example in docs:

https://developers.notion.com/reference/post-database-query

Thanks in advance.

enter image description here

11AprEdit

Via answer here is all the code and response.

import requests
import json
import notion_client
from pprint import pprint

database_id = "xoxoxoxoxoxoxo"

NOTION_TOKEN = "secret_xoxoxoxoxoxoxox"

notion = notion_client.Client(auth=NOTION_TOKEN)


notion = notion_client.Client(auth=NOTION_TOKEN)

# works fine!
hold_filter = {
  "filter": {
    "timestamp": "created_time",
    "created_time": {
      "after": "2023-04-01T07:04:00"
    }
  }
}

filter = {  # error pertains to this filter
    "filter": {
        "and": [
            {
                "timestamp": "created_time",
                "created_time": {
                    "after": "2023-04-01T07:04:00"
                }
            },
            # commenting out the next 2 objects in this list also works
            {
                "property": "Project",
                "id": {
                    "equals": "a211ab3d-a71b-49b9-a7a4-7b6f11e48b0c"
                }
            },
            {
                "property": "Status",
                "id": {
                    "equals": "25350d5b-a927-4036-aba7-f509b0e390d4"
                }
            }
            # end sketchy part
        ]
    }
}


def get_all_page_from_db():
    search_database = notion.databases.query(
        database_id,
        **filter
    )
    return search_database

print(get_all_page_from_db())

Generates:

 raise APIResponseError(response, body["message"], code)
notion_client.errors.APIResponseError: body failed validation. Fix one:
body.filter.and[1].title should be defined, instead was `undefined`.
body.filter.and[1].rich_text should be defined, instead was `undefined`.
body.filter.and[1].number should be defined, instead was `undefined`.
body.filter.and[1].checkbox should be defined, instead was `undefined`.
body.filter.and[1].select should be defined, instead was `undefined`.
body.filter.and[1].multi_select should be defined, instead was `undefined`.
body.filter.and[1].status should be defined, instead was `undefined`.
body.filter.and[1].date should be defined, instead was `undefined`.
body.filter.and[1].people should be defined, instead was `undefined`.
body.filter.and[1].files should be defined, instead was `undefined`.
body.filter.and[1].url should be defined, instead was `undefined`.
body.filter.and[1].email should be defined, instead was `undefined`.
body.filter.and[1].phone_number should be defined, instead was `undefined`.
body.filter.and[1].relation should be defined, instead was `undefined`.
body.filter.and[1].created_by should be defined, instead was `undefined`.
body.filter.and[1].created_time should be defined, instead was `undefined`.
body.filter.and[1].last_edited_by should be defined, instead was `undefined`.
body.filter.and[1].last_edited_time should be defined, instead was `undefined`.
body.filter.and[1].formula should be defined, instead was `undefined`.
body.filter.and[1].rollup should be defined, instead was `undefined`.
body.filter.and[1].or should be defined, instead was `undefined`.
body.filter.and[1].and should be defined, instead was `undefined`.

I have tried variations on "id" and "equals" from the direct object UUID to a precise copy-paste of the name. All in accordance with the docs.

The query fragment for "created after" always works.


Solution

  • I tried querying my database using the part of your code that you mention as problematic. After reading through and trying example cases from Filter Database Entries, it seems to me that one cannot use "id" in filter object, it has to be the property type instead. Since you have a "rich_text" and a "select" property, the code should be as follows:

    filter = {
        "filter": {
            "and":[
                {
                    "property": "Project",
                    "rich_text": {
                        "contains": "!-☀️-Daily"
                    }
                },
                {
                    "property": "Select",
                    "select": {
                        "equals": "In Progress"
                    }
                }
            ]
        }
    }
    

    Hope it helps.