# Label devices

Labels (activates) up to 20 devices in one call. Each device is identified by the QR code
printed on it and is linked to your asset (`asset_barcode`) and to one of your applications.
One optional `shipment_details` block can be sent with the call; it is attached to every
device that is labelled successfully.
**Headers.** Send `customer_id` and `authorized_group` only when your API identity has access to
more than one account. Otherwise the account comes from the access token.
### How each device is handled
Devices are processed independently: one device failing never stops the others.
* **Not labelled yet.** The device is labelled and activated.
  * `application_id` can be the application's UUID or its code (for example `smart-asset-tracker`).
  * The device's MAC address and hardware type are looked up from Trackonomy's device registry by
`qrcode`. You don't send them.
  * Facility: see *Facility* below.
* **Already labelled** (any status other than never labelled, including `DEACTIVATED`):
  * Without `override`, the device fails with `ALREADY_LABELLED`.
  * With `override: true`, the existing label is edited in place: `asset_barcode` is replaced, and
`facility_id` and `tape_displayname` are replaced when you send them (`null` clears them,
omitting them keeps the current value). `application_id` (UUID or code) must be the device's
current application; a different one fails with `APPLICATION_CHANGE_NOT_ALLOWED`.
`lat`, `lon`, `skip_qrmac` and `device_id` are ignored.
  * The device's status is never changed. A `DEACTIVATED` device stays deactivated: labelling it
again with `override: true` does **not** reactivate it.

**`override: true` on a device that isn't labelled yet** first removes any device in your account
that has the same MAC address, QR code or asset barcode, then labels this one. Use it only when you
mean to replace that device.
### Facility (new labels)
The first rule that applies sets the device's facility:
1. `facility_id`, when you send it.
2. Your account's own top-level facility, when it has one.
3. `lat` + `lon` (both required): the nearest facility to that point is used, and the point is stored
as the device's location. If no facility is near enough, the device has no facility.
4. The shipment's `source_address`, when you send `shipment_details`: it is resolved to coordinates
and matched the same way.
5. Trackonomy's default top-level facility.

`lat` and `lon` are only used at step 3. They are ignored when you send `facility_id` or when your
account has its own top-level facility.
### Shipment
With `shipment_details`, the call starts a **new shipment**. Its id is returned as `shipment_id` and
stored on every device labelled in this call (replacing any shipment such a device had before).
Addresses are given as `label` (free text, resolved to coordinates), `lat` + `lon` (resolved to an
address), or both (stored as sent). On this call each address needs at least one of them.
This isn't checked before processing: an address with neither (or `lat` without `lon`) doesn't
return `400`. The devices are labelled and then reported as `INTERNAL_ERROR` because the shipment
couldn't be saved. Check them with `GET` before resending.
### Per-device `error_code` values
| Value | Meaning | What to do |
|  --- | --- | --- |
| `ALREADY_LABELLED` | The QR code already has a label in your account and `override` was not `true`. | Check it with `GET`. Resend with `override: true`, or use `PUT` to edit it. |
| `APPLICATION_NOT_FOUND` | `application_id` matches neither an application UUID nor an application code in your account, or the application isn't one your account can use. Codes are case-sensitive. | Fix `application_id`. |
| `QRMAC_NOT_FOUND` | The QR code isn't a registered Trackonomy device. | Check the QR code. If it is printed on a Trackonomy device, contact support. |
| `DUPLICATE_DEVICE` | Another device in your account already has this device's MAC address, this QR code or this asset barcode (this includes a device known to your account that was never labelled). On applications configured for multi-piece shipments, a shared asset barcode is allowed and doesn't cause this error (see `asset_barcode`). On `override: true`, the new `asset_barcode` is already used by another device. | Use another asset barcode, or resend with `override: true` to replace the other device. If that still fails, contact support. |
| `APPLICATION_CHANGE_NOT_ALLOWED` | `override: true` named a different application from the device's current one. | Send the device's current application (see `GET`). The application can't be changed through this API. |
| `FACILITY_NOT_FOUND` | `override: true` with a `facility_id` that doesn't exist in your account or isn't one your account can use. | Fix `facility_id`, or omit it. |
| `DEVICE_NOT_FOUND` | With `override: true`: the device is outside the applications or facilities your account can use. With `shipment_details`: the device was labelled, but its tracking record wasn't available to attach the shipment to. | Check the device with `GET`. Attach the shipment later with `PUT`. |
| `TAPE_TYPE_MISMATCH` | The device's hardware type doesn't match the application. **This case is currently reported as `INTERNAL_ERROR`**; handle both values. | Choose an application that matches the device type. |
| `INTERNAL_ERROR` | Anything else, for example: a device type that doesn't match the application, a QR code with lowercase letters or unsupported characters, an unknown `facility_id` on a new label, an asset barcode equal to the QR code, a shipment address that couldn't be resolved, or a temporary failure. | Check the inputs. **Before resending, check the device with `GET`**: it may already be labelled (for example when only the shipment failed). If so, attach the shipment with `PUT` instead. |

