# ELD and Hybrid tracking

import { Mermaid } from "zudoku/mermaid";

A tracking can follow the driver's phone (the default), the truck's ELD (electronic logging device), or
both. Choose with `trackingSource` on `POST /create-tracking`:

| `trackingSource` | What reports the position | Needs |
| --- | --- | --- |
| `mobile` (default) | The driver's phone, in the Load Market driver app | `driverPhone` |
| `eld` | The truck's ELD only - no phone, no driver app | `eld` |
| `hybrid` | The truck's ELD and the driver's phone | `eld` and `driverPhone` |

An ELD tracking reads the position from the carrier's ELD provider, so it works without the driver
installing anything. A hybrid tracking keeps everything the driver app brings (load instructions, chat,
BOL/POD files) and still has a position when the phone is off.

:::note{title="Enabled per organization"}

Load Market enables ELD and Hybrid tracking for each organization. Email office@load-market.com with the
organization's name to turn them on. Until then `trackingSource` `eld` or `hybrid` answers
`403 ELD_TRACKING_NOT_AVAILABLE`. The carrier must also have connected its ELD to Load Market at
[eld.load-market.com](https://eld.load-market.com).

:::

## Before you start

- **Client key.** ELD and Hybrid tracking items need the organization's client key in
  `x-api-client-key`. Created by `mcNum` / `dotNum` the request answers `400 CLIENT_KEY_REQUIRED`, unless
  your partner key is allowlisted for that organization. See [Authentication](/authentication).
- **Carrier.** `eld.carrierDotNumber` is the US DOT number of the carrier whose truck you track - not
  your organization's. A carrier that has not connected its ELD answers `400 ELD_CARRIER_NOT_INTEGRATED`,
  one whose ELD connection is not working right now `400 ELD_CONNECTION_UNAVAILABLE`.
- **Truck.** `eld.truckNumber` is the truck (unit) number as the carrier's ELD knows it. Load Market
  checks it with the carrier's ELD provider before anything is created, and answers
  `400 ELD_VEHICLE_NOT_FOUND` when the ELD does not know it, or when the truck's ELD plan does not
  include location tracking (the ELD would never send its location).
- **Billing.** ELD and Hybrid tracking items consume tracking requests exactly like phone tracking
  items. See [Billing and usage](/billing-and-usage).

## Create one

ELD only:

```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 "Content-Type: application/json" \
  -d '{
    "trackingSource": "eld",
    "eld": {
      "carrierDotNumber": 3012345,
      "truckNumber": "1042",
      "approvalEmails": ["dispatch@acme-freight.com"]
    },
    "refId": "LM-48655 KC-STL",
    "expDate": 1793800800000,
    "stopLocations": [
      { "streetNumber": "1601", "streetName": "Swift Ave", "city": "North Kansas City", "stateProvince": "MO", "zip": "64116", "country": "US" },
      { "streetNumber": "2101", "streetName": "Cass Ave", "city": "St. Louis", "stateProvince": "MO", "zip": "63106", "country": "US" }
    ]
  }'
```

Expected response:

```json
{
  "message": "Tracking request created",
  "trackingId": "api-Ey4nC7tR1wQ8zK5mV2bX",
  "customId": null,
  "status": { "value": "requestEld", "description": "Pending ELD admin approval" },
  "driverHasApp": false,
  "billingWeight": 1,
  "apiUsage": { "planName": "Premium", "totalTrackings": 300, "usedTrackings": 131, "currentPeriodEnd": 1794484800000 }
}
```

Hybrid - the same `eld` block, plus `driverPhone`:

```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 "Content-Type: application/json" \
  -d '{
    "trackingSource": "hybrid",
    "driverPhone": 5555550100,
    "eld": { "carrierDotNumber": 3012345, "truckNumber": "1042" },
    "refId": "LM-48702 KC-STL",
    "expDate": 1793800800000
  }'
```

Every other create field works as for a phone tracking: `schedule` (the status is then `scheduledEld`),
`stopLocations`, `emailUpdates`, `customId`, `metadata`, `createdByTms`. For an ELD only tracking:

- `driverPhone` and `loadInstructions` are ignored - there is no driver app.
- The emails never carry the Live Tracking Link: `emailUpdates.realTimeTrackingLink` is always `false`.
- `publicLink` is returned once the carrier approved, so you can still share it yourself.

### The `eld` block

| Field | Required | Description |
| --- | --- | --- |
| `carrierDotNumber` | Yes | US DOT number of the carrier. A numeric string is accepted |
| `truckNumber` | Yes | The truck number in the carrier's ELD, up to 100 characters |
| `providerId` | No | Leave it out and the carrier's connected, working ELD is used. Send it only when Load Market told you which of the carrier's ELD connections to use |
| `approvalEmails` | No | Up to 10 addresses that receive the carrier's approval email - see [Carrier approval](#carrier-approval) |

A field with the wrong shape answers `400 ELD_DATA_INVALID`, and the message names the field. An `eld`
block sent without `trackingSource` `eld` or `hybrid` is ignored and a phone tracking is created.

`trackingSource` is not case-sensitive, and an empty string or `null` means `mobile`. Once ELD tracking
is enabled for the organization, any other value answers `400 TRACKING_SOURCE_INVALID`.

## Carrier approval

Every ELD and Hybrid tracking needs the carrier's consent before the truck's position is shared. Right
after the create, the carrier gets an email with three links: **Start** (approve), **Stop** (pause) and
**Cancel** (refuse).

- The email goes to `approvalEmails` when you send them, otherwise to the approval emails the carrier
  listed when it connected its ELD, otherwise to the carrier's company email.
- Every address in `approvalEmails` must be on the carrier's own company email domain, or on the
  carrier's own approval list. Public email providers (Gmail, Yahoo, Outlook and similar) are accepted
  only when the carrier listed the address itself. Anything else answers `400 ELD_DATA_INVALID`.
- The Start link is valid for 24 hours. A tracking the carrier does not approve in time stays
  `requestEld` until its tracking time runs out - create a new one, or delete it.
- Stop and Cancel work at any time, and Start also resumes a tracking the carrier paused.

| Link | ELD only | Hybrid |
| --- | --- | --- |
| Start | `requestEld` becomes `activeEld` and the truck's ELD starts reporting | The truck's ELD starts reporting. `requestEld` becomes `activeEld` |
| Stop | The truck's ELD stops reporting, the status stays | The truck's ELD stops reporting, the phone keeps reporting |
| Cancel | `cancelEld` - the tracking ends | Only the ELD part ends. The tracking ends (`cancelEld`) once the driver declined too |

A carrier pause is not a status of its own: the status stays, and positions stop until the carrier
resumes.

## Hybrid: two approvals

A hybrid tracking has two parts. The driver gets the usual invitation (a push notification, or a text
message to install the app) and the carrier gets the approval email. The tracking starts with whichever
answer comes first:

- **The driver accepts first** - `activeEld`, the phone reports. The truck's ELD joins once the carrier
  approves.
- **The carrier approves first** - `activeEld`, the truck's ELD reports, also when the driver has not
  installed the app yet (`driverHasApp: false` until the driver is known).
- **The driver pauses the phone** - `stopEld` ("Paused by the driver (ELD continuing)"), and the truck's
  ELD keeps reporting.
- **Either side declines** - only that part ends. The tracking ends (`cancelEld`) once both declined.

While the phone reports, the position comes from the phone. Otherwise it comes from the truck's ELD.

## Statuses

| `value` | `description` | Meaning |
| --- | --- | --- |
| `requestEld` | Pending ELD admin approval | Waiting for the carrier (a hybrid also for the driver) |
| `scheduledEld` | Scheduled ELD | Waiting for `schedule.start` |
| `activeEld` | Active ELD | The location is shared |
| `stopEld` | Paused by the driver (ELD continuing) | Hybrid: the driver paused the phone, the truck's ELD keeps reporting |
| `cancelEld` | Rejected by ELD admin | The carrier refused (a hybrid once the driver declined too) |
| `doneEld` | ELD tracking finished | Stopped with `stop-tracking` or in the Load Market web app, or expired |

`noAccount` and `uninstallAccount` can replace `stopEld` the same way they replace `stop` - see
[Tracking lifecycle](/tracking-lifecycle). New status values may be added over time, so keep showing
`status.description` for a value you do not know.

<Mermaid
  chart={`
stateDiagram-v2
    direction LR
    [*] --> scheduledEld: create with schedule
    [*] --> requestEld: create
    scheduledEld --> activeEld: schedule.start, carrier already approved
    scheduledEld --> requestEld: schedule.start, not approved yet
    requestEld --> activeEld: carrier approves, or the hybrid driver accepts
    requestEld --> cancelEld: carrier refuses (ELD only)
    activeEld --> stopEld: hybrid driver pauses
    stopEld --> activeEld: driver resumes
    activeEld --> cancelEld: carrier cancels (hybrid - both declined)
    activeEld --> doneEld: stop-tracking or expDate
    stopEld --> doneEld: stop-tracking or expDate
    cancelEld --> doneEld: stop-tracking or expDate
    doneEld --> [*]
`}
/>

While an ELD or Hybrid tracking waits for the carrier and is not in the driver's app yet, `get-tracking`
returns it in the pending shape (`TrackingPendingData`, with `driverPhone: null` for ELD only), and
`stop-tracking` does not find it - use `delete-tracking`. An ELD only tracking the carrier refused before
approving stays `cancelEld` until it is deleted or its tracking time runs out.

## Positions

- The truck's position is read from the carrier's ELD provider about once a minute.
  `driverCoords.lastUpdate` is the time the ELD recorded it, which can be a few minutes before the read.
- `battery`, `accuracy` and `locationSource` describe the driver's phone, so they are `null` for a
  position from the truck's ELD.
- `driverCoords` is filled while the tracking is `activeEld` or `stopEld`. See
  [Driver position](/tracking-lifecycle#driver-position).
- `driverHasApp` is always `false` for an ELD only tracking. A hybrid has `true` once its driver is
  known.
- [Potential Fraud Alerts](/fraud-alerts) describe the driver's phone: always `null` for an ELD only
  tracking, and for a hybrid until the driver accepted it in the app.
- `get-reports` lists the reported positions like for any tracking.

## Webhooks

- An ELD only tracking sends every update from the moment the carrier approves it (`carrier.approved`). A
  hybrid sends them once the driver has the app or the carrier approved, whichever comes first.
- Before that, the request sends only items with [events](/webhooks#events), with the pending shape
  (`TrackingPendingData`). A carrier that refuses before approving sends the last one: status `cancelEld` with
  the event `carrier.rejected`.
- The last update is `doneEld` or `cancelEld`. No more updates follow it.
- A carrier pause or resume does not change the status. It is sent as the event `carrier.paused` or
  `carrier.resumed`.

See [Webhooks](/webhooks).

## Edit, stop and delete

- `edit-tracking` changes the same fields as for a phone tracking. The carrier, the truck and
  `trackingSource` cannot change - create a new tracking instead.
- `stop-tracking` ends it with `doneEld`.
- `delete-tracking` works in every status.

## Errors

Nothing is created and nothing is billed when the create fails.

| HTTP | `code` | Cause | Fix |
| --- | --- | --- | --- |
| 403 | `ELD_TRACKING_NOT_AVAILABLE` | ELD tracking is not enabled for the organization | Ask Load Market to enable it |
| 400 | `CLIENT_KEY_REQUIRED` | Created by `mcNum` / `dotNum` without the organization's client key | Send `x-api-client-key` |
| 400 | `TRACKING_SOURCE_INVALID` | `trackingSource` is not `mobile`, `eld` or `hybrid` | Send one of the three |
| 400 | `ELD_DATA_INVALID` | The `eld` block is missing or has a wrong field, `providerId` is not one of the carrier's, or an approval email is not allowed | Fix the field the message names |
| 400 | `ELD_CARRIER_NOT_INTEGRATED` | The carrier has not connected its ELD to Load Market | Ask the carrier to connect at eld.load-market.com, or track by phone |
| 400 | `ELD_CONNECTION_UNAVAILABLE` | The carrier's ELD connection is not working | Ask the carrier to reconnect its ELD, or track by phone |
| 400 | `ELD_VEHICLE_NOT_FOUND` | The carrier's ELD does not know `truckNumber`, or its ELD plan does not include location tracking for that truck | Check the truck number with the carrier, or ask the carrier to enable location tracking for the truck |
| 400 | `DRIVER_PHONE_REQUIRED` | A hybrid without `driverPhone` | Send `driverPhone`, or use `eld` |
| 429 | `RATE_LIMITED` | Too many unknown truck numbers for the organization in a short time | Wait the seconds in `Retry-After` and check the truck numbers |
| 503 | `ELD_PROVIDER_UNAVAILABLE` | The carrier's ELD provider did not answer the truck check | Retry after the seconds in `Retry-After` |
| 504 | `UPSTREAM_TIMEOUT` | The truck check took too long | Retry with backoff |

See [Errors](/errors) for the error format.

## Testing

Test keys create ELD and Hybrid test tracking items without contacting any carrier or ELD provider: only
the shape of the `eld` block is checked, and the organization still needs ELD tracking enabled. Drive the
carrier's side with the `carrier.approved`, `carrier.paused`, `carrier.resumed` and `carrier.rejected`
events:

```bash
curl -X POST https://api.load-market.com/v1/test/simulate \
  -H "x-api-key: YOUR_TEST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "trackingId": "api-test-Gn5vR8kQ2wM7xT4cL9pB", "event": "carrier.approved" }'
```

A hybrid test tracking with `driverPhone` `5555550100` starts in the driver's app, any other number as
pending installation. See [Test mode](/test-mode) for every event.

## Checklist

- Ask Load Market to enable ELD tracking, and send the organization's client key.
- Handle the ELD statuses and keep showing `status.description` for any value you do not know.
- Tell your users that the carrier approves by email, and look at `get-tracking` when nothing happens.
