Unity Notion API Database Query: Handling 400 Bad Request Error with start_cursor

Struggling with Notion API Database Query in Unity

I’m having trouble with my Notion database query in Unity. The API works fine for the first 100 items, but when I try to get more using start_cursor, I get a 400 Bad Request error.

Here’s what I’m doing:

  1. Making a POST request to https://api.notion.com/v1/databases/{databaseID}/query
  2. Using next_cursor to get more pages
  3. Saving responses in a list

The first query works great, but when I use the start_cursor parameter for the next page, it fails.

Am I missing something? Is this the right way to use the API?

Here’s a simplified version of my code:

string nextCursor = null;
bool hasMore = true;

while (hasMore)
{
    string queryEndpoint = string.IsNullOrEmpty(nextCursor) 
        ? EndPointQuery 
        : EndPointQuery + $"?start_cursor={nextCursor}";

    var response = await MakeApiRequest(queryEndpoint);
    
    if (response.IsSuccess)
    {
        ProcessResults(response.Data);
        hasMore = response.HasMore;
        nextCursor = response.NextCursor;
    }
    else
    {
        Debug.Log($"Error: {response.Error}");
        break;
    }
}

Any ideas on what might be causing this 400 error? Thanks!

I encountered a similar issue when working with the Notion API in Unity. The problem lies in how you’re handling pagination. Instead of appending the start_cursor to the URL, you should include it in the request body as JSON.

Here’s a modified approach that should work:

string nextCursor = null;
bool hasMore = true;

while (hasMore)
{
    var requestBody = new Dictionary<string, object>();
    if (!string.IsNullOrEmpty(nextCursor))
    {
        requestBody["start_cursor"] = nextCursor;
    }

    var response = await MakeApiRequest(EndPointQuery, JsonConvert.SerializeObject(requestBody));
    
    if (response.IsSuccess)
    {
        ProcessResults(response.Data);
        hasMore = response.HasMore;
        nextCursor = response.NextCursor;
    }
    else
    {
        Debug.LogError($"API Error: {response.Error}");
        break;
    }
}

Ensure your MakeApiRequest method is set up to send the body as JSON. This should resolve the 400 Bad Request error you’re experiencing.

I’ve run into this exact issue before with the Notion API in Unity. The problem is likely in how you’re constructing the query URL for subsequent requests. Instead of appending the start_cursor as a query parameter, you should include it in the request body.

Try modifying your code to always use the same endpoint (EndPointQuery) and include the start_cursor in the request body like this:

var requestBody = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(nextCursor))
{
    requestBody["start_cursor"] = nextCursor;
}

var response = await MakeApiRequest(EndPointQuery, requestBody);

Make sure your MakeApiRequest method is set up to handle the body parameter and send it as JSON in the request. This approach worked for me and should resolve the 400 error you’re encountering. Let me know if you need any further clarification!

hey olivias, i had the same issue! the trick is to put the start_cursor in the request body, not the URL. change your MakeApiRequest like this:

var body = new Dictionary<string, object>();
if (nextCursor != null) body[“start_cursor”] = nextCursor;
var response = await MakeApiRequest(EndPointQuery, JsonConvert.SerializeObject(body));

that should fix it! lmk if u need more help