# Extension States

The **Extension States** endpoint returns the state of **every extension in one tenant** in a single JSON array, including offline extensions. It supports `GET` only and has its own advanced-key permission, `getExtensionStates`.

## Endpoint and authentication

```
GET https://pbx.example.com/pbx/openapi.php/extensions/states?tenant=CANISTRACCI
```

The equivalent query form is:

```
GET https://pbx.example.com/pbx/openapi.php?object=extension&action=states&tenant=CANISTRACCI
```

Full and read-only tenant or global API keys are supported. Send the key with `X-API-Key`, `Authorization: Bearer`, or the `key` query parameter. Extension Dial API keys are not supported.

An **Advanced Open API Key** must explicitly authorize **GET /extensions/states** (`getExtensionStates`) and the selected tenant. Global administrative permission is not required. Permission to list extensions or read one extension's live state does not grant this aggregate operation. Adding this endpoint does not automatically expand existing keys.

## Parameters

<table id="bkmrk-parameterrequireddes"><thead><tr><th>Parameter</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td>`tenant`</td><td>Yes</td><td>One exact tenant code or name. Codes take precedence over names. Ambiguous names and wildcard tenant selection are rejected, including for global keys.</td></tr><tr><td>`live`</td><td>No</td><td>Defaults to `true`. Use `false` for cached device states only. `1` and `0` are accepted aliases.</td></tr><tr><td>`key`</td><td>Unless an authentication header is used</td><td>An API key authorized for this operation and tenant.</td></tr></tbody></table>

Omit `id`, `objectid`, `number`, and `ext`: this endpoint always covers all extensions. It does not use a reserved value such as `number=ALL`.

## Examples

```
curl -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/extensions/states?tenant=CANISTRACCI"

curl -H "Authorization: Bearer TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php/extensions/states?tenant=CANISTRACCI&live=false"

curl -H "X-API-Key: TENANT_API_KEY" \
  "https://pbx.example.com/pbx/openapi.php?object=extension&action=states&tenant=CANISTRACCI"
```

## Response

The response is always an array, ordered by extension number and internal ID. An empty tenant returns `[]`. Missing backend mappings do not remove extensions from the result.

```
[
  {
    "id": 134,
    "number": "103",
    "name": "Reception",
    "tech": "PJSIP",
    "state": "RINGING",
    "dnd": false,
    "device_state": "NOT_INUSE",
    "active_channels": 1,
    "live_status": "ok"
  },
  {
    "id": 135,
    "number": "104",
    "name": "Warehouse",
    "tech": "SIP",
    "state": "UNAVAILABLE",
    "dnd": false,
    "device_state": "UNAVAILABLE",
    "active_channels": 0,
    "live_status": "ok"
  }
]
```

<table id="bkmrk-fieldtypemeaning-idi"><thead><tr><th>Field</th><th>Type</th><th>Meaning</th></tr></thead><tbody><tr><td>`id`</td><td>Integer</td><td>Extension internal ID.</td></tr><tr><td>`number`</td><td>String</td><td>Dialable extension number; leading zeros are preserved.</td></tr><tr><td>`name`</td><td>String</td><td>Extension display name.</td></tr><tr><td>`tech`</td><td>String</td><td>Configured technology, such as SIP, PJSIP, CUSTOM, or VIRTUAL.</td></tr><tr><td>`state`</td><td>String</td><td>Cached device state, overlaid with observed live channel activity. Observed channels can produce `INUSE`, `RINGING`, or `RINGINUSE`. Cached `ONHOLD` and `BUSY` states are preserved for observed non-ringing calls. Without observed channels, the cached state is retained.</td></tr><tr><td>`dnd`</td><td>Boolean</td><td>Configured extension Do Not Disturb flag, independent of channel activity.</td></tr><tr><td>`device_state`</td><td>String or null</td><td>Normalized cached state before the live overlay; null when no trustworthy peer mapping or sample exists.</td></tr><tr><td>`active_channels`</td><td>Integer or null</td><td>Observed channel count for a complete live snapshot. Null when live collection is disabled, unsupported, or incomplete. This counts channels, not distinct conversations.</td></tr><tr><td>`live_status`</td><td>String</td><td>`ok`: complete live snapshot; `partial`: at least one node failed to provide a valid snapshot; `cached_only`: live collection disabled or channel attribution unsupported.</td></tr></tbody></table>

Recognized cached states are `UNKNOWN`, `NOT_INUSE`, `INUSE`, `BUSY`, `INVALID`, `UNAVAILABLE`, `RINGING`, `RINGINUSE`, and `ONHOLD`. Missing or ambiguous peer mappings return `state: "UNKNOWN"` and `device_state: null`.

An idle phone with DND enabled may return `state: "INUSE"`, `dnd: true`, and `active_channels: 0`. Zero observed channels does not reset a cached device state to `NOT_INUSE`.

## Live collection and cached mode

By default, device states are read in batches and live collection uses **one connection and one fixed `core show channels concise` command per configured PBX node**. There are no separate commands for each extension or channel. Each connection/socket read has a two-second timeout; new nodes stop being admitted after a 20-second collection budget.

SIP/PJSIP channels are matched by exact device name and exact tenant account code. Virtual extensions also observe their own tenant's physical members; custom extensions use cached state only. Ambiguous ownership across technologies, tenants, or canonical device-state aliases is excluded. Identically named channels on different nodes are counted separately.

If a node fails, live-capable entries report `live_status: "partial"` and `active_channels: null`, while retaining the cached state and any observed overlay. A successful empty live snapshot returns zero channels, which differs from an unavailable snapshot.

With `live=false`, the endpoint reads cached states only, does not read AMI credentials, and makes no AMI connection. Channel counts are null and `live_status` is `cached_only`. Neither mode returns credentials, registration addresses, channel names, caller IDs, or other tenants' call details.

Node snapshots are sequential rather than one atomic cluster-wide instant. The device-state cache has no sample timestamp and may be stale if its sender/receiver is stopped.

## Errors

<table id="bkmrk-http-statusreason-40"><thead><tr><th>HTTP status</th><th>Reason</th></tr></thead><tbody><tr><td>400</td><td>Missing or invalid exact tenant selection, unsupported identifier filters, another action combined with the endpoint, or an invalid `live` value.</td></tr><tr><td>401</td><td>Missing, unknown, revoked, or expired API key.</td></tr><tr><td>403</td><td>Disallowed IP, tenant, or endpoint; or OpenAPI disabled for the tenant. A missing advanced-key operation returns `api_endpoint_not_allowed`.</td></tr><tr><td>404</td><td>Tenant or requested route not found.</td></tr><tr><td>405</td><td>A method other than GET was used.</td></tr><tr><td>409</td><td>The tenant display name is ambiguous; use its unique code.</td></tr><tr><td>503</td><td>Database lookup failed (`extension_states_unavailable`) or the key policy could not be verified.</td></tr></tbody></table>

## Related endpoints

- `GET /extensions` lists basic extension information without this state snapshot.
- `GET /extensions/state?tenant=...&number=...` returns live call details for one extension and retains its existing object response.
- Extension detail endpoints retain their existing cached state and configuration fields.