Tags API (C20)
Feature 004, user story 5. Tags label plants, customers and devices. They drive list filters and TAG-scoped member
access: a member whose scope is TAG sees exactly the plants carrying one of their tags. Attaching or removing a tag
can therefore grant or revoke access. To attach tags to plants, see Plants API, plant tags.
:::caution Deployed; no captured run yet
Deployed on dev (verified live 2026-10-13).
As of 2026-10-13 these routes are in lib/routes.ts (ListTags, CreateTag, UpdateTag, DeleteTag,
GetTagDependents; handler packages/api/src/handlers/tags.ts, core packages/core/src/tags/repository.ts). No
execution has been captured. Every example on this page is illustrative, derived from the Zod schemas and the
handler code.
:::
Common rules
- Auth: Cognito access token from the web client (
Authorization: Bearer <access token>). - Org:
{orgId}in the path is authoritative. If you also sendX-Org-Idand it differs, you get400 VALIDATION_FAILED(platform members are exempt). - Non-members: a caller who isn't a member of
{orgId}gets404 NOT_FOUNDon every route here (the org is hidden), not403. - Permissions: list and dependents need
plants≥ VIEW. Create, edit and delete needsettings≥ EDIT. - Writes: send
Content-Type: application/json. Create needsIdempotency-Key. Edit and delete needIf-Match: "<version>"(from theETag/version).
The Tag object
| Field | Type | Notes |
|---|---|---|
tagId | ULID | |
code | string? | TG-NN, allocated per org |
label | string | Unique within org + group |
group | enum | SEGMENT, REGION, SERVICE, COMMERCIAL, COMPLIANCE |
color | enum | NEUTRAL (default), INFO, SUCCESS, WARNING, ERROR, ACCENT |
note | string? | |
appliesTo | enum[] | Subset of PLANT, CUSTOMER; DEVICE only on tags created before ADR 0012 |
system | boolean | true only for REGION tags |
regionKey | string? | REGION tags only, e.g. PK#sindh |
status | enum | ACTIVE, DELETING |
version | integer | Send back as If-Match |
plantCount, customerCount, deviceCount | integer | Counters; updated asynchronously (eventually consistent) |
createdAt, updatedAt | ISO-8601 UTC |
Region tags are automatic
REGION tags are system tags. The server creates one per country + address.region (normalised, so "Sindh" and
" sindh " are the same tag) the first time a plant uses it, attaches it, and swaps it when the plant's address region
changes. You cannot create (422), rename, edit or delete (409 INVALID_STATE) a REGION tag, and you cannot attach
or detach one by hand. Show them read-only with the text "Applied automatically from the plant address".
GET /v1/orgs/{orgId}/tags: list tags
| Query | Type | Notes |
|---|---|---|
group | enum? | Filter to one group |
appliesTo | enum? | PLANT, CUSTOMER or DEVICE |
limit | 1–50 | Server cap 50; default 15 on the first page, 10 with a cursor (lazy loading) |
cursor | string? | nextCursor from the previous page |
Response 200: { items: Tag[], nextCursor?, groupCounts: { <group>: number } }.
Successful response: illustrative example
{
"items": [
{ "tagId": "01JB0000000000000000000T01", "code": "TG-01", "label": "Key accounts", "group": "SEGMENT",
"color": "INFO", "appliesTo": ["PLANT", "CUSTOMER"], "system": false, "status": "ACTIVE", "version": 3,
"plantCount": 12, "customerCount": 4, "deviceCount": 0,
"createdAt": "2026-10-01T09:00:00.000Z", "updatedAt": "2026-10-10T09:00:00.000Z" }
],
"groupCounts": { "SEGMENT": 1, "REGION": 2 }
}
| Status | code | When | What the FE should do |
|---|---|---|---|
| 400 | VALIDATION_FAILED | Unknown/bad query param, X-Org-Id mismatch | Fix the request |
| 400 | INVALID_CURSOR | Bad or stale cursor | Reload page 1 |
| 401 | — (gateway body) | Missing/expired token | Refresh token, retry |
| 403 | FORBIDDEN | Member, but plants below VIEW | Show "no access" |
| 404 | NOT_FOUND | Not a member of {orgId} (the org is hidden, not forbidden) | Switch org / show "not found" |
| 500 | INTERNAL | Unexpected | Retry later |
| 503 | DEPENDENCY_UNAVAILABLE | DynamoDB unavailable | Retry with backoff |
Empty org: items: [], groupCounts: {}.
:::note Admin-web 2026-11-24
The list also accepts q: every word must be the start of a word in the label. Page 1 carries total, the sum of groupCounts. See Admin-web list totals, counts and filter options.
:::
POST /v1/orgs/{orgId}/tags: create a tag
Headers: Idempotency-Key (required), Content-Type: application/json.
| Body field | Type | Rules |
|---|---|---|
label | string | Required; trimmed; unique within org + group |
group | enum | Required; any group except REGION |
color | enum | Optional, default NEUTRAL |
note | string | Optional |
appliesTo | enum[] | Required; 1–2 distinct of PLANT, CUSTOMER. DEVICE → 422 VALIDATION_FAILED (ADR 0012: device records belong to TCP) |
Unknown fields are rejected. Response 201 with the Tag, a Location header and an ETag.
Request: illustrative example
{ "label": "Key accounts", "group": "SEGMENT", "color": "INFO", "appliesTo": ["PLANT", "CUSTOMER"] }
| Status | code | When | What the FE should do |
|---|---|---|---|
| 400 | MALFORMED_REQUEST / VALIDATION_FAILED | Body not JSON; X-Org-Id mismatch | Fix the request |
| 403 | FORBIDDEN | settings below EDIT | Hide the create button |
| 409 | ALREADY_EXISTS | Label already used in that group | Show "a tag with this name exists" next to the label field |
| 409 | IDEMPOTENCY_IN_PROGRESS | Same key still processing | Retry after Retry-After with the same key |
| 409 | CONFLICT | Code allocation or concurrent retry raced | Retry with the same key |
| 415 | UNSUPPORTED_MEDIA_TYPE | Missing JSON content type | Fix the client |
| 422 | VALIDATION_FAILED | Bad field, or group: "REGION" | Show the field errors |
| 422 | IDEMPOTENCY_KEY_REUSED | Same key, different body | Generate a new key per new create |
| 428 | PRECONDITION_REQUIRED | No Idempotency-Key | Always send one |
| 500 / 503 | INTERNAL / DEPENDENCY_UNAVAILABLE | Server / DynamoDB | Retry with the same key |
Replaying the same key with the same body returns the original tag (no duplicate).
PATCH /v1/orgs/{orgId}/tags/{tagId}: edit a tag
Headers: If-Match: "<version>" (required). Body: any of label, color, note (null clears it), appliesTo (PLANT/CUSTOMER only — DEVICE is 422, ADR 0012);
at least one field. group cannot change. Response 200 with the Tag and a new ETag.
Renaming is safe: filters and scopes store the tagId, not the label. Search results catch up asynchronously.
| Status | code | When | What the FE should do |
|---|---|---|---|
| 400 | VALIDATION_FAILED | Malformed If-Match, X-Org-Id mismatch | Fix the request |
| 403 | FORBIDDEN | settings below EDIT | Read-only view |
| 404 | NOT_FOUND | Unknown tag in this org | Refresh the list |
| 409 | VERSION_CONFLICT | Stale If-Match (details.currentVersion) | Re-fetch, show changes, resubmit |
| 409 | INVALID_STATE | System REGION tag; tag is DELETING; removing an appliesTo type that is still attached | Explain; detach first for appliesTo |
| 409 | ALREADY_EXISTS | New label taken in this group | Field error |
| 422 | VALIDATION_FAILED | Bad field or empty body | Field errors |
| 428 | PRECONDITION_REQUIRED | No If-Match | Always send it |
DELETE /v1/orgs/{orgId}/tags/{tagId}: delete a tag
Headers: If-Match: "<version>" (required). Response 202:
{ "tagId": "01JB0000000000000000000T01", "status": "DELETING",
"dependentsLosingScope": [ { "kind": "MEMBER_SCOPE", "id": "sub-example", "label": "Field engineer (illustrative)" } ] }
The tag is marked DELETING at once and is removed from every plant and customer in the background, after which it
disappears from the list. Until then it is still listed with status: "DELETING" and cannot be attached. Call
dependents first to show the confirmation "These depend on
it and will lose their scope".
| Status | code | When | What the FE should do |
|---|---|---|---|
| 403 | FORBIDDEN | settings below EDIT | Hide delete |
| 404 | NOT_FOUND | Unknown tag | Refresh |
| 409 | VERSION_CONFLICT | Stale If-Match | Re-fetch and confirm again |
| 409 | INVALID_STATE | System REGION tag, or already DELETING | Explain / refresh |
| 428 | PRECONDITION_REQUIRED | No If-Match | Send it |
| 500 / 503 | INTERNAL / DEPENDENCY_UNAVAILABLE | Server / DynamoDB | Retry |
GET /v1/orgs/{orgId}/tags/{tagId}/dependents: what depends on a tag
Response 200:
| Field | Notes |
|---|---|
counts.plants, counts.customers, counts.devices | Entities carrying the tag |
dependents[] | { kind, id, label }; kind ∈ MEMBER_SCOPE, SHARE, MAINTENANCE_PLAN, REPORT_SCHEDULE, NOTIFICATION_RULE, SAVED_VIEW |
sources | { <kind>: boolean }; false means the owning feature isn't built yet, so that kind can't be checked. Say "not checked" rather than "none" |
Errors: 400 VALIDATION_FAILED (X-Org-Id mismatch), 401, 403 FORBIDDEN (plants below VIEW), 404 NOT_FOUND (unknown tag, or not a member of the org),
500, 503.
Web console additions
See Web console additions (N-37 … N-51) for the newer, not-yet-deployed fields and routes on this area.