# 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.

Source: https://www.wayline.cloud/docs/guides/delivery-radius

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.

## 2. Filter by threshold

```typescript
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](/docs/concepts/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](/use-cases/last-mile-delivery) · [Matrix reference](/docs/api/compute-matrix)
