const axios = require('axios');
const fs = require('fs');
async function fetchRoute(coordinates) {
const apiKey = 'YOUR_API_KEY_HERE';
const apiUrl = 'https://www.usepathway.dev/api/routing';
const payload = {
data: {
coordinates: coordinates
}
};
try {
const response = await axios.post(apiUrl, payload, {
headers: {
'x-api-key': apiKey,
'Content-Type': 'application/json',
},
});
console.log('Received Routing Data:', response.data);
response.data.routes.forEach((route, index) => {
const geometryData = `Route ${index + 1} Geometry:\n${route.geometry}\n`;
fs.writeFile(`route${index + 1}_geometry.txt`, geometryData, (err) => {
if (err) throw err;
console.log(`Geometry for Route ${index + 1 has been saved in a separate file.`);
});
});
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
}
const coordinates = [
[-122.4194, 37.7749],
[-118.2437, 34.0522],
];
fetchRoute(coordinates);
Here is an example of the JSON response structure after a successful POST request:
{
"code": "Ok",
"routes": [
{
"geometry": "...",
"legs": [...],
"weight_name": "routability",
"weight": 633.6,
"duration": 630.7,
"distance": 4732.2
}
],
"waypoints": [
{
"hint": "...",
"distance": 4.231521214,
"name": "Friedrichstraße",
"location": [13.388798, 52.517033]
},
{
"hint": "...",
"distance": 2.795148358,
"name": "Torstraße",
"location": [13.39763, 52.529432]
}
]
}