oslew docs
Console
Menu

Visit order

GET /v1/optimize answers the fastest order in which to visit a set of points — for one vehicle, a courier's stops, a technician's calls — as a round trip back to the start or as a path that ends anywhere. It returns the order, the time of every leg, and the route that joins them.

curl
curl -G "https://api.oslew.com/v1/optimize" \
  -H "Authorization: Bearer $GEO_API_KEY" \
  --data-urlencode 'points=19.4406,-99.2047;19.4326,-99.1332;19.4352,-99.1412;19.3500,-99.1620' \
  --data-urlencode 'geometry=false'
Python
from oslew import Client

# The key is read from GEO_API_KEY.
client = Client(base_url="https://api.oslew.com")

answer = client.optimize(
    "19.4406,-99.2047;19.4326,-99.1332;19.4352,-99.1412;19.3500,-99.1620",
    geometry=False,
)
TypeScript
import { Client } from "@oslew/client";

// The key is read from GEO_API_KEY.
const client = new Client({ baseUrl: "https://api.oslew.com" });

const answer = await client.optimize("19.4406,-99.2047;19.4326,-99.1332;19.4352,-99.1412;19.3500,-99.1620", { geometry: false });
Response
HTTP/1.1 200 OK
content-type: application/json
ratelimit-limit: 100
ratelimit-remaining: 294
ratelimit-reset: 1

{
  "order": [
    0,
    2,
    1,
    3,
    0
  ],
  "waypoints": [
    [
      19.4406,
      -99.2047
    ],
    [
      19.4352,
      -99.1412
    ],
    [
      19.4326,
      -99.1332
    ],
    [
      19.35,
      -99.162
    ],
    [
      19.4406,
      -99.2047
    ]
  ],
  "duration_s": 2173,
  "legs_s": [
    561,
    170,
    651,
    791
  ],
  "nearest_neighbour_s": 2173,
  "distance_m": 34016,
  "geometry": [],
  "engine": "base/hierarchy",
  "provenance": {
    "name": "overture-transportation",
    "release": "2026-08-19.0",
    "license": "ODbL-1.0"
  },
  "took_us": 5137
}

A real response, captured from the service at commit b8f4bde-dirty. Timing fields such as took_us differ on every call.

Four points, starting and ending at the first. The order is 0, 2, 1, 3 and back to 0, and the legs take 561, 170, 651 and 791 s: 2,173 s, a little over 36 minutes, and 34,016 m of road.

Parameters

ParameterDefaultIs
pointsrequired2 to 500 lat,lon pairs joined with semicolons. Fewer or more is a 400, between 2 and 500 points.
roundtriptrueReturn to the start at the end. A round trip always starts at the first point.
sourcefirstfirst to start at the first point; any to start wherever makes the whole fastest. Only matters when roundtrip=false.
destinationanylast to end at the last point; any to end wherever makes the whole fastest. Only matters when roundtrip=false.
profiledrivingdriving, walking, cycling or rail; see routes and profiles.
trafficnonelive to time the legs on a traffic feed's weights, driving only. Read the pitfall below first.
geometrytrueWhether to return the path of every leg.

Where it starts and ends

roundtripsourcedestinationThe answer
true——Starts at point 0, visits every other, and returns to 0. order ends with 0 again.
falsefirstanyStarts at point 0 and ends wherever is fastest. A courier who does not go back to the depot.
falsefirstlastStarts at point 0 and ends at the last point you sent. Depot to home, calls in between.
falseanylastEnds at the last point and starts wherever is fastest.
falseanyanyThe fastest path through every point, from wherever to wherever.

The same four points without the return:

curl
curl -G "https://api.oslew.com/v1/optimize" \
  -H "Authorization: Bearer $GEO_API_KEY" \
  --data-urlencode 'points=19.4406,-99.2047;19.4326,-99.1332;19.4352,-99.1412;19.3500,-99.1620' \
  --data-urlencode 'roundtrip=false' \
  --data-urlencode 'geometry=false'
Python
from oslew import Client

# The key is read from GEO_API_KEY.
client = Client(base_url="https://api.oslew.com")

answer = client.optimize(
    "19.4406,-99.2047;19.4326,-99.1332;19.4352,-99.1412;19.3500,-99.1620",
    roundtrip=False,
    geometry=False,
)
TypeScript
import { Client } from "@oslew/client";

// The key is read from GEO_API_KEY.
const client = new Client({ baseUrl: "https://api.oslew.com" });

const answer = await client.optimize("19.4406,-99.2047;19.4326,-99.1332;19.4352,-99.1412;19.3500,-99.1620", { roundtrip: false, geometry: false });
Response
HTTP/1.1 200 OK
content-type: application/json
ratelimit-limit: 100
ratelimit-remaining: 293
ratelimit-reset: 1