### Status codes
* `201`: every device was labelled.
* `207`: at least one device failed; devices not listed in `errors` were labelled. Also returned when
every device failed, except in the case below.
* `400` with the batch body: **every** device failed with `QRMAC_NOT_FOUND`. Nothing was labelled.
* `400` with `Validation failed`: the request was rejected before any device was processed.

### Retries
This call is not idempotent. Resending a request that already succeeded returns `ALREADY_LABELLED`
for those devices. To change a label afterwards, use `PUT` (or `override: true`).

Endpoint: POST /external/v1/devices/label
Version: 2.2
Security: bearerAuth

## Description:

  - `How each device is handled` ()
    Devices are processed independently: one device failing never stops the others. Not labelled yet. The device is labelled and activated.application_id can be the application's UUID or its code (for example smart-asset-tracker).The device's MAC address and hardware type are looked up from Trackonomy's device registry byqrcode. You don't send them.Facility: see Facility below.Already labelled (any status other than never labelled, including DEACTIVATED):Without override, the device fails with ALREADY_LABELLED.With override: true, the existing label is edited in place: asset_barcode is replaced, andfacility_id and tape_displayname are replaced when you send them (null clears them,omitting them keeps the current value). application_id (UUID or code) must be the device'scurrent application; a different one fails with APPLICATION_CHANGE_NOT_ALLOWED.lat, lon, skip_qrmac and device_id are ignored.The device's status is never changed. A DEACTIVATED device stays deactivated: labelling itagain with override: true does not reactivate it. override: true on a device that isn't labelled yet first removes any device in your accountthat has the same MAC address, QR code or asset barcode, then labels this one. Use it only when youmean to replace that device.

  - `Facility (new labels)` ()
    The first rule that applies sets the device's facility: facility_id, when you send it.Your account's own top-level facility, when it has one.lat + lon (both required): the nearest facility to that point is used, and the point is storedas the device's location. If no facility is near enough, the device has no facility.The shipment's source_address, when you send shipment_details: it is resolved to coordinatesand matched the same way.Trackonomy's default top-level facility. lat and lon are only used at step 3. They are ignored when you send facility_id or when youraccount has its own top-level facility.

  - `Shipment` ()
    With shipment_details, the call starts a new shipment. Its id is returned as shipment_id andstored on every device labelled in this call (replacing any shipment such a device had before).Addresses are given as label (free text, resolved to coordinates), lat + lon (resolved to anaddress), or both (stored as sent). On this call each address needs at least one of them.This isn't checked before processing: an address with neither (or lat without lon) doesn'treturn 400. The devices are labelled and then reported as INTERNAL_ERROR because the shipmentcouldn't be saved. Check them with GET before resending.

  - `Per-device error_code values` ()
    ValueMeaningWhat to doALREADY_LABELLEDThe QR code already has a label in your account and override was not true.Check it with GET. Resend with override: true, or use PUT to edit it.APPLICATION_NOT_FOUNDapplication_id matches neither an application UUID nor an application code in your account, or the application isn't one your account can use. Codes are case-sensitive.Fix application_id.QRMAC_NOT_FOUNDThe QR code isn't a registered Trackonomy device.Check the QR code. If it is printed on a Trackonomy device, contact support.DUPLICATE_DEVICEAnother device in your account already has this device's MAC address, this QR code or this asset barcode (this includes a device known to your account that was never labelled). On applications configured for multi-piece shipments, a shared asset barcode is allowed and doesn't cause this error (see asset_barcode). On override: true, the new asset_barcode is already used by another device.Use another asset barcode, or resend with override: true to replace the other device. If that still fails, contact support.APPLICATION_CHANGE_NOT_ALLOWEDoverride: true named a different application from the device's current one.Send the device's current application (see GET). The application can't be changed through this API.FACILITY_NOT_FOUNDoverride: true with a facility_id that doesn't exist in your account or isn't one your account can use.Fix facility_id, or omit it.DEVICE_NOT_FOUNDWith override: true: the device is outside the applications or facilities your account can use. With shipment_details: the device was labelled, but its tracking record wasn't available to attach the shipment to.Check the device with GET. Attach the shipment later with PUT.TAPE_TYPE_MISMATCHThe device's hardware type doesn't match the application. This case is currently reported as INTERNAL_ERROR; handle both values.Choose an application that matches the device type.INTERNAL_ERRORAnything else, for example: a device type that doesn't match the application, a QR code with lowercase letters or unsupported characters, an unknown facility_id on a new label, an asset barcode equal to the QR code, a shipment address that couldn't be resolved, or a temporary failure.Check the inputs. Before resending, check the device with GET: it may already be labelled (for example when only the shipment failed). If so, attach the shipment with PUT instead.

  - `Status codes` ()
    201: every device was labelled.207: at least one device failed; devices not listed in errors were labelled. Also returned whenevery device failed, except in the case below.400 with the batch body: every device failed with QRMAC_NOT_FOUND. Nothing was labelled.400 with Validation failed: the request was rejected before any device was processed.

  - `Retries` ()
    This call is not idempotent. Resending a request that already succeeded returns ALREADY_LABELLEDfor those devices. To change a label afterwards, use PUT (or override: true).

