Overview
The Drivers API provides endpoints to query and manage driver data, including real-time location, Hours of Service (HOS), and editable profile information. Database Tables:drivers- Real-time driver data from Terminal API (polled every 60s)driver_profiles- Editable profile data (user-managed)
- Terminal API (
/tsp/v1/drivers) - Provides driver identity, contact info, status - Terminal API (
/tsp/v1/hos/available-time) - Provides HOS data (synced every 5 minutes) - Frontend - User edits to profiles
Get Current Drivers
GET /api/drivers/current
Get all drivers from the database with staleness metadata. This is a DB-first read endpoint.Maximum number of drivers to return (1-1000)
ISO 8601 timestamp - only return drivers updated after this time
- Query Database: Uses
get_drivers_snapshot()which queries thedriverstable - Window Function: Uses PostgreSQL
ROW_NUMBER()to get latest row perterminal_driver_id - Name Construction: Combines
first_name,middle_name,last_nameintodriver_name - Staleness Check: Compares current time to
max(updated_at)across all drivers - Stale Threshold: Data is considered stale if
staleMs > DRIVER_MAX_STALE_SECONDS * 1000(default: 300 seconds)
| Header | Description |
|---|---|
X-Data-Source | Always db |
X-Stale-Ms | Milliseconds since most recent update |
ETag | MD5 hash of payload |
Last-Modified | HTTP date of most recent update |
Retry-After | Seconds to wait (only on 202) |
Real-Time Driver Stream
GET /api/drivers/stream
Server-Sent Events (SSE) endpoint for real-time driver updates. Event Types:connected
connected
Sent immediately when client connects.
driver_update
driver_update
Sent when new driver data is available.
- Connection: Client connects, receives unique
client_id - Initial Data: Immediately reads from
driverstable viaget_drivers_snapshot()and sends firstdriver_update - Polling Wake-up: Notifies
DriverPollingServiceto switch to fast polling - Live Updates: Receives broadcasts from
DriverPollingServiceafter each poll - Heartbeat: Sends
:heartbeatevery 30 seconds
DriverPollingServicepolls Terminal API/tsp/v1/driversevery 60 seconds- Upserts results to
driverstable viaupsert_drivers_batch() - Broadcasts to SSE clients
Hours of Service (HOS)
GET /api/drivers/hos/available-time
Get HOS available time for a driver. Reads from thedrivers table (HOS columns synced every 5 minutes).
Terminal driver ID (e.g.,
drv_01D8ZQFGHVJ858NBF2Q7DV9MNC)| Field | Description |
|---|---|
cycle | Total remaining time for current cycle (e.g., 70 hours) in milliseconds |
shift | Total remaining time for current shift (e.g., 14 hours) in milliseconds |
drive | Total remaining drive time for shift (e.g., 11 hours) in milliseconds |
break | Time before next break required (e.g., 8 hours) in milliseconds |
- Queries
driverstable byterminal_driver_id - Reads HOS columns:
hos_current_status,hos_cycle_available_ms,hos_shift_available_ms,hos_drive_available_ms,hos_break_available_ms - Returns 404 if driver not found or HOS data incomplete
- HOS data is synced from Terminal API
/tsp/v1/hos/available-timebyDriverHOSSyncService - Runs every 5 minutes automatically
- Updates
driverstable HOS columns viaupdate_drivers_hos_batch()
POST /api/drivers/hos/sync
Manually trigger HOS data sync from Terminal API. Response:- Calls
DriverHOSSyncService.sync_hos_available_time() - Fetches HOS data from Terminal API for all drivers with
terminal_driver_id - Updates
driverstable HOS columns in batch
Driver Location
PATCH /api/drivers/
Update location for a specific driver.Terminal driver ID (e.g.,
drv_01D8ZQFGHVJ858NBF2Q7DV9MNC)- Updates
driverstable columns:latitude,longitude,location_updated_at,updated_at - Uses SQL UPDATE with WHERE on
terminal_driver_id - Returns 404 if driver not found
GET /api/drivers/source//location
Get driver location bysource_id (McLeod driver ID).
Driver source ID from provider system (e.g.,
M72323)- Queries
driverstable bysource_idcolumn - Returns latest row using window function
- Returns 404 if driver not found
Lane Preferences
PATCH /api/drivers//lane-preferences
Update lane preferences for a driver.Terminal driver ID (e.g.,
drv_01D8ZQFGHVJ858NBF2Q7DV9MNC)[] or null to clear preferences.
Response:
- Updates
drivers.lane_preferencecolumn (PostgreSQL array) - Returns 404 if driver not found
Driver Profiles
Driver profiles are stored in a separatedriver_profiles table for user-editable data.
GET /api/drivers/profiles
Get all driver profiles. Response:- Queries
driver_profilestable, ordered byfull_name - Returns all profile fields
GET /api/drivers/profile/
Get a single driver profile.Terminal driver ID (e.g.,
drv_01D8ZQFGHVJ858NBF2Q7DV9MNC)/profiles response.
PATCH /api/drivers/profile/
Update driver profile (partial updates supported).Terminal driver ID
- Only updates fields provided in request body (partial update)
- Automatically sets
updated_attimestamp - Returns 404 if profile not found
POST /api/drivers/profile/backfill
Backfilldriver_profiles table from drivers table. Creates profiles for drivers that don’t have one yet.
Response:
- Queries all unique drivers from
driverstable - For each driver without a profile, creates new row in
driver_profiles - Copies:
terminal_driver_id,full_name(from name parts),phone_number,dispatcher,driver_status - Uses
ON CONFLICT DO NOTHING- does NOT overwrite existing profiles
POST /api/drivers/profile/backfill-reset
⚠️ WARNING: Overwrites ALL existing profile data. Reset and backfilldriver_profiles from drivers table. Creates new profiles and overwrites existing ones.
Response:
- Queries all unique drivers from
driverstable - Uses
ON CONFLICT DO UPDATE- overwrites ALL fields - Resets editable fields (address, emergency_contact, etc.) to null/defaults
- User edits will be lost
Background Services
DriverPollingService
- Interval: 60 seconds (when SSE clients connected), 300 seconds (idle)
- Source: Terminal API
/tsp/v1/drivers - Target:
driverstable viaupsert_drivers_batch() - Config:
LOCATION_POLL_ENABLED=true
DriverHOSSyncService
- Interval: Every 5 minutes
- Source: Terminal API
/tsp/v1/hos/available-time - Target:
driverstable HOS columns viaupdate_drivers_hos_batch() - Runs: Automatically as background task

