# Scopes

A scope groups devices. Your team's whole device inventory is the root, and you build a tree of
scopes under it. Each device belongs to exactly one scope.

A top-level scope binds to one [zone](https://docs.qualithm.com/zones.md) when you create it. Every scope under it shares that
zone, so each tree of scopes stays in one zone. A device takes its zone from its scope when it
claims its credential, and that zone never changes.

## Create a scope

You need an API token for your team. Send a name and a zone the [Zones](https://docs.qualithm.com/zones.md) page lists as open
in your environment:

```bash
curl -X POST <your-api-url>/v1/teams/<your-team-id>/scopes \
  -H "Authorization: Bearer <your-api-token>" \
  -H "Content-Type: application/json" \
  -d '{"name": "Warehouse", "zone": "sg-sin-a"}'
```

The response holds the new scope. Its `id` is what claim codes and devices refer to.

To nest a scope, send `parentId` instead of `zone`. The new scope takes its parent's zone.

## Add a device to a scope

Create a claim code for the scope:

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

The response's `code` is the claim code. The device sends it to the API to [claim](https://docs.qualithm.com/http.md) its
credential. It joins the scope and runs in the scope's zone from then on.

## Move a device

Send the target scope's ID as `scopeId`:

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

The target scope must be in the device's zone. A move to another zone returns `409`. To run a device
in another zone, delete it and claim it again with a claim code from a scope in that zone.

## Delete a scope

Move out or delete its devices and child scopes first. Qualithm refuses to delete a scope that still
holds either.

## See also

- [Zones](https://docs.qualithm.com/zones.md)
- [Devices guide](https://docs.qualithm.com/devices.md)
