Building compound filters for Notion API database queries

I’m struggling to create multiple filter conditions using the Notion API. I can get single filters to work perfectly but when I try to combine them with AND logic, I keep getting validation errors.

My working single filter:

simple_filter = {
  "filter": {
    "timestamp": "created_time",
    "created_time": {
      "after": "2023-03-15T08:00:00"
    }
  }
}

The problematic combined filter:

combined_filter = {
    "filter": {
        "and": [
            {
                "timestamp": "created_time",
                "created_time": {
                    "after": "2023-03-15T08:00:00"
                }
            },
            {
                "property": "Category",
                "select": {
                    "equals": "b123cd4e-f567-8901-a234-5b6c78d90e12"
                }
            },
            {
                "property": "Priority",
                "select": {
                    "equals": "89abcdef-0123-4567-8901-234567890abc"
                }
            }
        ]
    }
}

The error I’m getting:

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

I’ve tried different approaches like using property names instead of UUIDs and various other combinations but nothing works. The API keeps complaining about undefined properties even though I’m following the documentation structure. Anyone know what I’m doing wrong with the filter syntax?

The Problem:

You are encountering validation errors when creating multiple filter conditions using the Notion API’s and operator. Your single filters work correctly, but combining them results in an error message indicating undefined properties, even though the filter structure seems to adhere to the documentation. This prevents you from effectively querying your Notion database with multiple criteria.

TL;DR: The Quick Fix:

The issue is inconsistent property referencing in your filter conditions. Ensure all conditions within the and array use the property key consistently, referencing property names exactly as they appear in your Notion database (case-sensitive). For created_time, use "property": "Created time". For select properties, verify you’re using the correct UUIDs for each option, not the display names.

Understanding the “Why” (The Root Cause):

The Notion API requires precise adherence to its data schema. Your error stems from inconsistencies in how you reference properties across filter conditions. While your filter appears correct visually, the API interprets them differently. Specifically, mixing "timestamp": "created_time" with other conditions using "property" leads to validation failure. All conditions inside an and array must use a consistent property specification format. Incorrect UUIDs for select properties are another common cause. Using display names instead of actual UUIDs will also lead to this error.

Step-by-Step Guide:

  1. Standardize Property Referencing: Replace any instances of "timestamp" with "property" across all filter conditions within your and array. Use the exact property names from your Notion database (case-sensitive). Do not use aliases.

  2. Verify Property IDs (Select Properties): If your filter conditions involve select properties (e.g., “Category,” “Priority”), ensure that you are using the correct UUIDs for each option. These are unique identifiers, found within your Notion database schema or using the Notion API explorer. Avoid using the display names of the options.

  3. Correct created_time Property: Refer to the created_time property using the consistent "property": "Created time" format.

  4. Corrected combined_filter Example: This example shows the corrected structure. Replace your existing combined_filter with this corrected code. Remember to substitute the placeholder UUIDs with the actual values from your Notion database.

combined_filter = {
    "filter": {
        "and": [
            {
                "property": "Created time",
                "created_time": {
                    "after": "2023-03-15T08:00:00"
                }
            },
            {
                "property": "Category",
                "select": {
                    "equals": "b123cd4e-f567-8901-a234-5b6c78d90e12"  # Replace with your actual UUID
                }
            },
            {
                "property": "Priority",
                "select": {
                    "equals": "89abcdef-0123-4567-8901-234567890abc"  # Replace with your actual UUID
                }
            }
        ]
    }
}
  1. Retest Your Filter: After applying these changes, retest your API call. Your combined_filter should now pass validation and return the expected results.

Common Pitfalls & What to Check Next:

  • Typos in Property Names: Carefully check for any typos in your property names. Case sensitivity is crucial!
  • Incorrect UUIDs: Double-check that you are using the correct UUIDs from your Notion database, not the display names of select options. Use the Notion API explorer if needed.
  • API Rate Limits: If you’re making many API calls, be aware of Notion’s rate limits to avoid temporary account suspension.
  • Alternative Filtering Methods: Explore other Notion API filter operators (or, not) if necessary for more complex queries. Refer to the official Notion API documentation.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

