How to Get Travel Times from Many Origins to One Destination
- API or service
- Route Matrix API
- Task
- Many origins and one destination → travel time and distance per origin
- Examples
- Difficulty
- Beginner
- Time
- 10 min
Get travel times from many origins to one destination
You have many starting points (customers, employees, stores, job sites, or neighborhoods) and one destination, such as an office, a warehouse, a venue, or a hospital. You need the driving time and the driving distance from every origin to that destination.
The Geoapify Route Matrix API returns them in one POST request. Send the origins as sources and the destination as the only target, and the response contains one row per origin with a single travel time and distance.
Task flow: origin and destination coordinates → Route Matrix API (many sources, one target) → one row per origin → origins ranked by travel time.
The example uses eight public library branches around Seattle, Washington, as origins and Lumen Field, 800 Occidental Ave S, Seattle, WA 98134, as the destination. One request with 8 sources and 1 target costs 8 credits. The fastest origin is West Seattle, at 12.4 minutes. The slowest is Kirkland, at 22.9 minutes: it is closer in a straight line than Burien or Renton, but the drive has to go around Lake Washington or across a bridge.
Put the origins in sources
A route matrix calculates a route from every source to every target. sources_to_targets[i][j] is the route from sources[i] to targets[j]. For many origins and one destination:
| Request field | Value | Result |
|---|---|---|
sources |
All origins, in your own order | One row per origin |
targets |
The destination only | One cell in every row |
Keep this direction when the trip goes from the origins to the destination, such as a commute to an office or deliveries to a warehouse. Road travel can be directional because of one-way streets, turn restrictions, and highway ramps, so a matrix from the destination to the origins (one source, many targets) can return different times. In the Seattle example, the two directions differ by up to 2.8 minutes. Use that shape when the trip starts at the destination, for example, deliveries from a warehouse.
Size and credits
| Limit or cost | Value for N origins and one destination | Source |
|---|---|---|
| Matrix size | N × 1 cells; up to 1000 cells per regular API call, so up to 1000 origins per request | Route Matrix API pricing |
| Credits | max(N, 1) × min(N, 1, 10) = N credits |
Route Matrix API pricing |
| Example | 8 origins → 8 credits; 1000 origins → 1000 credits |
Avoid options and distances longer than 500 km add credits; see the pricing link above. For more than 1000 origins, split the list into several requests: see Handle more than 1000 origins.
Request driving times to one destination
POST a JSON body with eight sources and one target:
POST https://api.geoapify.com/v1/routematrix?apiKey=YOUR_API_KEY
Content-Type: application/json
{
"mode": "drive",
"units": "imperial",
"sources": [
{ "location": [-122.384107, 47.669727] },
{ "location": [-122.322853, 47.705979] },
{ "location": [-122.202754, 47.676225] },
{ "location": [-122.193961, 47.620129] },
{ "location": [-122.22107, 47.566825] },
{ "location": [-122.202181, 47.481746] },
{ "location": [-122.33975, 47.467033] },
{ "location": [-122.384704, 47.582731] }
],
"targets": [
{ "location": [-122.331644, 47.595346] }
]
}
| Parameter | Value | Meaning |
|---|---|---|
mode |
drive |
Calculate routes for a passenger car. Other modes include truck, bicycle, and walk; see travel modes. |
units |
imperial |
Return distances in miles. The default metric returns meters. Times are always in seconds. |
sources |
8 locations | The origins. Each source produces one row in the response. |
targets |
1 location | The destination. Each row contains one cell for it. |
Every location uses [longitude, latitude] order. Keep your own list of origins in the same order as sources: the response refers to each origin only by its position, source_index.
The origins are the coordinates that the Geocoding API returned for these addresses, rounded to six decimal places:
source_index |
Origin | Address |
|---|---|---|
| 0 | Ballard | 5614 22nd Ave NW, Seattle, WA 98107 |
| 1 | Northgate | 10548 5th Ave NE, Seattle, WA 98125 |
| 2 | Kirkland | 308 Kirkland Ave, Kirkland, WA 98033 |
| 3 | Bellevue | 1111 110th Ave NE, Bellevue, WA 98004 |
| 4 | Mercer Island | 4400 88th Ave SE, Mercer Island, WA 98040 |
| 5 | Renton | 100 Mill Ave S, Renton, WA 98057 |
| 6 | Burien | 400 SW 152nd St, Burien, WA 98166 |
| 7 | West Seattle | 2306 42nd Ave SW, Seattle, WA 98116 |
The target is Lumen Field, 800 Occidental Ave S, Seattle, WA 98134.

