Guides
Delivery radius by drive time
Build a delivery-radius checker in 15 minutes — decide which stores can deliver to an address within a drive-time limit using the distance matrix API.
A 5-mile circle around a store ignores rivers, bridges and highways. A drive-time radius reflects how far a driver can actually get. This guide builds a checker that answers: which of my stores can reach this customer in under 20 minutes?
1. One request per checkout
Send the customer as the only destination and your candidate stores as origins. A 1 × N or N × 1 matrix costs N units.
curl -X POST "https://api.wayline.cloud/v1/matrix" \
-H "Authorization: Bearer $WAYLINE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"origins":[{"lat":37.788,"lng":-122.4075},{"lat":37.8313,"lng":-122.2852}],"destinations":[{"lat":37.8044,"lng":-122.2712},{"lat":37.8716,"lng":-122.2727},{"lat":37.9358,"lng":-122.3478}],"units":"imperial"}'const res = await fetch("https://api.wayline.cloud/v1/matrix", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WAYLINE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
"origins": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
}
],
"destinations": [
{
"lat": 37.8044,
"lng": -122.2712
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
}
],
"units": "imperial"
}),
});
if (!res.ok) throw new Error(`Wayline ${res.status}: ${await res.text()}`);
console.log(await res.json());import os, requests
res = requests.post(
"https://api.wayline.cloud/v1/matrix",
json={
"origins": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
}
],
"destinations": [
{
"lat": 37.8044,
"lng": -122.2712
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
}
],
"units": "imperial"
},
headers={"Authorization": f"Bearer {os.environ['WAYLINE_API_KEY']}"},
timeout=10,
)
res.raise_for_status()
print(res.json())package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
body := `{"origins":[{"lat":37.788,"lng":-122.4075},{"lat":37.8313,"lng":-122.2852}],"destinations":[{"lat":37.8044,"lng":-122.2712},{"lat":37.8716,"lng":-122.2727},{"lat":37.9358,"lng":-122.3478}],"units":"imperial"}`
req, _ := http.NewRequest("POST", "https://api.wayline.cloud/v1/matrix", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("WAYLINE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
out, _ := io.ReadAll(res.Body)
fmt.Println(res.Status, string(out))
}<?php
$ch = curl_init("https://api.wayline.cloud/v1/matrix");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer " . getenv("WAYLINE_API_KEY"),
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => '{"origins":[{"lat":37.788,"lng":-122.4075},{"lat":37.8313,"lng":-122.2852}],"destinations":[{"lat":37.8044,"lng":-122.2712},{"lat":37.8716,"lng":-122.2727},{"lat":37.9358,"lng":-122.3478}],"units":"imperial"}',
CURLOPT_TIMEOUT => 10,
]);
print_r(json_decode(curl_exec($ch), true));require "net/http"
require "json"
uri = URI("https://api.wayline.cloud/v1/matrix")
req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
req["Authorization"] = "Bearer #{ENV.fetch("WAYLINE_API_KEY")}"
req.body = {origins: [{lat: 37.788, lng: -122.4075}, {lat: 37.8313, lng: -122.2852}], destinations: [{lat: 37.8044, lng: -122.2712}, {lat: 37.8716, lng: -122.2727}, {lat: 37.9358, lng: -122.3478}], units: "imperial"}.to_json
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(req) }
puts JSON.parse(res.body)import java.net.URI;
import java.net.http.*;
public class Main {
public static void main(String[] args) throws Exception {
String body = """
{
"origins": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
}
],
"destinations": [
{
"lat": 37.8044,
"lng": -122.2712
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
}
],
"units": "imperial"
}
""";
var req = HttpRequest.newBuilder(URI.create("https://api.wayline.cloud/v1/matrix"))
.header("Authorization", "Bearer " + System.getenv("WAYLINE_API_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body)).build();
var res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(res.statusCode() + " " + res.body());
}
}import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
Future<void> main() async {
final res = await http.post(
Uri.parse('https://api.wayline.cloud/v1/matrix'),
headers: {
'Authorization': 'Bearer ${Platform.environment['WAYLINE_API_KEY']}',
'Content-Type': 'application/json',
},
body: jsonEncode({
"origins": [
{
"lat": 37.788,
"lng": -122.4075
},
{
"lat": 37.8313,
"lng": -122.2852
}
],
"destinations": [
{
"lat": 37.8044,
"lng": -122.2712
},
{
"lat": 37.8716,
"lng": -122.2727
},
{
"lat": 37.9358,
"lng": -122.3478
}
],
"units": "imperial"
}),
);
print(jsonDecode(res.body));
}2. Filter by threshold
type Store = { id: string; lat: number; lng: number };
export async function storesWithin(customer: { lat: number; lng: number }, stores: Store[], maxMinutes = 20) {
const res = await fetch("https://api.wayline.cloud/v1/matrix", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.WAYLINE_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ origins: stores.map(({ lat, lng }) => ({ lat, lng })), destinations: [customer], metrics: "duration" }),
});
if (!res.ok) throw new Error(`Wayline ${res.status}`);
const data = await res.json();
return stores
.map((s, i) => ({ store: s, seconds: data.rows[i].elements[0].duration_seconds as number | undefined, status: data.rows[i].elements[0].status }))
.filter((x) => x.status === "OK" && x.seconds! <= maxMinutes * 60)
.sort((a, b) => a.seconds! - b.seconds!);
}3. Pre-filter to save units
Only send stores that could plausibly qualify. A cheap straight-line pre-filter (e.g. within 2× your drive-time radius at highway speed) removes far stores before the matrix call.
4. Cache answers
Customer addresses repeat. Cache (store, rounded customer coordinate) → seconds keyed with the region map_version. See Caching.
Caveats
Drive times are free-flow (no live traffic). Leave a safety margin, or scale by a time-of-day factor you measure from real deliveries.
Related: Last-mile delivery · Matrix reference