# Devices 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 the live state of a team's devices

`GET /v1/teams/{teamId}/device-states`

By `deviceId`, ascending. A deleted device has no row.

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

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | A team ID: `tem_…`; also accepts the legacy `tm_…` |
| `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 [DeviceStatePage](#devicestatepage) 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>/device-states" \
  -H "Authorization: Bearer <your-api-token>"
```

## List the team's devices, optionally in one scope

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

Newest first, by `createdAt` 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 |
| `scopeId` | query | string | No | Only the devices directly in this scope, not the scopes under it |
| `tag` | query | string | No | Only devices carrying this tag, matched case-insensitively |
| `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 [DevicePage](#devicepage) 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" \
  -H "Authorization: Bearer <your-api-token>"
```

## Create a device in a scope

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

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

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |
| `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:** [CreateDeviceRequest](#createdevicerequest)

**Response:** `201` with [Device](#device) 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" \
  -H "Authorization: Bearer <your-api-token>" \
  -H "Content-Type: application/json" \
  -d '{"scopeId":"<scope-id>"}'
```

## Get a device

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

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_…` |

**Response:** `200` with [Device](#device) in `data`.

**Response headers:**

- `ETag`: The resource's version; send it back as If-Match to edit only that version

**[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>" \
  -H "Authorization: Bearer <your-api-token>"
```

## Rename, move, park or retag a device

`PATCH /v1/teams/{teamId}/devices/{deviceId}`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `device.write` 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_…` |
| `If-Match` | header | string | No | The ETag you last read. The request is refused with 412 when the resource has changed since; omit it to apply the change regardless. |

**Request body:** [UpdateDeviceRequest](#updatedevicerequest)

**Response:** `204` with no body.

**Response headers:**

- `ETag`: The resource's version; send it back as If-Match to edit only that version

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

```bash
curl -X PATCH "<your-api-url>/v1/teams/<team-id>/devices/<device-id>" \
  -H "Authorization: Bearer <your-api-token>" \
  -H "Content-Type: application/json" \
  -d '{"name":"<name>","scopeId":"<scope-id>","parked":true,"tags":["<tags>"]}'
```

## Delete a device and revoke its credentials

`DELETE /v1/teams/{teamId}/devices/{deviceId}`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `device.write` 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_…` |
| `If-Match` | header | string | No | The ETag you last read. The request is refused with 412 when the resource has changed since; omit it to apply the change regardless. |

**Response:** `204` with no body.

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

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

## Types

### DeviceStatePage

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

### DeviceState

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `deviceId` | string | Yes | A device ID: `dvc_<zone>_…`; also accepts the legacy `dev_…` |
| `scopeId` | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |
| `online` | boolean | Yes | Whether the device was seen within the offline window |
| `lastSeenAt` | string (date-time) | Yes | When the device was last seen |
| `metrics` | map of number | Yes | The device's current metrics |
| `primary` | object or null | Yes | The first sensor the device declared (sensors declared together are taken by key), with its latest value; null when it declared none or hasn't reported it yet |

### DevicePage

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

### Device

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `id` | string | Yes | A device ID: `dvc_<zone>_…`; also accepts the legacy `dev_…` |
| `scopeId` | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |
| `scopeName` | string | Yes |  |
| `teamId` | string | Yes | A team ID: `tem_…`; also accepts the legacy `tm_…` |
| `name` | string | Yes |  |
| `createdAt` | string (date-time) | Yes | UTC time, RFC 3339 |
| `updatedAt` | string (date-time) | Yes | UTC time, RFC 3339 |
| `parkedAt` | string (date-time) or null | No | Set while the device is parked. A member's park keeps the device from connecting or writing until a member unparks it; an idle park lifts when the device next connects in its zone with a valid credential |
| `parkedReason` | one of `"member"`, `"idle"` or null | No | Why the device is parked: `member` for a park that holds until a member unparks the device, `idle` when it had no activity for 30 days. A development credential lives at most 30 days, so an idle-parked device usually needs a new credential before it can connect. Null while the device isn't parked |
| `capabilitiesDeclared` | boolean or null | No | Whether the device's latest session declared its capabilities before sending data; null until a session has done either |
| `virtual` | boolean | No | Whether the device declared itself virtual at claim: it runs on a host rather than a board, and its readings count toward the team's monthly events like any device's |
| `tags` | array of string | No | Operator-set tags, lowercased and sorted. A capability reference with this tag matches every capability on the device |
| `level` | one of `"development"`, `"production"` | No |  |
| `checks` | array of [DeviceCheck](#devicecheck) | No |  |
| `attention` | object or null | No | What about the device needs the builder, or null when nothing does: `failed` when it has had a credential but none is usable now, `warning` when its battery is below 20% |
| `decisions` | array of [Decision](#decision) | No | What Qualithm decided when the device was claimed: at most six, most consequential first. Returned by getDevice; empty for a device created without a claim. |

### DeviceCheck

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `id` | one of `"credential-certificate"`, `"key-protected"`, `"firmware-signed"`, `"credential-current"` | Yes |  |
| `pass` | boolean | Yes |  |
| `evidence` | string | Yes | The fact the result rests on, in plain words |

### Decision

One choice Qualithm made on the builder's behalf.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `key` | string | Yes | Stable identifier for the decision, e.g. `zone` |
| `label` | string | Yes | The decision's name, for people, e.g. `Zone` |
| `value` | string | Yes | The value chosen, as held by its source |
| `source` | string | Yes | Where the value is held, e.g. `scope` or `credential` |
| `docsUrl` | string | Yes | The docs page that explains the default |

### CreateDeviceRequest

The scope to create it in

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `scopeId` | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |

### UpdateDeviceRequest

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `name` | string | No | Unique among the live devices of the scope it ends up in; an empty name is refused |
| `scopeId` | string | No | A scope in the device's zone; a device stays in the zone it was claimed in |
| `parked` | boolean | No | Parking holds until a member unparks the device: it can't connect or write meanwhile, but it can renew its credentials, so they and its history stay. Parking an idle-parked device makes it a member's park. An idle park lifts when the device next connects in its zone with a valid credential |
| `tags` | array of string | No | Replaces the device's tags; `[]` clears them. At most 32, each 1 to 64 characters after trimming. Stored lowercased |
