Limit the total distance of each Driver's route
Requires Route Optimization v3 (Scale and Enterprise plans). Based URL: POST /api/v2/optimizations/scheduled.
The optional maxDistance parameter caps how far a single route may run from start to finish. Use it when vehicles have a range limit; an EV battery, a shift mileage allowance, a contractual cap. If you leave it out, routes have no distance limit and optimization works the same as today.
This is a cap on one route's total length. To limit the distance between two consecutive stops instead, see Route Optimization Max Distance Between Destinations.
| Name | Type | Description |
|---|---|---|
maxDistance | Number | Optional. Maximum total distance of one route, in your organization's distance unit: kilometers for metric organizations, miles for imperial organizations. Must be greater than 0. Decimals are allowed. Omit for no limit. |
🚧 Distance unitThe value is not in meters. It uses the same distance unit as the rest of your organization: 50 means 50 km on a metric organization and 50 miles on an imperial one. The response does not echo the unit back, so check your organization's unit setting before choosing a value.
curl -X POST "https://onfleet.com/api/v2/optimizations/scheduled" \
-u "<your_onfleet_api_key>:" \
-H "Content-Type: application/json" \
-d '{
"maxTasksPerRoute": 30,
"date": 1718728193838,
"timezone": "America/Toronto",
"mode": 1,
"serviceTime": 15,
"maxViolationTime": 0,
"schedulingMethod": 1,
"routeStart": "teams://DEFAULT",
"routeEnd": "teams://DEFAULT",
"maxDistance": 50,
"tasks": [
"atnk3APxhJrgfx9mgWaRhByQ",
"4kA7bOH*Kz7K0YA6~NpqWDke"
],
"teams": {
"5y1jHFp7OBfFU6SekbwwwRfL": [
"GLlLB2123ABCkXOJAHPklMPZ",
"~jCf16ZaT07pdtf~oFb~XAH9"
]
},
"defaultSchedule": [
{
"date": 1718683200000,
"slots": [{ "start": "0000", "end": "2359" }],
"owner": "pOoPktB~Hry706w2JT30rJGi",
"timezone": "America/Toronto"
}
]
}'Per-driver limit: vehicle.maxDistance
vehicle.maxDistanceYou can also store a range limit on each driver's vehicle, so every driver-based optimization respects it without you sending maxDistance each time. Set it on the worker with POST /api/v2/workers or PUT /api/v2/workers/{workerId}, inside the vehicle object. GET /api/v2/workers/{workerId} returns it. If you never set it, the vehicle has no limit.
| Name | Type | Description |
|---|---|---|
vehicle.maxDistance | Number | Optional. Maximum total distance of one route for this driver, in your organization's distance unit (kilometers or miles), the same unit as the top-level maxDistance. Must be greater than 0. Decimals are allowed. Send null to remove the limit. Returned as null when not set. |
Send the whole vehicleWhen a
PUT /api/v2/workers/{workerId}request includes avehicleobject, it replaces the worker's whole vehicle, and the vehicle gets a newid. Any vehicle field you leave out is cleared: sending{ "vehicle": { "type": "CAR", "maxDistance": 25 } }also clearsdescription,licensePlateandcolor. To change only the limit, first read the vehicle withGET /api/v2/workers/{workerId}, then send every field back with the newmaxDistance.typeis always required. APUTwithout avehicleobject leaves the vehicle unchanged.
Vehicle Max DistanceThis value must be set first on the Worker's object by using Update Worker Endpoint first. Do this for any Worker's vehicle.
Example Request
curl -X PUT "https://onfleet.com/api/v2/workers/tWkr4m~Lq9ZxP2vHcN8sD3fA" \
-u "<your_onfleet_api_key>:" \
-H "Content-Type: application/json" \
-d '{
"vehicle": {
"type": "CAR",
"description": "Nissan Leaf",
"licensePlate": "EV4821",
"color": "white",
"maxDistance": 120
}
}'On a metric organization, this request limits each of this driver's optimized routes to 120 km and keeps the vehicle's other details.
{
"vehicle": {
"id": "k9QTL~GZyBT8DHRn1TpLNzk1",
"type": "CAR",
"description": "Nissan Leaf",
"licensePlate": "EV4821",
"color": "white",
"maxDistance": 120,
"timeCreated": 1795565203000,
"timeLastModified": 1795565203362
}
}Which limit applies
Driver-based and service-area optimizations: If the request includes a top-level maxDistance, it applies to every driver in the run and overrides each vehicle's maxDistance, whether that value is higher or lower. If the request leaves maxDistance out, each driver uses their own vehicle.maxDistance. A driver with neither has no limit.
Vehicle-based optimizations: Neither the top-level maxDistance nor the drivers' vehicles are used. Set maxDistance on each vehicle type instead, inside optimizationByVehicle.vehicleTypes (see Initialize Vehicle Based Route Optimization). A selected type without maxDistance has no limit.
"optimizationByVehicle": {
"vehicleTypes": {
"car": { "selected": true, "count": 2, "maxTasksPerRoute": 10, "maxDistance": 200 },
"bicycle": { "selected": true, "count": 3, "maxTasksPerRoute": 8, "maxDistance": 20 }
}
}Auto-Dispatch and re-optimization: The same rules apply. A re-optimization reads each driver's vehicle again when it runs, so a changed vehicle.maxDistance takes effect.
Nothing set anywhere: Routes have no distance limit, the same as today.