## Security:

  - `bearerAuth` (unknown)
    http bearer JWT

## Header parameters:

  - `correlation_id` (string)
    Optional request ID. Trackonomy records it with the request so support can trace a call you report.

  - `customer_id` (string)
    Only needed when your API identity has access to more than one account. Send it together
with `authorized_group` to choose the account. When your identity has a single account,
omit both headers: the account comes from the access token.

  - `authorized_group` (string)
    Only needed when your API identity has access to more than one account; send it together
with `customer_id`. `authorized_groups` is accepted as the same header.

## Request fields (application/json):

  - `shipment_details` (object)
    Shipment information. Unknown fields are ignored.

  - `shipment_details.customer_name` (string, required)
    Any string.
    Example: Acme Pharma

  - `shipment_details.customer_email` (string, required)
    Example: logistics@acmepharma.com

  - `shipment_details.customer_contact` (string)
    Omitted or `null` keeps the saved value (none on a new shipment).
    Example: +1 415 555 0100

  - `shipment_details.source_address` (object, required)
    An address. Send a free-text `label` (resolved to coordinates), `lat` + `lon` (resolved to an
address), or both (stored as sent). On `PUT`, an empty object keeps the saved location when one
is saved. An address with neither isn't rejected with `400`; the shipment write fails instead and
the device is reported as `INTERNAL_ERROR`.

  - `shipment_details.source_address.label` (string)
    Example: 1 Market St, San Francisco, CA 94105

  - `shipment_details.source_address.lat` (number)
    Example: 37.79404

  - `shipment_details.source_address.lon` (number)
    Example: -122.39504

  - `shipment_details.notes` (string)
    Omitted or `null` keeps the saved value.
    Example: Keep refrigerated

  - `shipment_details.mode_of_transport` (string)
    Case-insensitive (`air` is stored as `AIR`). Omitted or `null` keeps the saved value.
    Enum: "AIR", "GROUND", "SEA"

  - `devices` (array, required)
    1–20 devices. Duplicate `qrcode` values reject the whole request.

  - `devices.qrcode` (string, required)
    QR code printed on the device (spaces around it are trimmed). Unique within the request.
    Example: TRK0012345

  - `devices.asset_barcode` (string, required)
    Your identifier for the asset the device is attached to (for example an air waybill). On
applications configured for multi-piece shipments, a `-NNNN` piece suffix is added to the
stored value (for example `AWB-777-1001-0001`; a value that already ends in `-NNNN` is kept),
`GET` returns the suffixed value, and several devices can share one asset barcode, each
getting the next piece number.
    Example: AWB-777-1001

  - `devices.application_id` (string, required)
    Application UUID or application code (case-sensitive), for new labels and for `override` alike.
    Example: smart-asset-tracker

  - `devices.facility_id` (string)
    Facility UUID. New label: omitted or `null` means the facility is chosen automatically (see
*Facility*). `override`: omitted keeps the current facility, `null` clears it.
    Example: 0b6f7c1e-2d3a-4b5c-8d9e-0f1a2b3c4d5e

  - `devices.lat` (number)
    New labels only. Used, together with `lon`, only when you send no `facility_id` and your account has no top-level facility.
    Example: 37.7749

  - `devices.lon` (number)
    See `lat`.
    Example: -122.4194

  - `devices.tape_displayname` (string)
    Display name. New label → blank when omitted. `override` → omitted keeps it, `null` clears it.
    Example: Pallet 1

  - `devices.override` (boolean)
    Edit an already-labelled device in place instead of failing with `ALREADY_LABELLED`. Never changes the device's status. `null` is rejected.
    Example: false

  - `devices.skip_qrmac` (boolean)
    Optional, and only where enabled for your integration environment (otherwise `400`). Labels a device
