[{"data":1,"prerenderedAt":7},["ShallowReactive",2],{"docs-entities\u002Fshipments\u002Faggregate":3,"docs-tabs-entities\u002Fshipments\u002Faggregate":6},{"content":4,"lastmod":5},"## Shipment aggregation\n\n`POST \u002Fv1\u002Fshipments\u002Faggregate`\n\nCounts shipments and computes the sum, average, minimum and maximum of the delivery cost, with filtering and grouping by order or delivery service.\n\n## Standard fields\n\n| Field | Purpose |\n|------|------------|\n| `priceDelivery` | Delivery cost — the main field for `sum` \u002F `avg` \u002F `min` \u002F `max`. Also available for `groupBy` |\n| `basePriceDelivery`, `discountPrice` | Base delivery cost and delivery discount — numeric fields for `sum` \u002F `avg` \u002F `min` \u002F `max` |\n| `orderId` | Order ID — used in `groupBy` to break shipments down by order |\n| `deliveryId` | Delivery service ID — used in `groupBy` to break shipments down by service |\n\nThe fields available for `groupBy` are listed in the `aggregatable` array in [`GET \u002Fv1\u002Fshipments\u002Ffields`](.\u002Ffields.md). Numeric functions accept any field of type `number` from the same schema, including fields outside `aggregatable`.\n\n**User fields.** For a non-existent field name in `sum` \u002F `avg` \u002F `min` \u002F `max`, the `400 INVALID_PARAMS` response lists the allowed numeric fields. That list contains only standard shipment fields — `basePriceDelivery`, `companyId`, `deliveryId`, `discountPrice`, `empAllowDeliveryId`, `empCanceledId`, `empDeductedId`, `empMarkedId`, `empResponsibleId`, `id`, `orderId`, `priceDelivery`, `responsibleId`, with no user fields. A rejection for a field outside `aggregatable` in `groupBy` names only `orderId`, `priceDelivery`, `deliveryId` as allowed.\n\n## Request fields (body)\n\n| Field | Type | Required | Description |\n|------|-----|:-----:|---------|\n| `aggregate` | array | no | Array of expressions `{ \"field\": \"priceDelivery\", \"function\": \"sum\" }`, up to 5 per request. Functions: `count`, `sum`, `avg`, `min`, `max`. For `count` the field is `\"*\"`. Without the parameter, only `count` is returned |\n| `filter` | object | no | Filtering by the fields of [`GET \u002Fv1\u002Fshipments\u002Ffields`](.\u002Ffields.md).\u003Cbr>[Filtering syntax](\u002Fdocs\u002Ffiltering). Example: `{ \"orderId\": 1027 }` |\n| `groupBy` | string \\| string[] | no | A field or an array of fields to group by, up to 5 fields. Allowed values come from the `aggregatable` array: `orderId`, `priceDelivery`, `deliveryId` |\n| `groupOrderBy` | array | no | Group sorting: `[{ \"field\": \"priceDelivery:sum\", \"direction\": \"desc\" }]`. `field` accepts `count`, a field name from `groupBy`, or `\u003Cfield>:\u003Cfunction>` from `aggregate`. Works only together with `groupBy` |\n| `groupLimit` | number | no | Limits the number of returned groups, from 1 to 1000. Works only together with `groupBy` |\n\n## Examples\n\n### curl — personal key\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipments\u002Faggregate\" \\\n  -H \"X-Api-Key: YOUR_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\n    \"filter\": { \"orderId\": 1027 },\n    \"aggregate\": [\n      { \"field\": \"*\", \"function\": \"count\" },\n      { \"field\": \"priceDelivery\", \"function\": \"sum\" },\n      { \"field\": \"priceDelivery\", \"function\": \"avg\" }\n    ],\n    \"groupBy\": \"deliveryId\"\n  }'\n```\n\n### curl — OAuth application\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipments\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\": { \"orderId\": 1027 },\n    \"aggregate\": [\n      { \"field\": \"*\", \"function\": \"count\" },\n      { \"field\": \"priceDelivery\", \"function\": \"sum\" },\n      { \"field\": \"priceDelivery\", \"function\": \"avg\" }\n    ],\n    \"groupBy\": \"deliveryId\"\n  }'\n```\n\n### JavaScript — personal key\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipments\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: { orderId: 1027 },\n    aggregate: [\n      { field: '*', function: 'count' },\n      { field: 'priceDelivery', function: 'sum' },\n      { field: 'priceDelivery', function: 'avg' },\n    ],\n    groupBy: 'deliveryId',\n  }),\n})\n\nconst { success, data } = await res.json()\n\u002F\u002F data.groups — delivery cost by delivery service\nconsole.log(data.groups)\n```\n\n### JavaScript — OAuth application\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipments\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: { orderId: 1027 },\n    aggregate: [\n      { field: '*', function: 'count' },\n      { field: 'priceDelivery', function: 'sum' },\n      { field: 'priceDelivery', function: 'avg' },\n    ],\n    groupBy: 'deliveryId',\n  }),\n})\n\nconst { success, data } = await res.json()\n```\n\n## Other scenarios\n\nThe blocks below are request bodies.\n\nShipment count for an order only — without the `aggregate` array no records are fetched, and `data.meta.recordsProcessed` in the response is `0`:\n\n```json\n{ \"filter\": { \"orderId\": 1027 } }\n```\n\nGrouping by two fields — by order and delivery service:\n\n```json\n{\n  \"aggregate\": [{ \"field\": \"*\", \"function\": \"count\" }],\n  \"groupBy\": [\"orderId\", \"deliveryId\"]\n}\n```\n\nSum and average of the delivery cost without grouping:\n\n```json\n{\n  \"filter\": { \"orderId\": 1027 },\n  \"aggregate\": [\n    { \"field\": \"*\", \"function\": \"count\" },\n    { \"field\": \"priceDelivery\", \"function\": \"sum\" },\n    { \"field\": \"priceDelivery\", \"function\": \"avg\" }\n  ]\n}\n```\n\n## Response fields\n\n| Field | Type | Description |\n|------|-----|---------|\n| `success` | boolean | Always `true` on success |\n| `data.count` | number | Number of shipments matching the filter |\n| `data.aggregates` | object | Results of numeric functions by field: `{ \"priceDelivery\": { \"sum\": 500, \"avg\": 500 } }`. An empty object if `aggregate` contains only `count` |\n| `data.groups` | array | Present only with `groupBy`. Each element holds the grouping field values, `count` and `aggregates` for the group |\n| `data.meta.totalRecords` | number | Total number of records matching the filter |\n| `data.meta.recordsProcessed` | number | Number of processed records, up to 5000 |\n| `data.meta.truncated` | boolean | `true` if the numbers were not computed over all records matching 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 subpage error. The size of the shortfall arrives in `data.meta.recordsShortfall`, the cut-short slice in `data.meta.pageErrorSample`. Always present, `false` on a complete response. If the request has neither `groupBy` nor numeric functions, no records are fetched and the flag is always `false` |\n| `data.meta.groupTotal` | number | Present only with `groupBy` — the number of groups before `groupLimit` is applied |\n| `data.meta.groupsTruncated` | boolean | Present only with `groupBy`. `true` if the group list was cut by `groupLimit` |\n\n## Response example\n\n```json\n{\n  \"success\": true,\n  \"data\": {\n    \"count\": 1,\n    \"aggregates\": {\n      \"priceDelivery\": {\n        \"sum\": 500,\n        \"avg\": 500\n      }\n    },\n    \"groups\": [\n      {\n        \"deliveryId\": 1,\n        \"count\": 1,\n        \"aggregates\": {\n          \"priceDelivery\": {\n            \"sum\": 500,\n            \"avg\": 500\n          }\n        }\n      }\n    ],\n    \"meta\": {\n      \"totalRecords\": 1,\n      \"recordsProcessed\": 1,\n      \"truncated\": false,\n      \"groupTotal\": 1,\n      \"groupsTruncated\": false\n    }\n  }\n}\n```\n\n## Error response example\n\n400 — the field is not in the schema. The message lists the allowed numeric fields:\n\n```json\n{\n  \"success\": false,\n  \"error\": {\n    \"code\": \"INVALID_PARAMS\",\n    \"message\": \"Field 'weight' not found. Available numeric fields: basePriceDelivery, companyId, deliveryId, discountPrice, empAllowDeliveryId, empCanceledId, empDeductedId, empMarkedId, empResponsibleId, id, orderId, priceDelivery, responsibleId.\"\n  }\n}\n```\n\n## Errors\n\n| HTTP | Code | Description |\n|------|-----|---------|\n| 400 | `INVALID_PARAMS` | `count` is passed with a field other than `\"*\"` |\n| 400 | `INVALID_PARAMS` | `aggregate` is passed as a non-array or an empty array |\n| 400 | `INVALID_PARAMS` | `aggregate` has more than 5 expressions |\n| 400 | `INVALID_PARAMS` | Unknown function — the message lists the allowed ones |\n| 400 | `INVALID_PARAMS` | `sum` \u002F `avg` \u002F `min` \u002F `max` without a field or with the field `\"*\"` |\n| 400 | `INVALID_PARAMS` | The field for `sum` \u002F `avg` \u002F `min` \u002F `max` is not numeric, for example `trackingNumber` — the message names its type |\n| 400 | `INVALID_PARAMS` | The field for `sum` \u002F `avg` \u002F `min` \u002F `max` is not in the schema — the message contains the list of numeric fields |\n| 400 | `INVALID_PARAMS` | The `op`, `field` or `function` fields are passed at the top level of the body instead of the `aggregate` array |\n| 400 | `INVALID_PARAMS` | `groupBy` is neither a string nor an array of strings |\n| 400 | `INVALID_PARAMS` | A field in `groupBy` is outside the `aggregatable` array — the message contains the list of allowed ones |\n| 400 | `INVALID_PARAMS` | `groupBy` has more than 5 fields |\n| 400 | `INVALID_PARAMS` | `groupBy` contains a reserved name: `count`, `aggregates`, `meta`, `groups` |\n| 400 | `INVALID_PARAMS` | `groupOrderBy` or `groupLimit` is passed without `groupBy`, or its value is invalid |\n| 400 | `UNKNOWN_FILTER_FIELD` | Unknown field in `filter` — the message contains the list of shipment fields |\n| 400 | `INVALID_FILTER_FIELD` | A field name in `filter` starts with the unsupported prefix `@` or `!@` |\n| 400 | `INVALID_FILTER_OPERATOR` | Unsupported operator in `filter`, an empty object in a condition value, or the `$or` logical condition |\n| 400 | `INVALID_FILTER_SHAPE` | `filter` is not an object of conditions, for example a string, a number or an array |\n| 400 | `INVALID_DUPLICATE_FILTER_FIELD` | Two filter conditions reduce to one Bitrix24 condition, for example `$ne` and `$nin` on the same field. Pass only one of them |\n| 403 | `SCOPE_DENIED` | The API key has no `sale` scope |\n| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is not passed |\n| 401 | `TOKEN_MISSING` | The API key has no configured tokens |\n| 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 |\n\nFor the full list of common API errors, see [Errors](\u002Fdocs\u002Ferrors).\n\n## Known specifics\n\n**`count` versus numeric functions.** A request without `groupBy` and without numeric functions is handled by a single request to Bitrix24 — no records are fetched, and the response time does not depend on the number of shipments. As soon as the request has `sum` \u002F `avg` \u002F `min` \u002F `max` or `groupBy`, including together with a single `count`, up to 5000 records are loaded page by page, and the aggregates are computed by Vibecode. With `meta.truncated: true`, the `truncated: true` mark is also set inside the object of each field in `data.aggregates` and on each element of `data.groups`, and `data.meta.warnings` carries the `AGGREGATE_TRUNCATED` warning. For an exact number on a large selection, use a request without `groupBy` and numeric functions, or narrow the filter, for example by `orderId`. For a full explanation, see [Aggregation POST — the 5000-record ceiling](\u002Fdocs\u002Fentity-api#aggregation-post-the-5000-record-ceiling).\n\n## See also\n\n- [List shipments](.\u002Flist.md)\n- [Search shipments](.\u002Fsearch.md)\n- [Shipment fields](.\u002Ffields.md)\n- [Shipments](\u002Fdocs\u002Fentities\u002Fshipments)\n- [Filtering syntax](\u002Fdocs\u002Ffiltering)\n- [Limits and optimization](\u002Fdocs\u002Foptimization)\n","2026-10-23",{},1795415295585]