## Currency aggregation

`POST /v1/currencies/aggregate`

Count of currencies and numeric aggregations over exchange rates and other numeric fields of the directory.

Supports the `count` function and the numeric functions `sum`, `avg`, `min`, `max` over the fields `amount`, `amountCnt`, `sort`, `decimals`. Grouping via `groupBy` is not available for currencies — the directory has no fields allowed for grouping, so any `groupBy` returns `400 INVALID_PARAMS`.

## Request fields (body)

| Parameter | Type | Required | Description |
|----------|-----|:-----:|---------|
| `aggregate` | array | no | Array of aggregations. Counting — `{ "field": "*", "function": "count" }`. Numeric functions — `{ "field": "<field>", "function": "sum" \| "avg" \| "min" \| "max" }` over the fields `amount`, `amountCnt`, `sort`, `decimals`. Without the parameter only `count` is returned |
| `groupBy` | string \| array | no | Not supported for currencies — returns `400 INVALID_PARAMS`. The directory has no fields allowed for grouping |
| `filter` | object | no | Not supported for currencies — any key returns `400 UNSUPPORTED_FILTER` |

## Examples

### curl — personal key

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/currencies/aggregate" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "aggregate": [
      { "field": "amount", "function": "sum" },
      { "field": "amount", "function": "avg" }
    ]
  }'
```

### curl — OAuth application

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/currencies/aggregate" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "aggregate": [
      { "field": "amount", "function": "sum" },
      { "field": "amount", "function": "avg" }
    ]
  }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/currencies/aggregate', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    aggregate: [
      { field: 'amount', function: 'sum' },
      { field: 'amount', function: 'avg' },
    ],
  }),
})

const { success, data } = await res.json()
console.log('Sum of rates:', data.aggregates.amount.sum)
```

### JavaScript — OAuth application

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

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

## Other scenarios

The total number of currencies in your Bitrix24 account — the fastest request, without fetching records:

```json
{}
```

Minimum and maximum exchange rate:

```json
{ "aggregate": [{ "field": "amount", "function": "min" }, { "field": "amount", "function": "max" }] }
```

## 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 | Number of currencies matching the request |
| `data.aggregates` | object | Results of numeric functions, grouped by field name: `data.aggregates.amount.sum` and so on. Empty object if the `aggregate` array is not passed |
| `data.meta.totalRecords` | number | Total number of currencies |
| `data.meta.recordsProcessed` | number | How many records were processed for numeric functions. `0` for a request with only `count` |
| `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` |

## Response example

```json
{
  "success": true,
  "data": {
    "count": 7,
    "aggregates": {
      "amount": { "sum": 59, "avg": 8.428571428571429 }
    },
    "meta": {
      "totalRecords": 7,
      "recordsProcessed": 7,
      "truncated": false
    }
  }
}
```

Without the `aggregate` array the `data.aggregates` field is an empty object, and `data.meta.recordsProcessed` equals `0`.

## Error response example

400 — field not available for aggregation:

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "Field 'nope' not found. Available numeric fields: amount, amountCnt, sort, decimals."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | Unknown field in `aggregate` — the message lists the available numeric fields |
| 400 | `INVALID_PARAMS` | Non-numeric field in `sum`, `avg`, `min`, `max` — the message names the field type |
| 400 | `INVALID_PARAMS` | Unknown aggregation function — `count`, `sum`, `avg`, `min`, `max` are supported |
| 400 | `INVALID_PARAMS` | `groupBy` was passed — grouping is not available for currencies |
| 400 | `UNSUPPORTED_FILTER` | `filter` was passed — currencies are not filtered server-side |
| 403 | `SCOPE_DENIED` | The API key does not have the `crm` scope |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |
| 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

**`count` does not fetch records, numeric functions do.** `count` is computed in a single request at any volume, so `data.meta.recordsProcessed` equals `0`. The functions `sum`, `avg`, `min`, `max` fetch records and are computed on the Vibecode side, so `data.meta.recordsProcessed` equals the number of processed currencies. The currency directory holds only a handful of records, so the 5000-record ceiling is never reached for it.

**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 on every element of `data.groups`, 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

- [Currencies](/docs/entities/currencies)
- [Currency list](/docs/entities/currencies/list)
- [Currency search](/docs/entities/currencies/search)
