
## Aggregate funnels

`POST /v1/deal-categories/aggregate`

Counts funnels matching a filter and computes numeric aggregates over them. Without a request body it returns the total number of funnels in a single call, without fetching records.

## Request fields (body)

| Parameter | Type | Required | Description |
|----------|-----|:-----:|---------|
| `aggregate` | array | no | Array of aggregations. Each element: `{ "field": "sort", "function": "sum" }`. Functions: `count`, `sum`, `avg`, `min`, `max`. For `count`, the field is `"*"`. Without the array only `count` is returned |
| `filter` | object | no | Exact match and `$in` on `id`, `name`, `sort`. Filtering by `isLocked` or `createdAt` returns `400 UNSUPPORTED_FILTER`. [Filtering syntax](/docs/filtering) |
| `groupBy` | string or string[] | no | A user-defined field name or an array of up to 5 user-defined field names returned by the fields endpoint. The schema has no standard grouping fields |

Numeric functions (`sum`/`avg`/`min`/`max`) apply to numeric funnel fields — `sort`. The schema declares no standard fields for grouping. `groupBy` accepts user-defined fields returned by the fields endpoint; other fields return `400 INVALID_PARAMS`.

## Examples

### curl — personal key

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/deal-categories/aggregate" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

### curl — OAuth application

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/deal-categories/aggregate" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/deal-categories/aggregate', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({}),
})

const { success, data } = await res.json()
console.log('Total funnels:', data.count)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/deal-categories/aggregate', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({}),
})

const { success, data } = await res.json()
```

## Other scenarios

To count records, use `count` with the field set to `"*"`. This is the fastest request because it does not fetch records. Without the `aggregate` array the result is the same:

```json
{ "aggregate": [{ "field": "*", "function": "count" }] }
```

Sum of the `sort` field across all funnels:

```json
{
  "aggregate": [{ "field": "sort", "function": "sum" }]
}
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data` | object | Aggregation result: record count, function values and selection details |
| `data.count` | number | Total number of funnels matching the filter |
| `data.aggregates` | object | Results of numeric aggregations by field, in the form `{ "sort": { "sum": 2410 } }`. Empty object if the `aggregate` array is not passed |
| `data.meta.totalRecords` | number | Total number of funnels matching the filter |
| `data.meta.recordsProcessed` | number | How many records were processed: `0` for a plain `count`, the number of records for numeric functions |
| `data.meta.truncated` | boolean | `true` when the numbers were computed over only some of the records that match the filter: fewer records were read than `data.count` promised — including when more than 5000 matched the filter — or the slice was cut short by a sub-page error. The size of the gap is reported in `data.meta.recordsShortfall`, the interrupted slice in `data.meta.pageErrorSample`. Always present, and `false` on a complete response. If the request has neither `groupBy` nor a numeric function, no records are fetched and the flag is always `false` |
| `data.meta.recordsShortfall` | number | How many funnels matching the filter were left out of the calculation: `totalRecords` minus `recordsProcessed`. Present only when fewer records were read than `data.count` promised |
| `data.meta.pageErrorSample` | object | The sub-page error that cut the slice short: `code` and `message`. Present only when the slice was cut short |
| `data.meta.warnings` | array | An array with a single warning: `code` is `AGGREGATE_TRUNCATED`, `message` is the text. Present only together with `data.meta.truncated: true` |

## Response example

Count request (body `{}`):

```json
{
  "success": true,
  "data": {
    "count": 6,
    "aggregates": {},
    "meta": {
      "totalRecords": 6,
      "recordsProcessed": 0,
      "truncated": false
    }
  }
}
```

Sum of the `sort` field (body `{ "aggregate": [{ "field": "sort", "function": "sum" }] }`):

```json
{
  "success": true,
  "data": {
    "count": 6,
    "aggregates": { "sort": { "sum": 2410 } },
    "meta": {
      "totalRecords": 6,
      "recordsProcessed": 6,
      "truncated": false
    }
  }
}
```

## Error response example

400 — `groupBy` was passed when no user-defined grouping fields were available:

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "groupBy is not supported on this entity"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | Unsupported `groupBy` field or a numeric function on a non-numeric field |
| 403 | `SCOPE_DENIED` | The API key lacks the `crm` scope |
| 401 | `TOKEN_MISSING` | The API key has no configured tokens |
| 429 | `RATE_LIMITED` | Rate limit exceeded: 300 requests per minute per portal. All API keys of the portal share one limit. The current limit is returned in the `x-ratelimit-limit` header. Retry after the delay in the `Retry-After` header |

For the full list of common API errors, see [Errors](/docs/errors).

## Known specifics

**The truncation marker travels with the number itself.** When a response arrives with `meta.truncated: true`, the marker `truncated: true` sits inside every field object in `data.aggregates`, and `data.meta.warnings` gains a warning with the code `AGGREGATE_TRUNCATED`. A client that reads only the number itself therefore sees that it was computed over only some of the records. None of these markers appear on a complete response. For full details, see [Aggregation POST — the 5000-record ceiling](/docs/entity-api#aggregation-post-the-5000-record-ceiling).

## See also

- [List funnels](/docs/entities/deal-categories/list)
- [Search funnels](/docs/entities/deal-categories/search)
- [Filtering syntax](/docs/filtering)
