# Track stops

> Record when a truck arrived at and left each stop of a Techvia TMS load through the API, and see detention worked out from the two times.

Source: https://techvia.software/docs/tms/use-cases/track-stops/

**Outcome:** your telematics, driver app or script reports arrivals and departures, the TMS timeline shows them, and detention is worked out from the two times by your workspace's detention settings.

## Steps

1. **Make a key** with `loads.read` and `loads.write`. Store it as `TMS_KEY`.
2. **Read the load** to get each stop's `id`: `GET /api/public/v1/loads/{id}` returns `stops[]` with `id`, `order`, `type`, `scheduledLocal`, `arrivedAt` and `departedAt`.
3. **Record the arrival** with `PUT /api/public/v1/loads/{id}/stops/{stopId}/arrival`. The body `{ "at": "2026-12-01T09:05:00" }` is optional: left out, it means now. With an offset it is that instant; without one it is the local time at the stop's facility. More than a day ahead is a `422`.
4. **Record the departure** the same way with `.../departure`. A departure before any arrival, or before the arrival, is a `422`.
5. **Read the load again.** `arrivedAt` and `departedAt` are filled (UTC), and any detention appears in `autoCharges` for a key with `billing.read` or `loads.rates`.

**curl**

```bash
curl -X PUT "https://tms-api.techvia.software/api/public/v1/loads/$LOAD_ID/stops/$STOP_ID/arrival" \
  -H "Authorization: Bearer $TMS_KEY" -H "Content-Type: application/json" \
  -d '{"at":"2026-12-01T09:05:00"}'

curl -X PUT "https://tms-api.techvia.software/api/public/v1/loads/$LOAD_ID/stops/$STOP_ID/departure" \
  -H "Authorization: Bearer $TMS_KEY" -H "Content-Type: application/json" \
  -d '{"at":"2026-12-01T11:30:00"}'
```

**JavaScript**

```js
const base = 'https://tms-api.techvia.software/api/public/v1/loads';
const headers = { Authorization: `Bearer ${process.env.TMS_KEY}`, 'Content-Type': 'application/json' };

async function stamp(loadId, stopId, kind, at) {
  const res = await fetch(`${base}/${loadId}/stops/${stopId}/${kind}`, {
    method: 'PUT', headers, body: JSON.stringify(at ? { at } : {}),
  });
  if (!res.ok) throw new Error(await res.text());
  return res.json(); // the load as GET returns it
}

await stamp(loadId, stopId, 'arrival', '2026-12-01T09:05:00');
await stamp(loadId, stopId, 'departure', '2026-12-01T11:30:00');
```

**Python**

```python

BASE = "https://tms-api.techvia.software/api/public/v1/loads"
headers = {"Authorization": f"Bearer {os.environ['TMS_KEY']}"}

def stamp(load_id, stop_id, kind, at=None):
    res = requests.put(f"{BASE}/{load_id}/stops/{stop_id}/{kind}", headers=headers, json={"at": at} if at else {})
    res.raise_for_status()
    return res.json()  # the load as GET returns it

stamp(load_id, stop_id, "arrival", "2026-12-01T09:05:00")
stamp(load_id, stop_id, "departure", "2026-12-01T11:30:00")
```

## Good to know

- The earliest arrival wins (a later one changes nothing) and a stop has one departure (a second changes nothing), so a repeated call is safe.
- These calls take no `rowVersion`. A call that collides with another change of the same load at that moment is a `409` ("Retry the call").
- An unknown stop or load is `404`. The timeline shows "Arrived @ facility (API key: your key name)".

## Next steps

- [Record arrival](/docs/tms/reference/loads/record-arrival-at-a-stop/) and [record departure](/docs/tms/reference/loads/record-departure-from-a-stop/) in the reference.
- The rules for the rest of a load: [Loads guide](/docs/tms/guides/loads/).
- Let an AI record them: [Run dispatch from Claude](/docs/tms/use-cases/run-dispatch-from-claude/).
