Analytics Aggregates API returning 400 Bad Request with 'Invalid query' error

I am trying to build a custom reporting widget for our agent desktop using the Embeddable Client App SDK. The goal is to fetch interval data for conversation duration over the last hour. I am using the POST /api/v2/analytics/conversations/aggregates endpoint.

Here is the JSON payload I am sending:

{
 "interval": "PT1H",
 "dateFrom": "2023-10-27T10:00:00.000Z",
 "dateTo": "2023-10-27T11:00:00.000Z",
 "groupings": [
 {
 "id": "mediaType",
 "type": "string"
 }
 ],
 "select": [
 {
 "id": "duration",
 "type": "sum"
 }
 ],
 "where": {
 "id": "mediaType",
 "type": "string",
 "value": "voice"
 }
}

The response is a 400 Bad Request with this body:

{
 "code": "bad_request",
 "message": "Invalid query",
 "status": 400
}

I have checked the documentation and the field names look correct. The interval is ISO 8601 duration format. The dateFrom and dateTo are ISO 8601 timestamps. I am using the same OAuth token that works for other endpoints like /api/v2/users/me.

Is there something wrong with the where clause structure? Or maybe the select array format? I have tried removing the where clause but it still fails. The error message is not helpful. I don’t know what part of the query is invalid.

Any ideas?

The groupings array is malformed in your payload. You left it cut off, but even if completed, the Analytics Aggregates API is picky about the structure. You need to define the id and optionally label for each grouping. If you’re missing the metrics array entirely, that’s likely why you’re getting the 400. The API doesn’t know what to calculate if you don’t ask for it.

Here’s a minimal working example for conversation duration. Notice the metrics array is mandatory.

{
 "interval": "PT1H",
 "dateFrom": "2023-10-27T10:00:00.000Z",
 "dateTo": "2023-10-27T11:00:00.000Z",
 "groupings": [
 {
 "id": "mediaType"
 }
 ],
 "metrics": [
 {
 "id": "conversationDuration",
 "type": "sum"
 }
 ]
}

Also, double-check your date format. ISO 8601 with ‘Z’ is correct, but ensure the dateFrom isn’t in the future relative to your system clock. The SDK usually handles this, but raw HTTP calls are stricter. Try hitting the endpoint via Postman first to isolate SDK issues.

Got it. The missing metrics array was the culprit. I added ["conversationDuration"] and the 400 error vanished.