## Aggregate application lines

`POST /v1/telephony-lines/aggregate`

Counts the external lines added by the application. Counting is the only aggregation function that applies to lines.

**Standard fields:** a line has no numeric fields and no fields to group by. `number`, `name` and `serverName` are strings, and `crmAutoCreate` is a boolean. Therefore `sum`, `avg`, `min`, `max` and `groupBy` are rejected with `400 INVALID_PARAMS`.

**User fields (UF):** a line has none, so they do not enable numeric functions or grouping either.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `aggregate` | array | no | Array of aggregations. For lines, only the element `{ "field": "*", "function": "count" }` applies. Without the parameter, `count` is returned |
| `filter` | object | no | Only an empty object `{}` is accepted: filtering by line fields is not supported, and any key inside returns `400 UNSUPPORTED_FILTER` |

## Examples

### curl — personal key

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

### curl — OAuth application

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

### JavaScript — personal key

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

const { success, data } = await res.json()
console.log('Application lines:', data.count)
```

### JavaScript — OAuth application

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

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

## Other scenarios

The blocks below are request bodies.

The same count without the `aggregate` array:

```json
{}
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.count` | number | Number of application lines |
| `data.aggregates` | object | Results of numeric aggregations. Always an empty object for lines |
| `data.meta.totalRecords` | number | Total number of application lines |
| `data.meta.recordsProcessed` | number | Number of records fetched. `count` fetches no records, so the value is `0` |
| `data.meta.truncated` | boolean | Whether the number was computed over only part of the records. Always `false` for lines: `count` fetches no records and is not truncated |

## Response example

```json
{
  "success": true,
  "data": {
    "count": 2,
    "aggregates": {},
    "meta": {
      "totalRecords": 2,
      "recordsProcessed": 0,
      "truncated": false
    }
  }
}
```

## Error response example

400 — a filter was passed:

```json
{
  "success": false,
  "error": {
    "code": "UNSUPPORTED_FILTER",
    "message": "telephony-lines aggregate does not support filter[]; B24 method \"telephony.externalLine.get\" takes no list parameters at all. Filter keys received: NUMBER. The full collection is returned in one page — filter it on your side."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `UNSUPPORTED_FILTER` | At least one key was passed in `filter` — filtering by line fields is not supported |
| 400 | `INVALID_PARAMS` | `count` was passed with a field other than `"*"` |
| 400 | `INVALID_PARAMS` | The function `sum`, `avg`, `min` or `max` was passed — a line has no numeric fields. The message names the field type or reports that the field was not found |
| 400 | `INVALID_PARAMS` | `groupBy` was passed — a line has no fields to group by |
| 400 | `INVALID_PARAMS` | Unknown aggregation function. The message lists the allowed ones |
| 400 | `INVALID_PARAMS` | `aggregate` was passed as an empty array |
| 400 | `INVALID_PARAMS` | `op`, `field` or `function` was passed at the root of the body without the `aggregate` array |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header was not provided |
| 401 | `INVALID_API_KEY` | The key is not recognized — no such key exists on the platform |
| 401 | `TOKEN_MISSING` | The API key has no configured tokens |
| 401 | `KEY_INACTIVE` | The API key is inactive or revoked |
| 403 | `SCOPE_DENIED` | The API key lacks the `telephony` scope |
| 429 | `RATE_LIMITED` | Request rate limit exceeded |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is temporarily unavailable — retry the request later |

Full list of common API errors — [Errors](/docs/errors).

## Known specifics

**The count covers the same lines the list returns.** `data.count` matches `meta.total` of [`GET /v1/telephony-lines`](/docs/telephony/lines/list): lines rented from Voximplant or connected via SIP are not counted. To list them, use [`GET /v1/voximplant-lines`](/docs/telephony/lines/voximplant).

**Sorting in the body is ignored.** The `order` field does not affect the response and does not cause an error.

## See also

- [List application lines](/docs/telephony/lines/list)
- [Search application lines](/docs/telephony/lines/search)
- [Application line fields](/docs/telephony/lines/fields)
- [Voximplant lines](/docs/telephony/lines/voximplant)
