Calling Customer List Groups

I have the below C# code calling the customer list groups endpoint the endpoint is always sending back a cursor unless I remove the limit and let it use the default. With the limit added to the query it never does come to the end of the list. I have two groups currently in my sandbox. FYI, I did see similar behavior when using the “Try API” on the Square Developer web page, but there it finally did stop sending a cursor but it sent back several empty groups while still showing a cursor.
What am I doing wrong?

            var pagination = true;
            var listGroupsURL = "https://connect.squareupsandbox.com/v2/customers/groups?limit=2";
            var cursor = string.Empty;
            using var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            while (pagination)
            {
                // This search will use the default limit of 50 records to be returned.
                var response = await httpClient.GetAsync($"{listGroupsURL}{cursor}");

                response.EnsureSuccessStatusCode();

                var json = await response.Content.ReadAsStringAsync();

                // This could be null if no groups exist.
                SquareGroupList? groupList = JsonSerializer.Deserialize<SquareGroupList>(json);

                // Add groups to the list if they exists.
                if (groupList?.Groups != null)
                {
                    groups.AddRange(groupList.Groups);
                }

                if (string.IsNullOrEmpty(groupList?.Cursor))
                {
                    pagination = false;
                }
                else
                {
                    cursor = $"&cusor={groupList.Cursor}";
                }
            }

:waving_hand: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Customer Groups API
Common Square API Patterns
Search for Customer Profiles

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

@Robin063Collins how embarrassing… :grimacing: thanks so much for pointing that out. All I can say is that it was late my time when I was trying to get it to work.

The behavior you’re experiencing with the cursor and pagination is related to how Square’s API handles pagination. When you specify a limit, the API might return a cursor even if there are no more results to fetch. This is because the limit is treated as advisory, and the API might return more or fewer results than specified.

Here are a few things to consider and try:

  1. Cursor Handling: Ensure that you’re correctly appending the cursor to the URL. It looks like there’s a typo in your code where you append the cursor: &cusor={groupList.Cursor} should be &cursor={groupList.Cursor}.

  2. Check for Empty Groups: If the API is returning empty groups, ensure that your deserialization logic correctly handles empty or null responses. The API might return an empty list of groups with a cursor if there are no more groups to fetch.

  3. Limit and Cursor: When you set a limit, the API might still return a cursor even if there are no more groups to fetch. This is expected behavior. You should continue to check if the groupList?.Groups is null or empty and break the loop if it is, even if a cursor is returned.

  4. Default Limit: If removing the limit resolves the issue, consider using the default limit unless you have a specific reason to limit the number of results per page.

  5. Debugging: Add logging to see the responses and cursor values. This can help you understand if the API is returning unexpected results or if there’s an issue with how the cursor is being handled.

By addressing these points, you should be able to handle pagination more effectively and avoid infinite loops or unnecessary API calls.

This answer was reviewed by @Bryan-Square.