Encountering errors while querying Notion database with Power Query M

Hey folks, I’m stuck with a problem in Power Query M. I’m trying to fetch data from my Notion database, but I keep getting a 400 error. Can anyone spot what’s wrong with my code?

let
  dbEndpoint = "[DATABASE_ENDPOINT]",
  authHeaders = [
    Accept = "application/json",
    ContentType = "application/json",
    Token = "Bearer " & secretKey,
    ApiVersion = "2022-06-28"
  ],
  filterCriteria = Json.FromValue({
    "filter" = {
      "property" = "DueDate",
      "date" = {
        "current_week" = {}
      }
    }
  }),
  apiCall = Web.Contents(dbEndpoint, [
    Headers = authHeaders,
    Content = filterCriteria
  ]),
  parsedResult = Json.Document(apiCall)
in
  parsedResult

I’m trying to filter for items due this week. The code looks okay to me, but something’s not right. Any ideas what could be causing the 400 error? Thanks in advance for your help!

hey mate, i think i kno whats wrong. ur sending the filter in the body, but notion wants it as a query param. try this:

apiCall = Web.Contents(dbEndpoint & “?filter=” & Uri.EscapeDataString(Json.FromValue(filterCriteria)))

also, use ‘this_week’ instead of ‘current_week’. that shud fix it. lemme kno if u need more help!

I’ve encountered similar issues when working with the Notion API in Power Query. From my experience, the problem might be in how you’re structuring the filter criteria. Notion’s API is quite particular about the JSON format for filters.

Try modifying your filterCriteria like this:

filterCriteria = Json.FromValue({
    filter = {
        property = "DueDate",
        date = {
            this_week = {}
        }
    }
})

Notice I changed ‘current_week’ to ‘this_week’ - that’s the correct syntax for Notion’s API. Also, make sure your dbEndpoint is correct and includes ‘/query’ at the end.

If you’re still getting errors, double-check your secretKey. Sometimes API keys can expire or be invalidated. You might want to generate a new one in your Notion settings just to be sure.

Lastly, consider using Fiddler or Postman to test your API call outside of Power Query. It can help isolate whether the issue is with your query or with Power Query itself.

I’ve run into this issue before when working with Notion’s API. The problem likely lies in how you’re sending the filter criteria. Notion expects the filter to be sent as a query parameter, not in the request body.

Try modifying your apiCall like this:

apiCall = Web.Contents(dbEndpoint, [
    Headers = authHeaders,
    Query = [
        filter = Json.FromValue({
            property = "DueDate",
            date = {
                this_week = {}
            }
        })
    ]
])

This change moves the filter into the Query parameter and adjusts the structure slightly. Also, as Pete_Magic mentioned, use ‘this_week’ instead of ‘current_week’.

If you’re still having trouble, double-check your dbEndpoint. It should end with ‘/query’ for database queries. And don’t forget to verify your API key’s validity and permissions.