---
title: Aggregate cases
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > API Reference > Case Management
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Aggregate cases{% #aggregate-cases %}
Copy pageCopied
{% tab title="v2" %}
**Note**: This endpoint is in preview and is subject to change. If you have any feedback, contact [Datadog support](https://docs.datadoghq.com/help/).
| Datadog site      | API endpoint                                              |
| ----------------- | --------------------------------------------------------- |
| ap1.datadoghq.com | POST https://api.ap1.datadoghq.com/api/v2/cases/aggregate |
| ap2.datadoghq.com | POST https://api.ap2.datadoghq.com/api/v2/cases/aggregate |
| app.datadoghq.eu  | POST https://api.datadoghq.eu/api/v2/cases/aggregate      |
| app.ddog-gov.com  | POST https://api.ddog-gov.com/api/v2/cases/aggregate      |
| us2.ddog-gov.com  | POST https://api.us2.ddog-gov.com/api/v2/cases/aggregate  |
| uk1.datadoghq.com | POST https://api.uk1.datadoghq.com/api/v2/cases/aggregate |
| app.datadoghq.com | POST https://api.datadoghq.com/api/v2/cases/aggregate     |
| us3.datadoghq.com | POST https://api.us3.datadoghq.com/api/v2/cases/aggregate |
| us5.datadoghq.com | POST https://api.us5.datadoghq.com/api/v2/cases/aggregate |

### Overview

Performs an aggregation query over cases, grouping results by specified fields and returning counts per group along with a total. Useful for dashboards and analytics.

OAuth apps require the `cases_read` authorization [scope](https://docs.datadoghq.com/api/latest/scopes.md#case-management) to access this endpoint.



### Request

#### Body Data (required)

Case aggregate request payload.

{% tab title="Model" %}

| Parent field | Field                          | Type     | Description                                                                                                                   |
| ------------ | ------------------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------- |
|              | data [*required*]         | object   | Data object wrapping the aggregation query type and attributes.                                                               |
| data         | attributes [*required*]   | object   | Attributes for the aggregation request, including the search query and grouping configuration.                                |
| attributes   | group_by [*required*]     | object   | Configuration for grouping aggregated results by one or more case fields.                                                     |
| group_by     | groups [*required*]       | [string] | Fields to group by.                                                                                                           |
| group_by     | limit [*required*]        | int32    | Maximum number of groups to return.                                                                                           |
| attributes   | query_filter [*required*] | string   | A search query to filter which cases are included in the aggregation. Uses the same syntax as the Case Management search bar. |
| data         | type [*required*]         | enum     | JSON:API resource type for case aggregation requests. Allowed enum values: `aggregate`                                        |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "group_by": {
        "groups": [
          "status"
        ],
        "limit": 14
      },
      "query_filter": "service:case-api"
    },
    "type": "aggregate"
  }
}
```

{% /tab %}

### Response

{% tab title="200" %}
OK
{% tab title="Model" %}
Response containing aggregated case counts grouped by the requested fields.

| Parent field | Field                        | Type     | Description                                                                                                     |
| ------------ | ---------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
|              | data [*required*]       | object   | Data object containing the aggregation results, including total count and per-group breakdowns.                 |
| data         | attributes [*required*] | object   | Attributes of the aggregation result, including the total count across all groups and the per-group breakdowns. |
| attributes   | groups [*required*]     | [object] | Aggregated groups.                                                                                              |
| groups       | group [*required*]      | string   | The value of the field being grouped on (for example, `OPEN` when grouping by status).                          |
| groups       | value [*required*]      | [number] | The count of cases in this group.                                                                               |
| attributes   | total [*required*]      | double   | Total count of aggregated cases.                                                                                |
| data         | id [*required*]         | string   | Aggregate response identifier.                                                                                  |
| data         | type [*required*]       | string   | Aggregate resource type.                                                                                        |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "groups": [
        {
          "group": "OPEN",
          "value": [
            42
          ]
        }
      ],
      "total": 100
    },
    "id": "agg-result-001",
    "type": "aggregate"
  }
}
```

{% /tab %}

{% /tab %}

