Looking for advice on constructing a POST request to /api/v2/analytics/conversations/details/query that groups by queue and media type. My Deno Deploy function sends the payload below but returns a 400 Bad Request with “Invalid aggregation query structure”. I’ve verified the token and endpoint, but the JSON schema for the aggregation object seems picky about nested array structures.
If I recall correctly, the issue isn’t just the json structure but how the details endpoint handles grouping keys. the suggestion above is a good start for flattening, but you are likely hitting a schema validation error because aggregation.groupings in the details query expects specific field names that map to the conversation detail schema, not just generic strings. when i was building a laravel service to pull this data, i found that using queue.id and mediaType directly in the groupings array often fails if the filter doesn’t explicitly scope the queue context first.
try switching to a groupBy array at the root level of the aggregation object instead of nesting it under groupings. the api v2 docs are a bit sparse on this, but the php sdk shows that the AnalyticsConversationsDetailsQuery object prefers a flat structure for the grouping keys. here is a working payload using guzzle that i use in my middleware. note that i am using queue.id instead of just queue because the detail schema requires the full path for the grouping key to resolve correctly against the conversation records.
this approach avoids the nested array structure that triggers the 400 error. also, make sure your oauth scope includes analytics:read because the details endpoint is stricter about permissions than the summary endpoints. if you still get an error, check the response body for the specific field that failed validation; it usually points to a missing or incorrectly typed grouping key.
Make sure you align the groupings array with the exact detail schema fields like queue.id and mediaType to avoid the 400 error. The suggestion above is correct, but using generic strings like “queue” breaks the validator.