that isn't in Trackonomy's device registry; requires `device_id`.
    Example: false

  - `devices.device_id` (string)
    Device MAC address, normally 12 hex characters (not format-checked). Required, and only used,
when `skip_qrmac` is `true`.
    Example: C47F510A129E

## Request examples:

  - `Every field except the optional skip_qrmac and device_id, see the skip-qrmac example` (unknown)

  - `Minimum request with one device and no shipment` (unknown)

  - `Two devices sharing one shipment` (unknown)

  - `Facility from coordinates, used only when your account has no top-level facility` (unknown)

  - `Replace the label of an already-labelled device` (unknown)

  - `Optional, only where enabled for your integration environment` (unknown)

## Response 201:

  - `201` (unknown)
    Every device was labelled.

## Response 201 fields (application/json):

  - `message` (string, required)
    `Successfully <action> <n> device(s).` when every device succeeded, otherwise `Some devices could not be <action>.`
    Example: Missing required headers: customer_id or authorized_groups

  - `successful` (integer, required)
    Number of devices that succeeded.
    Example: 1

  - `failed` (integer, required)
    Number of devices that failed.
    Example: 0

  - `errors` (array, required)
    One entry per failed device. Empty when every device succeeded.

  - `errors.qrcode` (string, required)
    QR code of the device that failed.
    Example: TRK0012345

  - `errors.error_code` (string, required)
    Machine-readable reason. Branch on this value, not on `message`.
    Example: ALREADY_LABELLED

  - `shipment_id` (string)
    The new shipment's id when `shipment_details` was sent and at least one device was labelled.
`null` when `successful` is `0`. Devices may still have been labelled (for example when only
the shipment failed), so check them with `GET`. Absent when `shipment_details` wasn't sent and at least
one device was labelled.
    Example: 9b2d4c4e-1f7a-4e8b-a0c1-3d5e7f9a1b2c

## Response 207:

  - `207` (unknown)
    At least one device failed. Devices not listed in `errors` were labelled. Also returned when
every device failed (`successful: 0`), unless every failure is `QRMAC_NOT_FOUND` (see `400`).

## Response 207 fields (application/json):

  - `message` (string, required)
    `Successfully <action> <n> device(s).` when every device succeeded, otherwise `Some devices could not be <action>.`
    Example: Missing required headers: customer_id or authorized_groups

  - `successful` (integer, required)
    Number of devices that succeeded.
    Example: 1

  - `failed` (integer, required)
    Number of devices that failed.
    Example: 0

  - `errors` (array, required)
    One entry per failed device. Empty when every device succeeded.

  - `errors.qrcode` (string, required)
    QR code of the device that failed.
    Example: TRK0012345

  - `errors.error_code` (string, required)
    Machine-readable reason. Branch on this value, not on `message`.
    Example: ALREADY_LABELLED

  - `shipment_id` (string)
    The new shipment's id when `shipment_details` was sent and at least one device was labelled.
`null` when `successful` is `0`. Devices may still have been labelled (for example when only
the shipment failed), so check them with `GET`. Absent when `shipment_details` wasn't sent and at least
one device was labelled.
    Example: 9b2d4c4e-1f7a-4e8b-a0c1-3d5e7f9a1b2c

## Response 400:

  - `400` (unknown)
    One of:
* the body failed validation (nothing was processed; `errors` lists each problem with its `path`);
* every device failed with `QRMAC_NOT_FOUND` (batch body, nothing was labelled);
* your API identity has several accounts and no account headers were sent.

## Response 400 fields (application/json):

  - `message` (string, required)
    Example: Validation failed

  - `code` (integer, required)
    Example: 400

  - `errors` (array, required)
    One entry per problem. Each entry has the `path` of the field that failed, a `message` and
