[{"data":1,"prerenderedAt":7},["ShallowReactive",2],{"docs-entities\u002Fcalendar-events\u002Faggregate":3,"docs-tabs-entities\u002Fcalendar-events\u002Faggregate":6},{"content":4,"lastmod":5},"## Aggregate calendar events\n\n`POST \u002Fv1\u002Fcalendar-events\u002Faggregate`\n\nCounts the events of one calendar over a period and sums their numeric fields, for example the total duration of an employee's meetings over a year. The calendar and the period are set inside the `filter` object.\n\n## Request fields (body)\n\n| Field | Type | Required | Description |\n|------|-----|:-----:|---------|\n| `filter` | object | yes | The calendar and the period to select. Accepts only `type`, `ownerId`, `from`, `to`, `section`. A field outside this set returns `400 UNSUPPORTED_FILTER` |\n| `filter.type` | string | yes | Calendar type: `user`, `group`, `company_calendar` |\n| `filter.ownerId` | number | yes | Calendar owner ID. For `type=user` — the employee ID from `GET \u002Fv1\u002Fusers`, for `type=group` — the workgroup ID |\n| `filter.from` | string | no | Start of the event selection period (ISO 8601). Without `from` and `to`, the same period applies as for the [event list](\u002Fdocs\u002Fentities\u002Fcalendar-events\u002Flist): from one month back to three months ahead |\n| `filter.to` | string | no | End of the event selection period (ISO 8601) |\n| `filter.section` | number | no | Calendar section ID — the value of the event's `sectionId` field. Accepted as a single number. An array returns `400 UNSUPPORTED_FILTER` |\n| `aggregate` | array | no | Array of `{ \"field\": \"\u003Cfield>\", \"function\": \"\u003Cfunction>\" }` expressions, up to 5. Functions: `count`, `sum`, `avg`, `min`, `max`. `count` accepts only `\"field\": \"*\"`. Numeric functions accept fields of type `number`: of these, `durationSeconds` contains the duration; the rest are identifiers (`id`, `ownerId`, `sectionId`, `createdBy` and others). Without the parameter, only `count` is returned |\n\n## Examples\n\n### curl — personal key\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fcalendar-events\u002Faggregate\" \\\n  -H \"X-Api-Key: YOUR_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  -d '{\n    \"filter\": {\n      \"type\": \"user\",\n      \"ownerId\": 1,\n      \"from\": \"2026-01-01\",\n      \"to\": \"2026-12-31\"\n    },\n    \"aggregate\": [\n      { \"field\": \"durationSeconds\", \"function\": \"sum\" }\n    ]\n  }'\n```\n\n### curl — OAuth application\n\n```bash\ncurl -X POST \"https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fcalendar-events\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\": {\n      \"type\": \"user\",\n      \"ownerId\": 1,\n      \"from\": \"2026-01-01\",\n      \"to\": \"2026-12-31\"\n    },\n    \"aggregate\": [\n      { \"field\": \"durationSeconds\", \"function\": \"sum\" }\n    ]\n  }'\n```\n\n### JavaScript — personal key\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fcalendar-events\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: {\n      type: 'user',\n      ownerId: 1,\n      from: '2026-01-01',\n      to: '2026-12-31',\n    },\n    aggregate: [\n      { field: 'durationSeconds', function: 'sum' },\n    ],\n  }),\n})\n\nconst { success, data } = await res.json()\nconsole.log('Events:', data.count, 'hours:', data.aggregates.durationSeconds.sum \u002F 3600)\n```\n\n### JavaScript — OAuth application\n\n```javascript\nconst res = await fetch('https:\u002F\u002Fvibecode.bitrix24.com\u002Fv1\u002Fcalendar-events\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: {\n      type: 'user',\n      ownerId: 1,\n      from: '2026-01-01',\n      to: '2026-12-31',\n    },\n    aggregate: [\n      { field: 'durationSeconds', function: 'sum' },\n    ],\n  }),\n})\n\nconst { success, data } = await res.json()\n```\n\n## Other scenarios\n\nThe blocks below are request bodies.\n\nNumber of events in an employee's calendar over the default period, without loading the records:\n\n```json\n{ \"filter\": { \"type\": \"user\", \"ownerId\": 1 } }\n```\n\nNumber of events in one calendar section over a year:\n\n```json\n{ \"filter\": { \"type\": \"user\", \"ownerId\": 1, \"from\": \"2026-01-01\", \"to\": \"2026-12-31\", \"section\": 3 } }\n```\n\n## Response fields\n\n| Field | Type | Description |\n|------|-----|---------|\n| `success` | boolean | Always `true` on success |\n| `data.count` | number | Number of calendar events over the period |\n| `data.aggregates` | object | Results of the numeric functions: the key is the field name, inside is the value for each function, for example `durationSeconds.sum`. An empty object if the `aggregate` array is not passed |\n| `data.meta.totalRecords` | number | Total number of records matching the filter |\n| `data.meta.recordsProcessed` | number | Number of records read. `0` when only `count` is computed |\n| `data.meta.truncated` | boolean | `true` if the numbers are 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 off by a subpage error. The size of the shortfall is reported in `data.meta.recordsShortfall`, the cut-off slice in `data.meta.pageErrorSample`. Always present, `false` on a complete response. If the request has no numeric functions, no records are loaded and the flag is always `false` |\n\n## Response example\n\nSum of event durations over a year:\n\n```json\n{\n  \"success\": true,\n  \"data\": {\n    \"count\": 6,\n    \"aggregates\": {\n      \"durationSeconds\": { \"sum\": 104400 }\n    },\n    \"meta\": {\n      \"totalRecords\": 6,\n      \"recordsProcessed\": 6,\n      \"truncated\": false\n    }\n  }\n}\n```\n\n## Error response example\n\n400 — the required `type` and `ownerId` are not passed in `filter`:\n\n```json\n{\n  \"success\": false,\n  \"error\": {\n    \"code\": \"MISSING_REQUIRED_PARAMS\",\n    \"message\": \"POST \u002Fv1\u002Fcalendar-events\u002Faggregate requires filter params: type, ownerId. Example body: { \\\"filter\\\": {\\\"type\\\":\\\"...\\\",\\\"ownerId\\\":\\\"...\\\"} }\"\n  }\n}\n```\n\n## Errors\n\n| HTTP | Code | Description |\n|------|-----|---------|\n| 400 | `MISSING_REQUIRED_PARAMS` | `filter` lacks `type` and\u002For `ownerId`, or the value is empty or passed as an array or an object. These fields at the top level of the body, outside `filter`, do not count. `message` lists the missing fields |\n| 400 | `UNSUPPORTED_FILTER` | `filter` contains a field outside the `type`, `ownerId`, `from`, `to`, `section` set, or `section` is passed as an array |\n| 400 | `INVALID_PARAMS` | Unknown aggregation function. The message lists the allowed ones |\n| 400 | `INVALID_PARAMS` | Empty `aggregate` array |\n| 400 | `INVALID_PARAMS` | More than 5 expressions in `aggregate` |\n| 400 | `INVALID_PARAMS` | `count` with a field other than `\"*\"` |\n| 400 | `INVALID_PARAMS` | Numeric function on a field whose type is not `number`. The message names the field type |\n| 400 | `INVALID_PARAMS` | Numeric function on a field that events do not have. The message lists the available numeric fields |\n| 400 | `INVALID_PARAMS` | Numeric function without a field or with the `\"*\"` field |\n| 400 | `INVALID_PARAMS` | `groupBy` is passed — calendar events have no fields to group by |\n| 403 | `SCOPE_DENIED` | The API key does not have the `calendar` scope |\n| 401 | `TOKEN_MISSING` | The API key has no configured tokens |\n| 429 | `RATE_LIMITED` | Request limit exceeded: 300 per minute per Bitrix24 account. All API keys of the account 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**The count matches the list.** Without `from`, `to`, and `section`, `data.count` equals `meta.total` of the [event list](\u002Fdocs\u002Fentities\u002Fcalendar-events\u002Flist) for the same `type` and `ownerId`: both count over the same default period. There is no grouping, so a count across several sections or employees takes a separate request per section or per calendar owner.\n\n**A nonexistent calendar returns zero, not an error.** An unknown `type` value or a nonexistent `ownerId` returns `200` with `count: 0`. A zero count does not tell an empty calendar from a mistake in the `type` + `ownerId` pair — check the pair with an [event list](\u002Fdocs\u002Fentities\u002Fcalendar-events\u002Flist) request.\n\n**Numeric functions are computed over the records read.** `count` without numeric functions is returned without loading the records. `sum`, `avg`, `min`, `max` read events page by page, at most 5000. When the response comes with `meta.truncated: true`, a `truncated: true` marker sits inside each field's object in `data.aggregates`, and a warning with the `AGGREGATE_TRUNCATED` code is added to `data.meta.warnings`. Full details — [Aggregation POST — the 5000-record ceiling](\u002Fdocs\u002Fentity-api#aggregation-post-the-5000-record-ceiling).\n\n## See also\n\n- [Event list](\u002Fdocs\u002Fentities\u002Fcalendar-events\u002Flist)\n- [Search events](\u002Fdocs\u002Fentities\u002Fcalendar-events\u002Fsearch)\n- [Event fields](\u002Fdocs\u002Fentities\u002Fcalendar-events\u002Ffields)\n- [Limits and optimization](\u002Fdocs\u002Foptimization)\n","2026-10-23",{},1795415183224]