[{"data":1,"prerenderedAt":7},["ShallowReactive",2],{"docs-entities\u002Fperson-types\u002Faggregate":3,"docs-tabs-entities\u002Fperson-types\u002Faggregate":6},{"content":4,"lastmod":5},"## Aggregate payer types\n\n`POST \u002Fv1\u002Fperson-types\u002Faggregate`\n\nCounts payer types with filtering and grouping by activity or code, and calculates the sum, average, minimum, and maximum of numeric fields.\n\n## Standard fields\n\n| Field | Purpose |\n|------|------------|\n| `active` | Activity flag for `groupBy` |\n| `code` | Unique code for `groupBy` |\n| `sort` | Sort order: a numeric field for `sum`, `avg`, `min`, `max` |\n| `id` | Payer type ID from [List payer types](.\u002Flist.md). A numeric field for `sum`, `avg`, `min`, `max` |\n\nFields for `groupBy` are listed in `data.aggregatable` of [`GET \u002Fv1\u002Fperson-types\u002Ffields`](.\u002Ffields.md). Numeric functions accept fields with the `number` type from the same schema: `id` and `sort`.\n\n## Request body fields\n\n| Field | Type | Required | Description |\n|------|-----|:-----:|---------|\n| `aggregate` | array | no | An array of 1 to 5 expressions such as `{ \"field\": \"sort\", \"function\": \"sum\" }`. Functions: `count`, `sum`, `avg`, `min`, `max`. For `count`, the field is `\"*\"`. Defaults to `[{ \"field\": \"*\", \"function\": \"count\" }]` |\n| `filter` | object | no | Filter by fields from [`GET \u002Fv1\u002Fperson-types\u002Ffields`](.\u002Ffields.md).\u003Cbr>[Filtering syntax](\u002Fdocs\u002Ffiltering). Example: `{ \"id\": 21 }` |\n| `groupBy` | string \\| string[] | no | A field or array of up to 5 grouping fields. Allowed fields: `active`, `code` |\n| `groupOrderBy` | array | no | Group sorting: `[{ \"field\": \"count\", \"direction\": \"desc\" }]`. The `field` is `count`, a field from `groupBy`, or `\u003Cfield>:\u003Cfunction>` from `aggregate`. Directions: `asc`, `desc`. Defaults to `desc` when `direction` is omitted. Requires `groupBy` |\n| `groupLimit` | number | no | Number of groups to return: an integer from 1 to 1000. If omitted, groups are not truncated. Requires `groupBy` |\n\n## Examples\n\n### curl — personal key\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fperson-types\u002Faggregate\" \\\n  -H \"X-Api-Key: YOUR_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\n    \"filter\": { \"id\": 21 },\n    \"aggregate\": [{ \"field\": \"*\", \"function\": \"count\" }],\n    \"groupBy\": [\"active\", \"code\"],\n    \"groupOrderBy\": [{ \"field\": \"count\", \"direction\": \"desc\" }],\n    \"groupLimit\": 10\n  }'\n```\n\n### curl — OAuth application\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fperson-types\u002Faggregate\" \\\n  -H \"X-Api-Key: YOUR_APP_KEY\" \\\n  -H \"Authorization: Bearer USER_SESSION_TOKEN\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\n    \"filter\": { \"id\": 21 },\n    \"aggregate\": [{ \"field\": \"*\", \"function\": \"count\" }],\n    \"groupBy\": [\"active\", \"code\"],\n    \"groupOrderBy\": [{ \"field\": \"count\", \"direction\": \"desc\" }],\n    \"groupLimit\": 10\n  }'\n```\n\n### JavaScript — personal key\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fperson-types\u002Faggregate', {\n  method: 'POST',\n  headers: {\n    'X-Api-Key': 'YOUR_API_KEY',\n    'Content-Type': 'application\u002Fjson',\n  },\n  body: JSON.stringify({\n    filter: { id: 21 },\n    aggregate: [{ field: '*', function: 'count' }],\n    groupBy: ['active', 'code'],\n    groupOrderBy: [{ field: 'count', direction: 'desc' }],\n    groupLimit: 10,\n  }),\n})\n\nconst { data } = await res.json()\nconsole.log('Payer type counts by activity and code:', data.groups)\n```\n\n### JavaScript — OAuth application\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fperson-types\u002Faggregate', {\n  method: 'POST',\n  headers: {\n    'X-Api-Key': 'YOUR_APP_KEY',\n    'Authorization': 'Bearer USER_SESSION_TOKEN',\n    'Content-Type': 'application\u002Fjson',\n  },\n  body: JSON.stringify({\n    filter: { id: 21 },\n    aggregate: [{ field: '*', function: 'count' }],\n    groupBy: ['active', 'code'],\n    groupOrderBy: [{ field: 'count', direction: 'desc' }],\n    groupLimit: 10,\n  }),\n})\n\nconst { data } = await res.json()\n```\n\n## Other scenarios\n\nThe blocks below are request bodies.\n\nCount types matching the filter without grouping or numeric functions. Records are not fetched:\n\n```json\n{ \"filter\": { \"id\": 21 } }\n```\n\nSum, average, minimum, and maximum sort order:\n\n```json\n{\n  \"filter\": { \"id\": 21 },\n  \"aggregate\": [\n    { \"field\": \"*\", \"function\": \"count\" },\n    { \"field\": \"sort\", \"function\": \"sum\" },\n    { \"field\": \"sort\", \"function\": \"avg\" },\n    { \"field\": \"sort\", \"function\": \"min\" },\n    { \"field\": \"sort\", \"function\": \"max\" }\n  ]\n}\n```\n\nSum of sort order grouped by activity when the filter matches no records:\n\n```json\n{\n  \"filter\": { \"id\": 21, \"code\": \"ABSENT_DOCS_PERSON_397F55D8FB1D\" },\n  \"aggregate\": [{ \"field\": \"sort\", \"function\": \"sum\" }],\n  \"groupBy\": \"active\"\n}\n```\n\nFor the last request, `data.count` and the sum are `0`, and `data.groups` is an empty array.\n\n## Response fields\n\n| Field | Type | Description |\n|------|-----|---------|\n| `success` | boolean | Always `true` on success |\n| `data.count` | number | Total number of types matching the filter |\n| `data.aggregates` | object | Numeric results by field and function, for example `{ \"sort\": { \"sum\": 900 } }`. An empty object when only `count` is used |\n| `data.aggregates.\u003Cfield>.truncated` | boolean | Included as `true` when numeric results are calculated from an incomplete set of records |\n| `data.groups` | array | Included with `groupBy`. Each group contains grouping field values, `count`, and `aggregates`. The `active` value is a `boolean`; the `code` value is a `string` or `null` |\n| `data.groups[].count` | number | Number of processed records in the group |\n| `data.groups[].aggregates` | object | Numeric results within the group, in the same format as `data.aggregates` |\n| `data.groups[].truncated` | boolean | Included as `true` for an incomplete set of records. Group counts and calculations apply to processed records |\n| `data.meta.totalRecords` | number | Total number of records matching the filter |\n| `data.meta.recordsProcessed` | number | Number of processed records, up to 5000. `0` without grouping or numeric functions |\n| `data.meta.truncated` | boolean | `true` if fewer records were read than the total count, including because of the 5000-record ceiling, or a subpage error stopped the read. `false` for a complete response. Always `false` without grouping or numeric functions |\n| `data.meta.recordsShortfall` | number | Number of unread records: `totalRecords - recordsProcessed`. Included when there is a shortfall, including one caused by the 5000-record ceiling |\n| `data.meta.pageErrorSample` | object | A sample subpage error. Included when an error stopped the read |\n| `data.meta.warnings` | array | Contains a warning with the code `AGGREGATE_TRUNCATED` for an incomplete set of records |\n| `data.meta.groupTotal` | number | Included with `groupBy`: the number of groups before applying `groupLimit` |\n| `data.meta.groupsTruncated` | boolean | Included with `groupBy`. `true` if groups were truncated by `groupLimit` |\n\n## Response example\n\n```json\n{\n  \"success\": true,\n  \"data\": {\n    \"count\": 1,\n    \"aggregates\": {},\n    \"groups\": [\n      {\n        \"active\": false,\n        \"code\": \"DOCS_PERSON_397F55D8FB1D\",\n        \"count\": 1,\n        \"aggregates\": {}\n      }\n    ],\n    \"meta\": {\n      \"totalRecords\": 1,\n      \"recordsProcessed\": 1,\n      \"truncated\": false,\n      \"groupTotal\": 1,\n      \"groupsTruncated\": false\n    }\n  }\n}\n```\n\nWithout `groupBy`, the response omits `data.groups`.\n\n## Error response example\n\n400 — the string field `name` was passed to the numeric function `sum`:\n\n```json\n{\n  \"success\": false,\n  \"error\": {\n    \"code\": \"INVALID_PARAMS\",\n    \"message\": \"Field 'name' is not numeric (type: string). Only number fields support sum.\"\n  }\n}\n```\n\n## Errors\n\n| HTTP | Code | Description |\n|------|-----|---------|\n| 400 | `INVALID_PARAMS` | The `aggregate` is not an array or is an empty array |\n| 400 | `INVALID_PARAMS` | The `aggregate` contains more than 5 expressions |\n| 400 | `INVALID_PARAMS` | The function is unknown or missing |\n| 400 | `INVALID_PARAMS` | The field for `count` differs from `\"*\"` |\n| 400 | `INVALID_PARAMS` | The field for `sum`, `avg`, `min`, or `max` is missing or is `\"*\"` |\n| 400 | `INVALID_PARAMS` | A numeric function received a non-numeric field, such as `name` |\n| 400 | `INVALID_PARAMS` | The numeric function field was not found. The message lists the allowed fields: `id`, `sort` |\n| 400 | `INVALID_PARAMS` | The `op`, `field`, or `function` was provided at the root without an `aggregate` array |\n| 400 | `INVALID_PARAMS` | The `groupBy` is neither a string nor an array of strings, or the array contains a value of another type |\n| 400 | `INVALID_PARAMS` | The grouping field is neither `active` nor `code` |\n| 400 | `INVALID_PARAMS` | The `groupBy` contains more than 5 fields |\n| 400 | `INVALID_PARAMS` | The `groupBy` contains a reserved name: `count`, `aggregates`, `meta`, or `groups` |\n| 400 | `INVALID_PARAMS` | The `groupOrderBy` was provided without `groupBy` |\n| 400 | `INVALID_PARAMS` | The `groupOrderBy` is not an array, an item has no string `field`, the field is absent from grouping or aggregate expressions, or `direction` is neither `asc` nor `desc` |\n| 400 | `INVALID_PARAMS` | The `groupLimit` was provided without `groupBy` |\n| 400 | `INVALID_PARAMS` | The `groupLimit` is not an integer from 1 to 1000 |\n| 400 | `UNKNOWN_FILTER_FIELD` | An unknown field in `filter` |\n| 400 | `INVALID_FILTER_FIELD` | A field name in `filter` starts with the unsupported prefix `@` or `!@` |\n| 400 | `INVALID_FILTER_OPERATOR` | An unsupported operator, an empty condition object, or logical conditions with `$or` \u002F `$and` |\n| 400 | `INVALID_FILTER_SHAPE` | The `filter` is not a condition object, such as a string, number, or array |\n| 400 | `INVALID_DUPLICATE_FILTER_FIELD` | Two filter conditions resolve to the same condition, such as `$ne` and `$nin` for one field |\n| 422 | `AGGREGATION_LIMIT_EXCEEDED` | With the limit enabled for the account, more than 5000 records match the filter and the request requires numeric calculations or grouping. No records are fetched. Narrow the filter |\n| 403 | `SCOPE_DENIED` | The API key lacks the `sale` scope |\n| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |\n| 401 | `TOKEN_MISSING` | The API key has no configured tokens |\n| 429 | `RATE_LIMITED` | Request limit exceeded: 300 per minute per portal, shared by all API keys for the portal. The exact value is in the `x-ratelimit-limit` header. Retry after the delay in the `Retry-After` header |\n\nFor the full list of common API errors, see [Error codes](\u002Fdocs\u002Ferrors).\n\n## Known specifics\n\nA request without grouping or numeric functions counts records without fetching them. Numeric calculations and any grouping, including grouping with only `count`, are subject to a 5000-record limit. If the account is configured to reject requests above this limit and more records match the filter, the request returns `422 AGGREGATION_LIMIT_EXCEEDED` without fetching them. Otherwise, records are read page by page, up to 5000. For an exact count over a large set, use a request without grouping or numeric functions. Narrow the filter to calculate over the entire set. See [Aggregation POST — the 5000-record ceiling](\u002Fdocs\u002Fentity-api#aggregation-post-the-5000-record-ceiling).\n\n## See also\n\n- [List payer types](.\u002Flist.md)\n- [Search payer types](.\u002Fsearch.md)\n- [Payer type fields](.\u002Ffields.md)\n- [Payer types](\u002Fdocs\u002Fentities\u002Fperson-types)\n- [Filtering syntax](\u002Fdocs\u002Ffiltering)\n- [Limits and optimization](\u002Fdocs\u002Foptimization)\n","2026-10-09",{},1795415278451]