Route Example¶
Using Route API with Multiple Tasks¶
This example demonstrates how to use the Route API to calculate routes for multiple tasks with different configurations.
The Route API allows you to: * Calculate optimal routes between origin and destination * Add multiple waypoints along the route * Get alternative route options * Optimize for distance or duration * Specify vehicle dimensions for truck routing * Avoid specific features (tunnels, ferries, toll roads, etc.) * Calculate toll costs in different currencies * Handle multiple routing tasks in a single request
import requests
# Configuration
API_BASE_URL = "https://api.flio.ai"
ROUTE_ENDPOINT = "/solver/route"
API_KEY = "YOUR-API-KEY"
payload = {
"tasks": [
{
"mode": "car",
"origin": [41.0082, 28.9784],
"destination": [41.0150, 28.9850],
"waypoints": [
[41.0100, 28.9800],
[41.0122, 28.9820]
],
"alternatives": 2,
"minimize": "duration",
"avoid_features": ["toll_road", "ferry"],
"tolls": False,
},
{
"mode": "truck",
"origin": [41.0082, 28.9784],
"destination": [41.0030, 28.9720],
"alternatives": 1,
"minimize": "distance",
"tolls": True,
"currency": "EUR",
"vehicle": {
"length": 1200,
"width": 250,
"height": 350,
"axle_count": 4,
"hazardous_goods": ["flammable"]
},
"avoid_features": ["tunnel"],
},
{
"mode": "car",
"origin": [41.0050, 28.9750],
"destination": [41.0180, 28.9880],
"alternatives": 0,
"minimize": "distance",
"avoid_features": ["controlled_access_highway"],
"tolls": False
}
]
}
url = f"{API_BASE_URL}{ROUTE_ENDPOINT}?api_key={API_KEY}"
print(f"\nSending request to: {url}")
response = requests.post(url, json=payload)
print(f"Response Status: {response.status_code}")
print(f"Response Headers: {dict(response.headers)}")
if response.status_code == 200:
response_data = response.json()
print(f"Response: {response_data}")
# Print summary for each task
if "tasks" in response_data:
for task_idx, task in enumerate(response_data["tasks"]):
print(f"\n=== TASK {task_idx + 1} SUMMARY ===")
if "routes" in task:
for route_idx, route in enumerate(task["routes"]):
print(f"\n Route {route_idx + 1} (ID: {route.get('id', 'N/A')})")
# Calculate totals from all sections
total_distance = 0
total_duration = 0
total_toll_cost = 0
if "sections" in route:
for section in route["sections"]:
summary = section.get("summary", {})
total_distance += summary.get("distance", 0)
total_duration += summary.get("duration", 0)
total_toll_cost += summary.get("toll_cost", 0)
print(f" Total Distance: {total_distance} meters ({total_distance / 1000:.2f} km)")
print(f" Total Duration: {total_duration} seconds ({total_duration / 60:.2f} minutes)")
if total_toll_cost > 0:
print(f" Total Toll Cost: {total_toll_cost}")
# Print notices if any
if "notices" in route and route["notices"]:
print(f" Notices: {', '.join(route['notices'])}")
else:
print(f"Error: {response.status_code}")
print(f"Response: {response.text}")