Skip to main content

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 send X-Org-Id and it differs, you get 400 VALIDATION_FAILED (platform members are exempt).
  • Non-members: a caller who isn't a member of {orgId} gets 404 NOT_FOUND on every route here (the org is hidden), not 403.
  • Permissions: list and dependents need plants ≥ VIEW. Create, edit and delete need settings ≥ EDIT.
  • Writes: send Content-Type: application/json. Create needs Idempotency-Key. Edit and delete need If-Match: "<version>" (from the ETag / version).

The Tag object​

FieldTypeNotes
tagIdULID
codestring?TG-NN, allocated per org
labelstringUnique within org + group
groupenumSEGMENT, REGION, SERVICE, COMMERCIAL, COMPLIANCE
colorenumNEUTRAL (default), INFO, SUCCESS, WARNING, ERROR, ACCENT
notestring?
appliesToenum[]Subset of PLANT, CUSTOMER; DEVICE only on tags created before ADR 0012
systembooleantrue only for REGION tags
regionKeystring?REGION tags only, e.g. PK#sindh
statusenumACTIVE, DELETING
versionintegerSend back as If-Match
plantCount, customerCount, deviceCountintegerCounters; updated asynchronously (eventually consistent)
createdAt, updatedAtISO-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​

QueryTypeNotes
groupenum?Filter to one group
appliesToenum?PLANT, CUSTOMER or DEVICE
limit1–50Server cap 50; default 15 on the first page, 10 with a cursor (lazy loading)
cursorstring?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 }
}
StatuscodeWhenWhat the FE should do
400VALIDATION_FAILEDUnknown/bad query param, X-Org-Id mismatchFix the request
400INVALID_CURSORBad or stale cursorReload page 1
401— (gateway body)Missing/expired tokenRefresh token, retry
403FORBIDDENMember, but plants below VIEWShow "no access"
404NOT_FOUNDNot a member of {orgId} (the org is hidden, not forbidden)Switch org / show "not found"
500INTERNALUnexpectedRetry later
503DEPENDENCY_UNAVAILABLEDynamoDB unavailableRetry 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 fieldTypeRules
labelstringRequired; trimmed; unique within org + group
groupenumRequired; any group except REGION
colorenumOptional, default NEUTRAL
notestringOptional
appliesToenum[]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"] }
StatuscodeWhenWhat the FE should do
400MALFORMED_REQUEST / VALIDATION_FAILEDBody not JSON; X-Org-Id mismatchFix the request
403FORBIDDENsettings below EDITHide the create button
409ALREADY_EXISTSLabel already used in that groupShow "a tag with this name exists" next to the label field
409IDEMPOTENCY_IN_PROGRESSSame key still processingRetry after Retry-After with the same key
409CONFLICTCode allocation or concurrent retry racedRetry with the same key
415UNSUPPORTED_MEDIA_TYPEMissing JSON content typeFix the client
422VALIDATION_FAILEDBad field, or group: "REGION"Show the field errors
422IDEMPOTENCY_KEY_REUSEDSame key, different bodyGenerate a new key per new create
428PRECONDITION_REQUIREDNo Idempotency-KeyAlways send one
500 / 503INTERNAL / DEPENDENCY_UNAVAILABLEServer / DynamoDBRetry 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.

StatuscodeWhenWhat the FE should do
400VALIDATION_FAILEDMalformed If-Match, X-Org-Id mismatchFix the request
403FORBIDDENsettings below EDITRead-only view
404NOT_FOUNDUnknown tag in this orgRefresh the list
409VERSION_CONFLICTStale If-Match (details.currentVersion)Re-fetch, show changes, resubmit
409INVALID_STATESystem REGION tag; tag is DELETING; removing an appliesTo type that is still attachedExplain; detach first for appliesTo
409ALREADY_EXISTSNew label taken in this groupField error
422VALIDATION_FAILEDBad field or empty bodyField errors
428PRECONDITION_REQUIREDNo If-MatchAlways 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".

StatuscodeWhenWhat the FE should do
403FORBIDDENsettings below EDITHide delete
404NOT_FOUNDUnknown tagRefresh
409VERSION_CONFLICTStale If-MatchRe-fetch and confirm again
409INVALID_STATESystem REGION tag, or already DELETINGExplain / refresh
428PRECONDITION_REQUIREDNo If-MatchSend it
500 / 503INTERNAL / DEPENDENCY_UNAVAILABLEServer / DynamoDBRetry

GET /v1/orgs/{orgId}/tags/{tagId}/dependents: what depends on a tag​

Response 200:

FieldNotes
counts.plants, counts.customers, counts.devicesEntities 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.