Skip to main content

How to optimize the route

Basic Order Optimization

GraphQL query

You can optimize the route to reduce costs, distance, and the time taken and more.

const fetch = require("node-fetch")

fetch("https://backend.impargo.eu/", {
method: "POST",
headers: {
"authorization": token,
"content-type": "application/json",
},
body: JSON.stringify([{
"operationName": "Optimize",
"variables": {
"stops": [
{"lat": 43.34991, "lon": 1.86602},
{"lat": 53.52996, "lon": -8.77419},
{"lat": 48.13641, "lon": 11.57754}
],
"fixLastStop": false,
"departureDate": null
},
"query": `query Optimize($stops: [OptimizationStop]!, $fixLastStop: Boolean, $departureDate: DateTime) {
optimize(
stops: $stops
fixLastStop: $fixLastStop
departureDate: $departureDate
) {
order
warning
__typename
}
}`
}])
})
.then(response => response.json())
.then(data => {
const graphqlData = data[0].data.optimize;
console.log("GraphQL Response:", graphqlData);
})
.catch(error => {
console.error("Fetch Error:", error);
});

[
{
"data": {
"optimize": {
"order": [
0,
1,
2
],
"warning": null,
"__typename": "OptimizationResult"
}
}
}
]

Advanced Time Planning Optimization

This example demonstrates how to optimize a route considering time windows for each stop, waiting time at stops, and a specific departure date and time.

const fetch = require("node-fetch")

fetch("https://backend.impargo.eu/", {
method: "POST",
headers: {
"authorization": token,
"content-type": "application/json",
},
body: JSON.stringify([{
"operationName": "Optimize",
"variables": {
"stops": [
{
"lat": 52.53131,
"lon": 13.3503,
"timeWindows": [
{ "from": "09:00", "to": "14:00" }
],
"waitingTime": 1800
},
{
"lat": 52.40326,
"lon": 13.06908,
"timeWindows": [
{ "from": "08:00", "to": "16:00" }
],
"waitingTime": 900
},
{
"lat": 52.51661,
"lon": 13.37706,
"timeWindows": [
{ "from": "08:00", "to": "16:00" }
],
"waitingTime": 1800
}
],
"fixLastStop": false,
"departureDate": "2025-10-18T08:00:00.000Z"
},
"query": `query Optimize(
$stops: [OptimizationStop]!
$fixLastStop: Boolean
$departureDate: DateTime
) {
optimize(
stops: $stops
fixLastStop: $fixLastStop
departureDate: $departureDate
) {
order
warning
}
}`
}])
})
.then(response => response.json())
.then(data => {
const graphqlData = data[0].data.optimize;
console.log("GraphQL Response:", graphqlData);
})
.catch(error => {
console.error("Fetch Error:", error);
});

[
{
"data": {
"optimize": {
"order": [0, 2, 1],
"warning": null
}
}
}
]

In this example, each stop has a time window and waiting time. The optimization considers these constraints and the specified departure date/time to calculate the best order of stops.