[{"data":1,"prerenderedAt":7},["ShallowReactive",2],{"docs-entities\u002Fshipment-items\u002Faggregate":3,"docs-tabs-entities\u002Fshipment-items\u002Faggregate":6},{"content":4,"lastmod":5},"## Aggregate shipment items\n\n`POST \u002Fv1\u002Fshipment-items\u002Faggregate`\n\nCounts shipment items and computes the sum, average, minimum and maximum of the product quantity, with filtering and grouping by shipment or basket item.\n\n## Standard fields\n\n| Field | Purpose |\n|------|------------|\n| `quantity` | Product quantity in the item — the main field for `sum` \u002F `avg` \u002F `min` \u002F `max`. Also available for `groupBy` |\n| `reservedQuantity` | Reserved quantity — a numeric field for `sum` \u002F `avg` \u002F `min` \u002F `max` and `groupBy` |\n| `orderDeliveryId` | Shipment ID from [`GET \u002Fv1\u002Fshipments`](\u002Fdocs\u002Fentities\u002Fshipments\u002Flist) — used in `groupBy` to break items down by shipment |\n| `basketId` | Basket item ID from [`GET \u002Fv1\u002Fbasket-items`](\u002Fdocs\u002Fentities\u002Fbasket-items\u002Flist) — used in `groupBy` to break items down by basket product |\n\nThe fields allowed in `groupBy` are listed in the `aggregatable` array of [`GET \u002Fv1\u002Fshipment-items\u002Ffields`](.\u002Ffields.md). Numeric functions accept any field of type `number` from the same schema, including `id`, which is outside `aggregatable`.\n\n**User fields.** If you pass a nonexistent field to `sum` \u002F `avg` \u002F `min` \u002F `max`, the `400 INVALID_PARAMS` response lists the allowed numeric fields. That list contains only the standard shipment item fields — `basketId`, `id`, `orderDeliveryId`, `quantity`, `reservedQuantity`. It contains no user fields.\n\n## Request fields (body)\n\n| Field | Type | Required | Description |\n|------|-----|:-----:|---------|\n| `aggregate` | array | no | Array of expressions `{ \"field\": \"quantity\", \"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\u002Fshipment-items\u002Ffields`](.\u002Ffields.md).\u003Cbr>[Filtering syntax](\u002Fdocs\u002Ffiltering). Example: `{ \"orderDeliveryId\": 985 }` |\n| `groupBy` | string \\| string[] | no | A field or array of fields to group by, up to 5 fields. Allowed values come from the `aggregatable` array: `quantity`, `reservedQuantity`, `orderDeliveryId`, `basketId` |\n| `groupOrderBy` | array | no | Group sorting: `[{ \"field\": \"quantity:sum\", \"direction\": \"desc\" }]`. The `field` takes `count`, a field name from `groupBy`, or `\u003Cfield>:\u003Cfunction>` from `aggregate`. Works only together with `groupBy` |\n| `groupLimit` | number | no | Limit on 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\u002Fshipment-items\u002Faggregate\" \\\n  -H \"X-Api-Key: YOUR_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\n    \"filter\": { \"orderDeliveryId\": { \"$in\": [975, 985] } },\n    \"aggregate\": [\n      { \"field\": \"*\", \"function\": \"count\" },\n      { \"field\": \"quantity\", \"function\": \"sum\" },\n      { \"field\": \"quantity\", \"function\": \"avg\" }\n    ],\n    \"groupBy\": \"orderDeliveryId\"\n  }'\n```\n\n### curl — OAuth application\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipment-items\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\": { \"orderDeliveryId\": { \"$in\": [975, 985] } },\n    \"aggregate\": [\n      { \"field\": \"*\", \"function\": \"count\" },\n      { \"field\": \"quantity\", \"function\": \"sum\" },\n      { \"field\": \"quantity\", \"function\": \"avg\" }\n    ],\n    \"groupBy\": \"orderDeliveryId\"\n  }'\n```\n\n### JavaScript — personal key\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipment-items\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: { orderDeliveryId: { $in: [975, 985] } },\n    aggregate: [\n      { field: '*', function: 'count' },\n      { field: 'quantity', function: 'sum' },\n      { field: 'quantity', function: 'avg' },\n    ],\n    groupBy: 'orderDeliveryId',\n  }),\n})\n\nconst { success, data } = await res.json()\n\u002F\u002F data.groups — product quantity per shipment\nconsole.log(data.groups)\n```\n\n### JavaScript — OAuth application\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fshipment-items\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: { orderDeliveryId: { $in: [975, 985] } },\n    aggregate: [\n      { field: '*', function: 'count' },\n      { field: 'quantity', function: 'sum' },\n      { field: 'quantity', function: 'avg' },\n    ],\n    groupBy: 'orderDeliveryId',\n  }),\n})\n\nconst { success, data } = await res.json()\n```\n\nTo group by several fields, pass an array: `\"groupBy\": [\"orderDeliveryId\", \"basketId\"]`, up to 5 fields.\n\n## Other scenarios\n\nThe blocks below are request bodies.\n\nOnly the number of items in a shipment — without the `aggregate` array no records are loaded, and `data.meta.recordsProcessed` in the response is `0`:\n\n```json\n{ \"filter\": { \"orderDeliveryId\": 985 } }\n```\n\nShipments with the largest product quantity — the first two groups by descending sum of `quantity`:\n\n```json\n{\n  \"aggregate\": [{ \"field\": \"quantity\", \"function\": \"sum\" }],\n  \"groupBy\": \"orderDeliveryId\",\n  \"groupOrderBy\": [{ \"field\": \"quantity:sum\", \"direction\": \"desc\" }],\n  \"groupLimit\": 2\n}\n```\n\nTotal and maximum product quantity across all items, without grouping:\n\n```json\n{\n  \"aggregate\": [\n    { \"field\": \"*\", \"function\": \"count\" },\n    { \"field\": \"quantity\", \"function\": \"sum\" },\n    { \"field\": \"quantity\", \"function\": \"max\" }\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 shipment items matching the filter |\n| `data.aggregates` | object | Results of numeric functions by field: `{ \"quantity\": { \"sum\": 16, \"avg\": 5.33 } }`. An empty object if `aggregate` contains only `count`: the `count` result comes in `data.count` and `data.groups[].count`, not here |\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 records processed, 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 shortfall is reported in `data.meta.recordsShortfall`, and the interrupted slice in `data.meta.pageErrorSample`. Always present, `false` on a complete response. If the request has neither `groupBy` nor numeric functions, no records are loaded 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\": 3,\n    \"aggregates\": {\n      \"quantity\": {\n        \"sum\": 16,\n        \"avg\": 5.333333333333333\n      }\n    },\n    \"groups\": [\n      {\n        \"orderDeliveryId\": 975,\n        \"count\": 1,\n        \"aggregates\": {\n          \"quantity\": {\n            \"sum\": 1,\n            \"avg\": 1\n          }\n        }\n      },\n      {\n        \"orderDeliveryId\": 985,\n        \"count\": 2,\n        \"aggregates\": {\n          \"quantity\": {\n            \"sum\": 15,\n            \"avg\": 7.5\n          }\n        }\n      }\n    ],\n    \"meta\": {\n      \"totalRecords\": 3,\n      \"recordsProcessed\": 3,\n      \"truncated\": false,\n      \"groupTotal\": 2,\n      \"groupsTruncated\": false\n    }\n  }\n}\n```\n\nWithout `groupBy`, the `data.groups`, `data.meta.groupTotal` and `data.meta.groupsTruncated` fields are absent from the response.\n\n## Error response example\n\n400 — the field is not in the schema, and 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: basketId, id, orderDeliveryId, quantity, reservedQuantity.\"\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` contains 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 `xmlId` — 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 `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, for example `xmlId` — the message contains the list of allowed ones |\n| 400 | `INVALID_PARAMS` | `groupBy` contains 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 the value of one of them is invalid |\n| 400 | `UNKNOWN_FILTER_FIELD` | Unknown field in `filter` — the message contains the list of shipment item 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` or a logical condition, for example `$or` |\n| 400 | `INVALID_FILTER_SHAPE` | `filter` is not passed as an object of conditions, for example as a string |\n| 400 | `INVALID_DUPLICATE_FILTER_FIELD` | Two filter conditions reduce to one Bitrix24 condition, for example `$ne` and `$nin` on the same field. Pass one of them |\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, and all API keys of the portal share one limit. The current limit is returned in the `x-ratelimit-limit` header. Retry after the period 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 runs as a single request to Bitrix24 — no records are loaded, and the response time does not depend on the number of items. As soon as the request contains `sum` \u002F `avg` \u002F `min` \u002F `max` or `groupBy`, including together with a single `count`, records are loaded page by page, at most 5000, and aggregated on the Vibecode side. With `meta.truncated: true`, the `truncated: true` marker is set both inside each field object 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 result set, use a request without `groupBy` and numeric functions, or narrow the filter, for example by `orderDeliveryId`. For full details, see [Aggregation POST — the 5000-record ceiling](\u002Fdocs\u002Fentity-api#aggregation-post-the-5000-record-ceiling).\n\n**The count includes items of the system shipment.** The selection includes the same records as the [item list](.\u002Flist.md), including items of the system shipment, which holds the unallocated remainder of the basket. Therefore, the sum of `quantity` for a basket item without a shipment filter equals the product quantity in the basket. To count only what has been shipped, filter by the `orderDeliveryId` of shipments from [`GET \u002Fv1\u002Fshipments`](\u002Fdocs\u002Fentities\u002Fshipments\u002Flist).\n\n## See also\n\n- [List shipment items](.\u002Flist.md)\n- [Search shipment items](.\u002Fsearch.md)\n- [Shipment item fields](.\u002Ffields.md)\n- [Shipment items](\u002Fdocs\u002Fentities\u002Fshipment-items)\n- [Filtering syntax](\u002Fdocs\u002Ffiltering)\n- [Limits and optimization](\u002Fdocs\u002Foptimization)\n","2026-10-23",{},1795415294034]