an issue `code`; some issues add detail such as `expected`, `values`, `minimum`, `maximum`,
`inclusive`, `origin`, `format` or `pattern`.

  - `errors.path` (array)

  - `errors.message` (string)
    Example: Missing required headers: customer_id or authorized_groups

  - `errors.code` (string)
    Example: invalid_type

  - `message` (string, required)
    `Successfully <action> <n> device(s).` when every device succeeded, otherwise `Some devices could not be <action>.`
    Example: Missing required headers: customer_id or authorized_groups

  - `successful` (integer, required)
    Number of devices that succeeded.
    Example: 1

  - `failed` (integer, required)
    Number of devices that failed.
    Example: 0

  - `errors` (array, required)
    One entry per failed device. Empty when every device succeeded.

  - `errors.qrcode` (string, required)
    QR code of the device that failed.
    Example: TRK0012345

  - `errors.error_code` (string, required)
    Machine-readable reason. Branch on this value, not on `message`.
    Example: ALREADY_LABELLED

  - `shipment_id` (string)
    The new shipment's id when `shipment_details` was sent and at least one device was labelled.
`null` when `successful` is `0`. Devices may still have been labelled (for example when only
the shipment failed), so check them with `GET`. Absent when `shipment_details` wasn't sent and at least
one device was labelled.
    Example: 9b2d4c4e-1f7a-4e8b-a0c1-3d5e7f9a1b2c

  - `message` (string, required)
    What went wrong.
    Example: Missing required headers: customer_id or authorized_groups

  - `code` (integer)
    The HTTP status code.
    Example: 400

## Response 401:

  - `401` (unknown)
    The access token is missing, expired, issued for another environment's audience, or your API
identity isn't granted this action. The same response is returned when the request body isn't
valid JSON or is larger than 50 MB. Check the body, request a new token and retry once; if it
still fails, contact your Trackonomy integration contact.

## Response 401 fields (application/json):

  - `message` (string, required)
    What went wrong.
    Example: Missing required headers: customer_id or authorized_groups

  - `code` (integer)
    The HTTP status code.
    Example: 400

## Response 403:

  - `403` (unknown)
    The `customer_id` / `authorized_group` pair isn't one of the accounts your API identity can access, or the identity has no account.

## Response 403 fields (application/json):

  - `message` (string, required)
    What went wrong.
    Example: Missing required headers: customer_id or authorized_groups

  - `code` (integer)
    The HTTP status code.
    Example: 400

## Response 429:

  - `429` (unknown)
    More than 10 requests in 60 seconds from your API identity. The limit is shared by label,
edit label, get labels, deactivate and configuration calls. Wait and retry with backoff.

## Response 429 fields (application/json):

  - `message` (string)
    Example: Missing required headers: customer_id or authorized_groups

## Response 500:

  - `500` (unknown)
    Unexpected failure outside per-device processing. Some devices may already have been labelled:
check them with `GET` before resending.

## Response 500 fields (application/json):

  - `message` (string, required)
    What went wrong.
    Example: Missing required headers: customer_id or authorized_groups

  - `code` (integer)
    The HTTP status code.
    Example: 400

## Response 503:

  - `503` (unknown)
    Trackonomy couldn't resolve your account access. Temporary; retry with exponential backoff.

## Response 503 fields (application/json):

  - `message` (string)
    Example: Missing required headers: customer_id or authorized_groups

  - `code` (integer)
    Example: 400

  - `success` (boolean)
    Example: false

## Response 201 examples:

  - `Response to the all-fields request` (unknown)

  - `Response to the minimal request: no shipment, so no shipment_id` (unknown)

  - `Response to the with-shipment request` (unknown)

## Response 207 examples:

  - `Response to the with-shipment request: second device already labelled` (unknown)

  - `Response to the with-shipment request: no device labelled` (unknown)

## Response 400 examples:

  - `A required device field is missing` (unknown)

  - `The same qrcode twice in one request` (unknown)

  - `More than 20 devices` (unknown)

  - `Invalid customer_email` (unknown)

  - `Unsupported mode_of_transport` (unknown)

  - `Sending `skip_qrmac` where it isn't enabled` (unknown)

  - `Sending skip_qrmac without device_id where it is enabled` (unknown)

  - `No request body` (unknown)

  - `Every device has an unknown QR code` (unknown)

  - `Identity with several accounts sent no account headers` (unknown)

## Response 403 examples:

  - `Account headers outside your access` (unknown)

  - `Identity has no account access` (unknown)

