# TMS integration

This guide shows how a TMS (or any load management system) keeps one Load Market tracking per load.

## Map the load lifecycle

| In your TMS | Call |
| --- | --- |
| Load dispatched, driver phone known | `POST /create-tracking` |
| Stops or reference changed | `PATCH /edit-tracking` |
| Show position, ETA, POD | Webhooks, or `GET /get-tracking` |
| Load delivered | `PATCH /stop-tracking` |
| Load canceled before pickup | `DELETE /delete-tracking` |
| End-of-day reconciliation | `GET /get-all-tracking-items` |

## 1. Create the tracking on dispatch

Store the returned `trackingId` on the load. Use your load id as `customId` - it is echoed back on every
read and in webhooks, so you can match updates without a lookup table.

```bash
curl -X POST https://api.load-market.com/v1/create-tracking \
  -H "x-api-key: YOUR_API_KEY" \
  -H "x-api-client-key: YOUR_CLIENT_KEY" \
  -H "Idempotency-Key: tms-load-48213-dispatch-1" \
  -H "Content-Type: application/json" \
  -d '{
    "driverPhone": 5555550100,
    "refId": "LM-48213 CHI-DAL",
    "customId": "tms-load-48213",
    "expDate": 1793800800000,
    "loadInstructions": "Call the receiver 30 min before arrival. Dock 12.",
    "createdByTms": {
      "companyName": "Prairie Freight Brokers",
      "displayName": "Dana Miller",
      "phoneNumber": "+15555550123",
      "email": "dana.miller@example.com"
    },
    "stopLocations": [
      { "streetNumber": "3601", "streetName": "S Ashland Ave", "city": "Chicago", "stateProvince": "IL", "zip": "60609", "country": "US", "customStopId": "PU-1" },
      { "streetNumber": "4500", "streetName": "Irving Blvd", "city": "Dallas", "stateProvince": "TX", "zip": "75247", "country": "US", "customStopId": "DEL-1" }
    ]
  }'
```

Tips:

- **`Idempotency-Key`**: one key per create attempt you mean (your load id plus a dispatch counter works).
  Retry a request that timed out with the SAME key - a create that went through answers its first response
  again, so nothing is created or billed twice. See [Errors](/errors#idempotency).
- **Expiration**: set `expDate` to the planned delivery plus a buffer (at most 10 days ahead). More than
  5 days counts as 2 tracking requests.
- **`createdByTms`** shows your dispatcher to the driver in the app, so the driver knows who is asking.
  Add `"hideChat": true` if you do not want in-app chat.
- **`customStopId`** on each stop lets you match arrival confirmations to your stop records.
- **Multi-tenant TMS**: every customer organization has its own client key. Store it per customer
  (encrypted) and send it with that customer's requests.
- **Driver without the app**: the response says `Pending tracking created` and `driverHasApp: false`.
  The driver gets an SMS. Show "waiting for driver" in your UI. When `driverInvite.smsStatus` turns
  `undeliverable` or `optedOut` (also the webhook events `driver.sms_undeliverable` / `driver.sms_opted_out`),
  the text cannot reach the driver: send `driverInvite.appLink` another way, or call the driver.
- **Truck with an ELD**: when the organization has ELD tracking enabled, send `trackingSource` `eld`
  or `hybrid` with the carrier's DOT number and the truck number. The status is `requestEld` until the
  carrier approves by email - show "waiting for carrier". See [ELD and Hybrid tracking](/eld-tracking).

## 2. Keep it in sync

When stops change, send the full new stop list - `stopLocations` replaces the old one:

```bash
curl -X PATCH "https://api.load-market.com/v1/edit-tracking?trackingId=api-7fK2mQ9xLpR4tN8vB1cD" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "stopLocations": [
        { "streetNumber": "3601", "streetName": "S Ashland Ave", "city": "Chicago", "stateProvince": "IL", "zip": "60609", "country": "US", "customStopId": "PU-1" },
        { "streetNumber": "2201", "streetName": "E Loop 820 N", "city": "Fort Worth", "stateProvince": "TX", "zip": "76118", "country": "US", "customStopId": "DEL-2" }
      ] }'
```

A stop that is still the same stop keeps its arrival/departure status and its Smart Arrival status: the same
`customStopId` within 100 m of where it was. Without ids only the same position and place count, and not when
stops were removed or inserted and another stop is at the same place (a round trip's return is never taken for
its pickup). Send `customStopId` on every stop, then you can insert, remove or reorder stops without losing the
confirmed ones. A stop that moved to another address starts over.

`expDate`, `schedule`, `loadInstructions` and `emailUpdates` cannot be edited. Delete and recreate the
tracking if they must change (the new tracking is billed again).

## 3. Receive updates

Subscribe to [webhooks](/webhooks) and update the load from each item:

- `events` - what happened since the last delivery: `stop.arrived` / `stop.departed` (with your
  `customStopId`), `stop.alert_status_changed` (a stop turned `late`), `driver.rejected`, `driver.paused`,
  `carrier.rejected`, `document.uploaded` and the rest of the [event types](/webhooks#events). Post them to
  your load's timeline and deduplicate them by `id`.
- `data.status.value` - show the driver's state (see [Tracking lifecycle](/tracking-lifecycle)).
- `data.driverCoords` - last position and `lastUpdate`.
- `data.stopLocations[i].locationStatus` - arrived / completed per stop.
- `data.stopLocations[i].stopAlertStatusInfo.stopAlertStatus` - `onTime`, `tight` or `late` (PRO), `null` when
  it cannot be estimated (for example the driver deleted the app account). A stop without `expectedArrival` has
  `stopAlertStatusInfo: null`.
- `data.fraudAlerts.alerts` - [Potential Fraud Alerts](/fraud-alerts) about the driver's phone (PRO).
  Show them to a person who reviews the load, never act on them automatically.
- `data.driverFiles` - BOL / POD uploads.

See [Live location via webhooks](/guides/live-location-webhooks) for a receiver.

## 4. Finish or cancel

```bash
# Delivered: finish the tracking (it moves to history)
curl -X PATCH "https://api.load-market.com/v1/stop-tracking?trackingId=api-7fK2mQ9xLpR4tN8vB1cD" \
  -H "x-api-key: YOUR_API_KEY"

# Canceled: remove it completely
curl -X DELETE "https://api.load-market.com/v1/delete-tracking?trackingId=api-7fK2mQ9xLpR4tN8vB1cD" \
  -H "x-api-key: YOUR_API_KEY"
```

When the tracking time (`expDate`) runs out, the server finishes the tracking item automatically
(status `done`, or `doneEld` for ELD and Hybrid). A pending installation tracking item the driver never accepts is removed by the
server after its tracking time runs out and never appears in history.

## 5. Reconcile

Once a day compare your open loads with `GET /get-all-tracking-items?type=active`, and pull finished
tracking items with the [daily history sync](/guides/daily-history-sync).

## Handle failures

- A create that times out may still have succeeded - retry it with the same `Idempotency-Key` and you get
  its response instead of a second tracking. Without the header, check the active list for your `customId`
  before retrying (creates are billed).
- Log `requestId` from every error.
- Branch on `code`, not on `message` - see [Errors](/errors).