The screenshot shows the coordinates from this request, rendered with MapLibre GL JS 4.7.1 and the Geoapify osm-bright map style.
The request does not set traffic, so the API uses the default free_flow model: travel times assume roads without congestion.
Read one row per origin
With eight sources and one target, sources_to_targets contains eight rows, and each row contains a single cell.
Reduced response: two of the eight sources and three of the eight rows are shown.
{
"sources": [
{
"original_location": [-122.384107, 47.669727],
"location": [-122.384106, 47.669726]
},
{
"original_location": [-122.322853, 47.705979],
"location": [-122.323157, 47.70596]
}
],
"targets": [
{
"original_location": [-122.331644, 47.595346],
"location": [-122.330097, 47.595342]
}
],
"sources_to_targets": [
[{ "distance": 7.3623, "time": 993, "source_index": 0, "target_index": 0 }],
[{ "distance": 10.2462, "time": 1020, "source_index": 1, "target_index": 0 }],
[{ "distance": 13.2368, "time": 1376, "source_index": 2, "target_index": 0 }]
],
"units": "imperial",
"distance_units": "miles",
"mode": "drive"
}
| Field | Meaning |
|---|---|
sources_to_targets[i][0] |
The route from origin i to the destination |
time |
Travel time in seconds |
distance |
Road distance in distance_units: miles here, because the request sets units: "imperial" |
source_index |
The origin's position in the request's sources array |
sources[i].location |
The origin snapped to the nearest road; original_location is the coordinate you sent |
distance and time can be null when no route is available for an origin. Treat such an origin as unreachable instead of as zero minutes.
All eight origins
The table converts time to minutes (time / 60) and adds the straight-line distance between the request coordinates for comparison. The API does not return the straight-line distance.
| Origin | Straight line | Driving distance | Driving time | time |
|---|---|---|---|---|
| West Seattle | 2.6 mi | 4.7 mi | 12.4 min | 743 |
| Mercer Island | 5.5 mi | 7.6 mi | 13.6 min | 814 |
| Ballard | 5.7 mi | 7.4 mi | 16.6 min | 993 |
| Northgate | 7.7 mi | 10.2 mi | 17.0 min | 1020 |
| Bellevue | 6.6 mi | 10.9 mi | 18.4 min | 1103 |
| Burien | 8.9 mi | 15.1 mi | 20.0 min | 1200 |
| Renton | 9.9 mi | 15.4 mi | 20.1 min | 1205 |
| Kirkland | 8.2 mi | 13.2 mi | 22.9 min | 1376 |
The three orders differ:
- Straight line vs time: Kirkland is the sixth closest in a straight line but the slowest by car, because the route has to cross Lake Washington on a bridge or go around it.
- Distance vs time: Mercer Island is 0.3 miles farther by road than Ballard but 3 minutes faster. Kirkland is about 2 miles shorter by road than Burien and Renton but about 3 minutes slower.
Rank by time when the question is "how long does it take" and by distance when you need mileage, for example for a reimbursement or a delivery fee.
Rank the origins and show them on a map
Use this after creating a MapLibre GL map and waiting for its load event. It sends the request from the previous step, maps every row back to its origin through source_index, ranks the reachable origins by travel time, and adds a colored label to each origin: green under 15 minutes, orange from 15 to 20, and red from 20 minutes.
const apiKey = "YOUR_API_KEY";
const destination = { name: "Lumen Field", location: [-122.331644, 47.595346] };
const origins = [
{ name: "Ballard", location: [-122.384107, 47.669727] },
{ name: "Northgate", location: [-122.322853, 47.705979] },
{ name: "Kirkland", location: [-122.202754, 47.676225] },
{ name: "Bellevue", location: [-122.193961, 47.620129] },
{ name: "Mercer Island", location: [-122.22107, 47.566825] },
{ name: "Renton", location: [-122.202181, 47.481746] },
{ name: "Burien", location: [-122.33975, 47.467033] },
{ name: "West Seattle", location: [-122.384704, 47.582731] }
];
const response = await fetch(`https://api.geoapify.com/v1/routematrix?apiKey=${apiKey}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
mode: "drive",
units: "imperial",
sources: origins.map(origin => ({ location: origin.location })),
targets: [{ location: destination.location }]
})
});
if (!response.ok) throw new Error(`Route Matrix request failed: ${response.status}`);
const matrix = await response.json();
// One row per origin; each row has a single cell for the destination.
const results = matrix.sources_to_targets.map(([cell]) => ({
...origins[cell.source_index],
minutes: Number.isFinite(cell.time) ? cell.time / 60 : null,
miles: Number.isFinite(cell.distance) ? cell.distance : null
}));
const ranked = results
.filter(result => result.minutes !== null)
.sort((a, b) => a.minutes - b.minutes);
const unreachable = results.filter(result => result.minutes === null);
const colorFor = minutes =>
minutes === null ? "#868e96" : minutes < 15 ? "#2b8a3e" : minutes < 20 ? "#e67700" : "#c92a2a";
results.forEach(result => {
const label = document.createElement("div");
label.className = "travel-time-label";
label.style.setProperty("--label-color", colorFor(result.minutes));
label.textContent = result.minutes === null
? `${result.name}: no route`
: `${result.name}: ${Math.round(result.minutes)} min`;
new maplibregl.Marker({ element: label, anchor: "bottom" })
.setLngLat(result.location)
.addTo(map);
});
new maplibregl.Marker({ color: "#222222" }).setLngLat(destination.location).addTo(map);
const bounds = new maplibregl.LngLatBounds();
[destination, ...origins].forEach(place => bounds.extend(place.location));
map.fitBounds(bounds, { padding: { top: 90, bottom: 40, left: 60, right: 60 }, duration: 0 });
The labels are HTML markers styled with this CSS:
/* MapLibre positions the marker element absolutely; do not override `position`. */
.travel-time-label {
margin-bottom: 7px;
padding: 4px 8px;
border-radius: 4px;
background: var(--label-color);
color: #ffffff;
font: 600 13px/1.2 sans-serif;
white-space: nowrap;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}
.travel-time-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
margin-left: -6px;
border-width: 6px 6px 0;
border-style: solid;
border-color: var(--label-color) transparent transparent;
}

The screenshot was rendered from this code with MapLibre GL JS 4.7.1 and the Geoapify osm-bright map style. The labels round the minutes, so Burien (20.0 minutes) and Renton (20.1 minutes) both show 20 minutes.
ranked holds the reachable origins from fastest to slowest; use it for a list or a table next to the map. unreachable holds the origins whose time is null; show them as "no route" instead of dropping them, so that users can see which inputs need checking.
The Route Matrix API returns no route geometry. To draw the road route for one origin, for example the one a user selects, request it from the Routing API.
Handle more than 1000 origins
A regular Route Matrix API call accepts up to 1000 cells (sources.length × targets.length). With one target, that is up to 1000 origins. A request with 1001 origins and one destination returned HTTP 400:
{
"statusCode": 400,
"error": "Bad Request",
"message": "Matrix is too big. Matrix size should not exceed 1000 elements for a regular API call, and 2000 elements for an asynchronous batch API call. Requested matrix size is 1001"
}
Split longer lists into chunks of up to 1000 origins and send one request per chunk. source_index starts at 0 in every response, so add the chunk offset to map each row back to the full list:
const MAX_CELLS = 1000; // sources.length × targets.length per regular call
async function travelTimesToDestination(origins, destination, apiKey, chunkSize = MAX_CELLS) {
const results = [];
for (let start = 0; start < origins.length; start += chunkSize) {
const chunk = origins.slice(start, start + chunkSize);
const response = await fetch(`https://api.geoapify.com/v1/routematrix?apiKey=${apiKey}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
mode: "drive",
units: "imperial",
sources: chunk.map(origin => ({ location: origin.location })),
targets: [{ location: destination.location }]
})
});
const matrix = await response.json();
if (!response.ok) throw new Error(matrix.message || `Route Matrix request failed: ${response.status}`);
// source_index restarts at 0 in every request: add the chunk offset.
matrix.sources_to_targets.forEach(([cell]) => {
results[start + cell.source_index] = {
...origins[start + cell.source_index],
minutes: Number.isFinite(cell.time) ? cell.time / 60 : null,
miles: Number.isFinite(cell.distance) ? cell.distance : null
};
});
}
return results;
}
Splitting does not change the results: with the eight Seattle origins and chunkSize set to 3, the three requests returned the same times and distances as the single request. It does not change the credits either, because each origin still costs 1 credit: 1000 origins in one request and 1000 origins in two requests of 500 both cost 1000 credits.
The requests in this function run one after another. If you run them in parallel, keep the number of simultaneous requests small and retry HTTP 429 (Quota exceeded) responses later with backoff.
For large lists processed in the background, the Batch API accepts up to 2000 cells per matrix and multiplies the credits by the job priority, from 0.5 to 1; see Route Matrix API pricing.
Travel times to one destination with an AI coding agent
Copy this prompt into Codex, Claude Code, Cursor, or another coding agent to add travel times from a list of origins to one destination to your project.
Add travel times from many origins to one destination to my project with the Geoapify Route Matrix API.
Before you start
- Inspect the existing project first. Reuse its current framework, components, styling, HTTP client, map library, and conventions. Make the smallest necessary changes.
- Ask only what the project does not answer: where the origins and the destination come from, whether results are shown in a table, on a map, or both, and whether browser requests use a restricted API key or a backend proxy.
- Read the Geoapify API key from the project's environment configuration. Never hardcode, print, or log it.
API request
- POST https://api.geoapify.com/v1/routematrix?apiKey=... with Content-Type: application/json.
- Body: mode (default "drive"), units ("imperial" for miles or "metric" for meters), sources = the origins, targets = [the destination].
- Every location is [longitude, latitude]. If the origins are addresses, geocode them first and keep the coordinates.
- Keep the origins in the same order as sources; the response identifies each origin only by source_index.
Result
- sources_to_targets has one row per origin with one cell: time in seconds, distance in distance_units.
- Show each origin with its travel time in minutes and its driving distance, sorted by time, fastest first.
- distance and time can be null when no route exists; show those origins as "no route", never as 0.
- If a map is used, mark the destination and label each origin with its minutes. The matrix has no route geometry.
Errors and limits
- One regular request allows up to 1000 cells (origins × 1). Split longer lists into chunks of 1000, add the chunk offset to source_index, and merge the results.
- Each origin costs 1 credit; avoid options and distances over 500 km add credits.
- Show the API message for HTTP 400 responses, show loading and empty states, and retry 429 responses a limited number of times with backoff.
- Add a test with a mocked response that includes a null cell and two chunks.
Review the generated code against the requests in this guide. Check the coordinate order ([longitude, latitude]), that origins map back through source_index with the chunk offset, the null handling, the units, and the API-key handling.