# Commands endpoints

Every path starts at your [API URL](https://docs.qualithm.com/api.md#find-your-api-url). Every response is JSON, and an error has the [error body](https://docs.qualithm.com/api.md#errors).

## List a device's recent command deliveries

`GET /v1/teams/{teamId}/devices/{deviceId}/commands`

Newest first, by `settledAt` (or `enqueuedAt` while unsettled) then `id`.

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `device.read` permission, which the owner, manager and viewer roles hold.

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |
| `deviceId` | path | string | Yes | A device ID: `dvc_<zone>_…`; also accepts the legacy `dev_…` |
| `limit` | query | integer | No | Items per page, 1 to 100. Default 50. |
| `cursor` | query | string | No | A previous page's `nextCursor`, to read the page after it |

**Response:** `200` with [DeviceCommandPage](#devicecommandpage) in `data`.

**[Errors](https://docs.qualithm.com/api.md#errors):** `400`, `401`, `403`, `404`, `429`, `500`, `503`

```bash
curl "<your-api-url>/v1/teams/<team-id>/devices/<device-id>/commands" \
  -H "Authorization: Bearer <your-api-token>"
```

## Send a command to a device

`POST /v1/teams/{teamId}/devices/{deviceId}/commands`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `device.command` permission, which the owner and manager roles hold.

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |
| `deviceId` | path | string | Yes | A device ID: `dvc_<zone>_…`; also accepts the legacy `dev_…` |
| `Idempotency-Key` | header | string | No | A unique key for this request. A retry with the same key and body returns the first response instead of acting again; keys last 24 hours. |

**Request body:** [CreateDeviceCommandRequest](#createdevicecommandrequest)

**Response:** `201` with [QueuedCommand](#queuedcommand) in `data`.

**[Errors](https://docs.qualithm.com/api.md#errors):** `400`, `401`, `403`, `404`, `409`, `413`, `415`, `429`, `500`, `503`

```bash
curl -X POST "<your-api-url>/v1/teams/<team-id>/devices/<device-id>/commands" \
  -H "Authorization: Bearer <your-api-token>" \
  -H "Content-Type: application/json" \
  -d '{"capability":"<capability>","dedupKey":"<dedup-key>"}'
```

## Types

### DeviceCommandPage

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `items` | array of [DeviceCommand](#devicecommand) | Yes |  |
| `nextCursor` | string or null | Yes | Pass as `cursor` to read the next page; `null` on the last page |

### DeviceCommand

One command delivery to a device.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `id` | string | Yes | A command ID: `cmd_<zone>_…`; also accepts the legacy `cmd_…` |
| `deviceId` | string | Yes | A device ID: `dvc_<zone>_…`; also accepts the legacy `dev_…` |
| `source` | one of `"automation"`, `"dashboard"`, `"api"` | Yes |  |
| `topic` | string | Yes |  |
| `status` | one of `"pending"`, `"sent"`, `"failed"` | Yes |  |
| `lastError` | string or null | Yes |  |
| `enqueuedAt` | string (date-time) | Yes | UTC time, RFC 3339 |
| `settledAt` | string (date-time) or null | Yes |  |

### CreateDeviceCommandRequest

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `capability` | string | Yes | The capability key the device declared |
| `value` | any | No | Omitted for a `trigger` capability |
| `dedupKey` | string | Yes | Caller-owned idempotency key: reuse it on retry so the device fires once |

### QueuedCommand

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `id` | string | Yes | A command ID: `cmd_<zone>_…`; also accepts the legacy `cmd_…` |
| `duplicate` | boolean | Yes | True when the dedup key was already used: no second command was sent |