{% tab title="400" %}
Bad Request
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="401" %}
Unauthorized
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="403" %}
Forbidden
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="404" %}
Not Found
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="429" %}
Too many requests
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

### Code Example

##### 
                  \## default
# 
 \# Curl command curl -X POST "https://api.datadoghq.com/api/v2/cases/aggregate" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" \
-d @- << EOF
{
  "data": {
    "attributes": {
      "group_by": {
        "groups": [
          "status"
        ],
        "limit": 14
      },
      "query_filter": "service:case-api"
    },
    "type": "aggregate"
  }
}
EOF 
                
##### 

```python
"""
Aggregate cases returns "OK" response
"""

from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v2.api.case_management_api import CaseManagementApi
from datadog_api_client.v2.model.case_aggregate_group_by import CaseAggregateGroupBy
from datadog_api_client.v2.model.case_aggregate_request import CaseAggregateRequest
from datadog_api_client.v2.model.case_aggregate_request_attributes import CaseAggregateRequestAttributes
from datadog_api_client.v2.model.case_aggregate_request_data import CaseAggregateRequestData
from datadog_api_client.v2.model.case_aggregate_resource_type import CaseAggregateResourceType

body = CaseAggregateRequest(
    data=CaseAggregateRequestData(
        attributes=CaseAggregateRequestAttributes(
            group_by=CaseAggregateGroupBy(
                groups=[
                    "status",
                ],
                limit=14,
            ),
            query_filter="service:case-api",
        ),
        type=CaseAggregateResourceType.AGGREGATE,
    ),
)

configuration = Configuration()
configuration.unstable_operations["aggregate_cases"] = True
with ApiClient(configuration) as api_client:
    api_instance = CaseManagementApi(api_client)
    response = api_instance.aggregate_cases(body=body)

    print(response)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=python) and then save the example to `example.py` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" python3 "example.py"
##### 

```ruby
# Aggregate cases returns "OK" response

require "datadog_api_client"
DatadogAPIClient.configure do |config|
  config.unstable_operations["v2.aggregate_cases".to_sym] = true
end
api_instance = DatadogAPIClient::V2::CaseManagementAPI.new

body = DatadogAPIClient::V2::CaseAggregateRequest.new({
  data: DatadogAPIClient::V2::CaseAggregateRequestData.new({
    attributes: DatadogAPIClient::V2::CaseAggregateRequestAttributes.new({
      group_by: DatadogAPIClient::V2::CaseAggregateGroupBy.new({
        groups: [
          "status",
        ],
        limit: 14,
      }),
      query_filter: "service:case-api",
    }),
    type: DatadogAPIClient::V2::CaseAggregateResourceType::AGGREGATE,
  }),
})
p api_instance.aggregate_cases(body)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=ruby) and then save the example to `example.rb` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" rb "example.rb"
##### 

```go
// Aggregate cases returns "OK" response

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
	"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)

func main() {
	body := datadogV2.CaseAggregateRequest{
		Data: datadogV2.CaseAggregateRequestData{
			Attributes: datadogV2.CaseAggregateRequestAttributes{
				GroupBy: datadogV2.CaseAggregateGroupBy{
					Groups: []string{
						"status",
					},
					Limit: 14,
				},
				QueryFilter: "service:case-api",
			},
			Type: datadogV2.CASEAGGREGATERESOURCETYPE_AGGREGATE,
		},
	}
	ctx := datadog.NewDefaultContext(context.Background())
	configuration := datadog.NewConfiguration()
	configuration.SetUnstableOperationEnabled("v2.AggregateCases", true)
	apiClient := datadog.NewAPIClient(configuration)
	api := datadogV2.NewCaseManagementApi(apiClient)
	resp, r, err := api.AggregateCases(ctx, body)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `CaseManagementApi.AggregateCases`: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}

	responseContent, _ := json.MarshalIndent(resp, "", "  ")
	fmt.Fprintf(os.Stdout, "Response from `CaseManagementApi.AggregateCases`:\n%s\n", responseContent)
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=go) and then save the example to `main.go` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" go run "main.go"
##### 

```java
// Aggregate cases returns "OK" response

