- Home
- Docs
- API reference
- Best order to visit a day's stops
API reference
Best order to visit a day's stops
Returns the visiting order that minimizes total drive time for up to 100 stops, with road distance and time per leg and in total. Typical use: a sales rep's or rider's daily plan. By default the first stop is the start (e.g. home or depot) and the plan ends at whichever stop is best; use `end: "start"` to come back, or `end: "last"` to finish at the last stop given. Starting and ending anywhere is not supported. Billed as one `optimize` unit per stop.
Endpoint
https://api.wayline.cloud/v1/optimizeRequires an API key (authentication). Product overview: Route Optimization API. Billing: see units & pricing.
Request body
JSON object (OptimizeRequest). Points accept "lat,lng" strings, {lat, lng} objects or [lat, lng] arrays.
| Field | Type | Description |
|---|---|---|
stopsrequired | Point[] | |
start | string | first: the plan starts at stops[0] (home, depot). any: best start.One of: first, anyDefault: first |
end | string | any: finish wherever is best. last: finish at the last stop given. start: return to the start.One of: any, last, startDefault: any |
profile | string | One of: car, driving, motorcycle, bike, motorbike, two_wheeler, bicycle, cyclingDefault: car |
units | string | One of: metric, imperialDefault: metric |
geometry | string | One of: none, polyline, polyline6, geojsonDefault: none |
Example
curl -X POST "https://api.wayline.cloud/v1/optimize" \
-H "Authorization: Bearer $WAYLINE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"stops":[{"lat":37.788,"lng":-122.4075},{"lat":37.8313,"lng":-122.2852},{"lat":37.8716,"lng":-122.2727},{"lat":37.9358,"lng":-122.3478},{"lat":37.8044,"lng":-122.2712}],"end":"start","units":"imperial"}'const res = await fetch("https://api.wayline.cloud/v1/optimize", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WAYLINE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
"stops": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
},
{
"lat": 37.8044,
"lng": -122.2712
}
],
"end": "start",
"units": "imperial"
}),
});
if (!res.ok) throw new Error(`Wayline ${res.status}: ${await res.text()}`);
console.log(await res.json());import os, requests
res = requests.post(
"https://api.wayline.cloud/v1/optimize",
json={
"stops": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
},
{
"lat": 37.8044,
"lng": -122.2712
}
],
"end": "start",
"units": "imperial"
},
headers={"Authorization": f"Bearer {os.environ['WAYLINE_API_KEY']}"},
timeout=10,
)
res.raise_for_status()
print(res.json())package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
body := `{"stops":[{"lat":37.788,"lng":-122.4075},{"lat":37.8313,"lng":-122.2852},{"lat":37.8716,"lng":-122.2727},{"lat":37.9358,"lng":-122.3478},{"lat":37.8044,"lng":-122.2712}],"end":"start","units":"imperial"}`
req, _ := http.NewRequest("POST", "https://api.wayline.cloud/v1/optimize", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("WAYLINE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
out, _ := io.ReadAll(res.Body)
fmt.Println(res.Status, string(out))
}<?php
$ch = curl_init("https://api.wayline.cloud/v1/optimize");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("WAYLINE_API_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => '{"stops":[{"lat":37.788,"lng":-122.4075},{"lat":37.8313,"lng":-122.2852},{"lat":37.8716,"lng":-122.2727},{"lat":37.9358,"lng":-122.3478},{"lat":37.8044,"lng":-122.2712}],"end":"start","units":"imperial"}',
CURLOPT_TIMEOUT => 10,
]);
print_r(json_decode(curl_exec($ch), true));require "net/http"
require "json"
uri = URI("https://api.wayline.cloud/v1/optimize")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req["Authorization"] = "Bearer #{ENV.fetch("WAYLINE_API_KEY")}"
req.body = {stops: [{lat: 37.788, lng: -122.4075}, {lat: 37.8313, lng: -122.2852}, {lat: 37.8716, lng: -122.2727}, {lat: 37.9358, lng: -122.3478}, {lat: 37.8044, lng: -122.2712}], end: "start", units: "imperial"}.to_json
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(req) }
puts JSON.parse(res.body)import java.net.URI;
import java.net.http.*;
public class Main {
public static void main(String[] args) throws Exception {
String body = """
{
"stops": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
},
{
"lat": 37.8044,
"lng": -122.2712
}
],
"end": "start",
"units": "imperial"
}
""";
var req = HttpRequest.newBuilder(URI.create("https://api.wayline.cloud/v1/optimize"))
.header("Authorization", "Bearer " + System.getenv("WAYLINE_API_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body)).build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(res.statusCode() + " " + res.body());
}
}import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
Future<void> main() async {
final res = await http.post(
Uri.parse('https://api.wayline.cloud/v1/optimize'),
headers: {
'Authorization': 'Bearer ${Platform.environment['WAYLINE_API_KEY']}',
'Content-Type': 'application/json',
},
body: jsonEncode({
"stops": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
},
{
"lat": 37.8044,
"lng": -122.2712
}
],
"end": "start",
"units": "imperial"
}),
);
print(jsonDecode(res.body));
}{
"order": [
0,
2,
3,
1,
4
],
"legs": [
{
"from_index": 0,
"to_index": 2,
"distance_meters": 4210,
"duration_seconds": 540,
"distance_text": "2.6 mi",
"duration_text": "9 mins"
},
{
"from_index": 2,
"to_index": 3,
"distance_meters": 3010,
"duration_seconds": 420,
"distance_text": "1.9 mi",
"duration_text": "7 mins"
},
{
"from_index": 3,
"to_index": 1,
"distance_meters": 6120,
"duration_seconds": 840,
"distance_text": "3.8 mi",
"duration_text": "14 mins"
},
{
"from_index": 1,
"to_index": 4,
"distance_meters": 9870,
"duration_seconds": 1140,
"distance_text": "6.1 mi",
"duration_text": "19 mins"
},
{
"from_index": 4,
"to_index": 0,
"distance_meters": 19870,
"duration_seconds": 1260,
"distance_text": "12.3 mi",
"duration_text": "21 mins"
}
],
"returns_to_start": true,
"total_distance_meters": 43080,
"total_duration_seconds": 4200,
"region": "us",
"map_version": "20261121-2330",
"profile": "car",
"units_charged": 5,
"warnings": []
}Response
200 — Optimized plan.
| Field | Type | Description |
|---|---|---|
orderrequired | integer[] | Input indices in visiting order. |
stopsrequired | object[] | Stops in visiting order. |
legsrequired | object[] | |
returns_to_startrequired | boolean | |
total_distance_metersrequired | integer | |
total_duration_secondsrequired | integer | |
total_distance_miles | number | |
total_distance_text | string | |
total_duration_text | string | |
geometry | any | Route geometry when requested (encoded polyline string or GeoJSON LineString). |
units_chargedrequired | integer | |
profilerequired | string | |
regionrequired | string | |
map_versionrequired | string | |
warningsrequired | string[] |
Response headers
X-Request-Id— Unique identifier for tracking and debugging this request.X-Units-Charged— Billable request units consumed by this request.X-Region— Region code serving this routing request (e.g. `bd`).
Errors
| Status | When |
|---|---|
400 | Bad request (invalid coordinates, out of coverage, too many elements, etc.). |
401 | Missing or invalid API key, or expired key. |
402 | Free quota exhausted, no payment method, account suspended, or the monthly spend limit you set was reached. Never billed. Raise or remove the limit in the dashboard (Billing) and retry; nothing else changes. |
403 | Account disabled, key IP restriction failed, origin not allowed, or scope not permitted. |
404 | No route found between the requested coordinates. |
429 | Rate limit exceeded or test key daily quota reached. |
500 | Internal server error. |
503 | Upstream routing service temporarily unavailable. |
Error bodies contain a code such as OUT_OF_COVERAGE — see the error reference.
Try it
Interactive API Console
Sends a real request from your browser using a test key (free, never billed). The key stays in this browser tab only.
No key yet? Create a free test key.