{
  "order": [
    0,
    2,
    1,
    3
  ],
  "waypoints": [
    [
      19.4406,
      -99.2047
    ],
    [
      19.4352,
      -99.1412
    ],
    [
      19.4326,
      -99.1332
    ],
    [
      19.35,
      -99.162
    ]
  ],
  "duration_s": 1382,
  "legs_s": [
    561,
    170,
    651
  ],
  "nearest_neighbour_s": 1382,
  "distance_m": 20755,
  "geometry": [],
  "engine": "base/hierarchy",
  "provenance": {
    "name": "overture-transportation",
    "release": "2026-08-19.0",
    "license": "ODbL-1.0"
  },
  "took_us": 4153
}

A real response, captured from the service at commit b8f4bde-dirty. Timing fields such as took_us differ on every call.

The order is the same, and the answer is the round trip without its last leg: 1,382 s and 20,755 m, ending at point 3, the farthest south. It need not be the same order in general — the best way to end somewhere is not always the best way to come back from it.

How the order is found

  1. A travel-time matrix between every pair of points, on the profile's network.
  2. A first order by nearest neighbour: from the start, always go to the closest point not yet visited. With a free start, this is tried from every point and the best kept.
  3. Improvement by 2-opt — reverse a stretch of the order — and Or-opt — move one, two or three consecutive stops elsewhere — repeated until neither finds anything faster.
  4. With 8 points or fewer, none of that: every order is tried, and the answer is exact.

Past eight points the answer is a local optimum: no single reversal or move improves it. It is usually very good and is not proven best. The same points in the same order always give the same answer.

nearest_neighbour_s is how long the first, nearest-neighbour order took, before improvement.nearest_neighbour_s − duration_s is what the improvement bought, and is the number to show a user who asks why not just drive to the nearest stop each time. With 8 points or fewer no nearest-neighbour order is built, and nearest_neighbour_s equals duration_s — as in both examples here — which says nothing about how much better the exact order is.

Reading the answer

FieldIs
orderIndices into your points, in visiting order. A round trip repeats the start at the end.
waypointsYour points, as you sent them, in that order.
legs_sEach leg's time in whole seconds, one fewer than order has entries.
duration_sThe total, in whole seconds.
nearest_neighbour_sThe nearest-neighbour order's time, before improvement.
distance_mWhole metres along the routed legs.
geometryEvery leg's path as [lat, lon] pairs, one after another. Each leg starts and ends at its stops' snapped positions, so a stop appears twice where one leg ends and the next begins.
engineThe engine that routed the legs.

The times come from the matrix, which moves each point to its nearest junction; the distance and path come from routing each leg, which starts from the nearest point on a road. The two can disagree by a few seconds' worth of road at each stop — travel-time matrices explains why — so do not expectdistance_m divided by duration_s to be a speed to trust to the metre.

Python
from oslew import Client

client = Client(base_url="https://api.oslew.com")
stops = [(19.4406, -99.2047), (19.4326, -99.1332), (19.4352, -99.1412), (19.3500, -99.1620)]

plan = client.optimize(";".join(f"{lat},{lon}" for lat, lon in stops), roundtrip=False, geometry=False)

clock = 0
for leg, (a, b) in enumerate(zip(plan["order"], plan["order"][1:])):
    clock += plan["legs_s"][leg]
    print(f"leg {leg + 1}, stop {a} to stop {b}: arrive {int(clock // 60)} min after the start")
saved = plan["nearest_neighbour_s"] - plan["duration_s"]
TypeScript
import { Client } from "@oslew/client";

const client = new Client({ baseUrl: "https://api.oslew.com" });
const stops: [number, number][] = [[19.4406, -99.2047], [19.4326, -99.1332], [19.4352, -99.1412], [19.35, -99.162]];

const plan = await client.optimize(stops.map(([lat, lon]) => `${lat},${lon}`).join(";"), { roundtrip: false, geometry: false });

let clock = 0;
plan.order.slice(1).forEach((stop, leg) => {
  clock += plan.legs_s[leg];
  console.log(`stop ${stop}: ${Math.floor(clock / 60)} min after the start`);
});
const saved = plan.nearest_neighbour_s - plan.duration_s;

Pitfalls

Live traffic times the legs; it does not route them

With traffic=live, the matrix — and so the order, legs_s and duration_s — uses the live weights where an operator has applied a feed. The legs are then routed on the release's weights, sodistance_m and geometry describe the free-flow route between each pair, which may not be the road the live time assumed. engine names the engine that routed the legs, so it readsbase/hierarchy even when the times came from live weights. See live traffic.

One unreachable pair fails the whole request

If some pair of points has no route between them, no order can visit them all, and the answer is a400, no order visits every point: some pair is unreachable. A point more than about 9 km from any road is unreachable from everything. To find the culprit, ask for the matrixof the same points and look for the nulls.

What it does not model

One vehicle; no time windows, capacities, breaks or priorities; no time spent at a stop. Add a stop's service time to the clock yourself, as a fixed amount per stop does not change the best order. For several vehicles or any of the constraints, take a matrix and give it to a solver that models them;delivery planning shows both.

Endpoints

GET/v1/optimizerouting

The fastest order to visit 2 to 500 points: a duration matrix, then nearest-neighbour, 2-opt and Or-opt (exact up to 8 points), with the legs routed.