import com.datadog.api.client.ApiClient;
import com.datadog.api.client.ApiException;
import com.datadog.api.client.v2.api.CaseManagementApi;
import com.datadog.api.client.v2.model.CaseAggregateGroupBy;
import com.datadog.api.client.v2.model.CaseAggregateRequest;
import com.datadog.api.client.v2.model.CaseAggregateRequestAttributes;
import com.datadog.api.client.v2.model.CaseAggregateRequestData;
import com.datadog.api.client.v2.model.CaseAggregateResourceType;
import com.datadog.api.client.v2.model.CaseAggregateResponse;
import java.util.Collections;

public class Example {
  public static void main(String[] args) {
    ApiClient defaultClient = ApiClient.getDefaultApiClient();
    defaultClient.setUnstableOperationEnabled("v2.aggregateCases", true);
    CaseManagementApi apiInstance = new CaseManagementApi(defaultClient);

    CaseAggregateRequest body =
        new CaseAggregateRequest()
            .data(
                new CaseAggregateRequestData()
                    .attributes(
                        new CaseAggregateRequestAttributes()
                            .groupBy(
                                new CaseAggregateGroupBy()
                                    .groups(Collections.singletonList("status"))
                                    .limit(14))
                            .queryFilter("service:case-api"))
                    .type(CaseAggregateResourceType.AGGREGATE));

    try {
      CaseAggregateResponse result = apiInstance.aggregateCases(body);
      System.out.println(result);
    } catch (ApiException e) {
      System.err.println("Exception when calling CaseManagementApi#aggregateCases");
      System.err.println("Status code: " + e.getCode());
      System.err.println("Reason: " + e.getResponseBody());
      System.err.println("Response headers: " + e.getResponseHeaders());
      e.printStackTrace();
    }
  }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=java) and then save the example to `Example.java` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" java "Example.java"
##### 

```rust
// Aggregate cases returns "OK" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_case_management::CaseManagementAPI;
use datadog_api_client::datadogV2::model::CaseAggregateGroupBy;
use datadog_api_client::datadogV2::model::CaseAggregateRequest;
use datadog_api_client::datadogV2::model::CaseAggregateRequestAttributes;
use datadog_api_client::datadogV2::model::CaseAggregateRequestData;
use datadog_api_client::datadogV2::model::CaseAggregateResourceType;

#[tokio::main]
async fn main() {
    let body = CaseAggregateRequest::new(CaseAggregateRequestData::new(
        CaseAggregateRequestAttributes::new(
            CaseAggregateGroupBy::new(vec!["status".to_string()], 14),
            "service:case-api".to_string(),
        ),
        CaseAggregateResourceType::AGGREGATE,
    ));
    let mut configuration = datadog::Configuration::new();
    configuration.set_unstable_operation_enabled("v2.AggregateCases", true);
    let api = CaseManagementAPI::with_config(configuration);
    let resp = api.aggregate_cases(body).await;
    if let Ok(value) = resp {
        println!("{:#?}", value);
    } else {
        println!("{:#?}", resp.unwrap_err());
    }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=rust) and then save the example to `src/main.rs` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" cargo run
##### 

```typescript
/**
 * Aggregate cases returns "OK" response
 */

import { client, v2 } from "@datadog/datadog-api-client";

const configuration = client.createConfiguration();
configuration.unstableOperations["v2.aggregateCases"] = true;
const apiInstance = new v2.CaseManagementApi(configuration);

const params: v2.CaseManagementApiAggregateCasesRequest = {
  body: {
    data: {
      attributes: {
        groupBy: {
          groups: ["status"],
          limit: 14,
        },
        queryFilter: "service:case-api",
      },
      type: "aggregate",
    },
  },
};

apiInstance
  .aggregateCases(params)
  .then((data: v2.CaseAggregateResponse) => {
    console.log(
      "API called successfully. Returned data: " + JSON.stringify(data)
    );
  })
  .catch((error: any) => console.error(error));
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=typescript) and then save the example to `example.ts` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" tsc "example.ts"
{% /tab %}
