# Scopes 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 team's scopes

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

Newest first, by `createdAt` then `id`.

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `scope.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 |
| `zone` | query | one of `"de-fra-a"`, `"sg-sin-a"`, `"us-nyc-a"` | No | Only the scopes in this zone |
| `parentId` | query | string | No | Only the scopes directly under this one |
| `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 [ScopePage](#scopepage) 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>/scopes" \
  -H "Authorization: Bearer <your-api-token>"
```

## Create a scope, top-level in a zone or under a parent

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

`409` with `scope_too_deep` when the new scope would sit deeper than 16 levels, or `scope_limit` when the team already holds 1000 live scopes; `zone_mismatch` when `zone` differs from the parent's.

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `scope.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:** [CreateScopeRequest](#createscoperequest)

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

## Get a scope

`GET /v1/teams/{teamId}/scopes/{scopeId}`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `scope.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` | path | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |

**Response:** `200` with [Scope](#scope) 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>/scopes/<scope-id>" \
  -H "Authorization: Bearer <your-api-token>"
```

## Rename a scope

`PATCH /v1/teams/{teamId}/scopes/{scopeId}`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `scope.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 |
| `scopeId` | path | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |
| `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:** [UpdateScopeRequest](#updatescoperequest)

**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`, `412`, `413`, `415`, `429`, `500`, `503`

```bash
curl -X PATCH "<your-api-url>/v1/teams/<team-id>/scopes/<scope-id>" \
  -H "Authorization: Bearer <your-api-token>" \
  -H "Content-Type: application/json" \
  -d '{"name":"<name>"}'
```

## Delete an empty scope

`DELETE /v1/teams/{teamId}/scopes/{scopeId}`

Refused while the scope holds devices or scopes.

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `scope.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 |
| `scopeId` | path | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |
| `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`, `409`, `412`, `429`, `500`, `503`

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

## List the devices in a scope and every scope under it

`GET /v1/teams/{teamId}/scopes/{scopeId}/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` | path | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |
| `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>/scopes/<scope-id>/devices" \
  -H "Authorization: Bearer <your-api-token>"
```

## Types

### ScopePage

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

### Scope

A group of devices in one zone. Scopes nest; each device belongs to exactly one.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `id` | string | Yes | A scope ID: `scp_<zone>_…`; also accepts the legacy `spc_…` |
| `teamId` | string | Yes | A team ID: `tem_…`; also accepts the legacy `tm_…` |
| `parentId` | string or null | Yes | The parent scope; null for a top-level scope |
| `name` | string | Yes |  |
| `zone` | [ZoneId](#zoneid) | Yes |  |
| `createdAt` | string (date-time) | Yes | UTC time, RFC 3339 |
| `updatedAt` | string (date-time) | Yes | UTC time, RFC 3339 |
| `deviceTotal` | integer (int64) | No | The live devices in the scope and every scope under it |

### ZoneId

A device zone: where a scope's devices connect and their data lives.

one of `"de-fra-a"`, `"sg-sin-a"`, `"us-nyc-a"`.

### CreateScopeRequest

A name, and either a zone or a parent

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `name` | string | Yes | Up to 64 characters |
| `zone` | [ZoneId](#zoneid) | No | Required for a top-level scope; a child takes its parent's zone |
| `parentId` | string | No | The parent scope; absent for a top-level scope |

### UpdateScopeRequest

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `name` | string | Yes |  |

### 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 |