The Problem:

You’re encountering validation errors when creating multiple filter conditions using the Notion API’s and operator. Your single filters work correctly, but combining them results in an error message indicating undefined properties, even though the filter structure seems to adhere to the documentation. This prevents you from effectively querying your Notion database with multiple criteria.

:thinking: Understanding the “Why” (The Root Cause):

The error arises from inconsistencies in the structure of your filter conditions. While you might visually perceive the structure as correct, the Notion API requires precise adherence to its data schema. Specifically, the timestamp property used in your first condition is incompatible with the property property used in subsequent conditions within the and array. All conditions within an and array must use the same property specification format.

:gear: Step-by-Step Guide:

  1. Standardize Property Referencing: The most crucial step is to ensure consistency in how you reference properties across all filter conditions. Instead of using timestamp in one condition and property in others, uniformly use the property key for all conditions. Ensure that the value associated with the property key accurately reflects the name of the property in your Notion database. Do not use aliases or internal Notion IDs; use the exact display names as they appear in your database.

  2. Verify Property IDs (for Select Properties): When dealing with select properties (like your “Category” and “Priority” properties), double-check that you are using the correct UUIDs. These UUIDs are unique identifiers for each option within a select property, not the display names of those options. Inspect your database’s schema or utilize the Notion API explorer to find the precise UUID for each option.

  3. Correct the created_time Property: The created_time property is a special system property within Notion. While your existing structure might seem logical, it’s crucial to use the consistent property approach here as well. Reference the created_time property directly under the property key, as demonstrated in the corrected code below.

  4. Corrected combined_filter: Implement the changes discussed above to resolve the validation error. Replace your original combined_filter with the following:

combined_filter = {
    "filter": {
        "and": [
            {
                "property": "Created time",  # Corrected: Use "property" consistently
                "created_time": {
                    "after": "2023-03-15T08:00:00"
                }
            },
            {
                "property": "Category",
                "select": {
                    "equals": "b123cd4e-f567-8901-a234-5b6c78d90e12"  # Verify this UUID
                }
            },
            {
                "property": "Priority",
                "select": {
                    "equals": "89abcdef-0123-4567-8901-234567890abc"  # Verify this UUID
                }
            }
        ]
    }
}
  1. Retest Your Filter: After making these corrections, retest your API call to verify that the combined_filter now successfully passes validation and returns the expected results.

:mag: Common Pitfalls & What to Check Next:

  • Typos in Property Names: Double-check for any typos in your property names. Case sensitivity matters!
  • Incorrect UUIDs: Ensure you are using the actual UUIDs from your Notion database and not the display names of your select options.
  • API Rate Limits: If you’re making numerous API calls, be mindful of Notion’s API rate limits to avoid temporary account suspension.
  • Alternative Filtering Methods: Consider using the Notion API’s other filter operators (e.g., or, not) if you need more complex queries. Understanding each option’s capabilities and limitations will be important in the long run. Notion’s API documentation is a good place to review these.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Had this exact issue last month building filters for a project management database. The inconsistent property format is definitely the problem, but here’s another gotcha that caught me - use the actual property names from your Notion database, not the display names. For created_time, reference it as the system property name. Also check if those UUID values are actually option IDs from your select properties rather than display text. Notion’s API explorer helped me verify the correct property structure before coding. The validation errors can be misleading and often point to the wrong element in your filter array.

Your filter structure is inconsistent - that’s what’s causing the validation error. The first condition uses “timestamp”: “created_time” which is incorrect syntax for compound filters. All conditions in an AND array need to follow the same format with the “property” key. Here’s how to correct it:

combined_filter = {
    "filter": {
        "and": [
            {
                "property": "Created time",
                "created_time": {
                    "after": "2023-03-15T08:00:00"
                }
            },
            {
                "property": "Category", 
                "select": {
                    "equals": "b123cd4e-f567-8901-a234-5b6c78d90e12"
                }
            }
        ]
    }
}

I faced a similar issue when I began using compound filters. The key is ensuring the property names match exactly what’s in your Notion database schema and maintaining consistency in property references across your filters.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.