How to charge delivery fees by distance: formulas, examples and code
Four formulas for distance-based delivery fees (per-mile, zones, time-weighted, free over a basket) with worked examples and API code.
By Wayline Engineering · · Updated · 3 min read
Charging a delivery fee by distance is fairer to customers and protects your margin on long trips. The key word is road distance: a customer 3 miles away as the crow flies may be 5 miles away by car if there's a river or highway in between. Price on straight-line distance and you lose money on exactly the trips that cost you most.
Formula 1: flat fee plus per-mile rate
The most common model:
fee = base + rate × road_miles
fee = min(max(fee, minimum), maximum)Example: base $3.99, $0.85 per mile, minimum $4.99, maximum $14.99.
| Road distance | Calculated | Charged |
|---|---|---|
| 0.8 mi | $4.67 | $4.99 (minimum) |
| 4.2 mi | $7.56 | $7.56 |
| 9.5 mi | $12.07 | $12.07 |
| 15.0 mi | $16.74 | $14.99 (maximum) |
Simple to explain at checkout, easy to tune.
Formula 2: distance tiers (zones)
Customers understand round numbers:
| Road distance | Fee |
|---|---|
| Up to 3 mi | $3.99 |
| 3–6 mi | $5.99 |
| 6–10 mi | $8.99 |
| Over 10 mi | Not served |
Tiers make the fee predictable and give you a clean "we don't deliver there" rule. Use road distance for the boundaries, not a circle on a map — see delivery radius checks.
Formula 3: time-weighted
Driver cost is mostly time. Blend distance and duration:
fee = base + a × road_miles + b × drive_minutesWith a = $0.50 and b = $0.20, a 4-mile trip that takes 18 minutes costs base + $2.00 + $3.60. This charges more for slow urban trips than fast suburban ones of the same length.
Formula 4: free delivery over a basket size
Combine any formula above with a threshold — "free delivery on orders over $50 within 6 miles." Check distance first, then basket value, so far-away orders don't get free delivery by accident.
Implementing it with a distance API
Compute the fee on your server, never in the browser, so customers can't tamper with it:
const res = await fetch(
`https://api.wayline.cloud/v1/distance?origin=${store}&destination=${customer}&units=imperial`,
{ headers: { Authorization: `Bearer ${process.env.WAYLINE_API_KEY}` } },
);
const { distance_meters, duration_seconds } = await res.json();
const miles = distance_meters / 1609.344;
const fee = Math.min(Math.max(3.99 + 0.85 * miles, 4.99), 14.99);Multiple stores? Call the Distance Matrix API once with all stores as origins and the customer as the destination, then pick the closest store by road.
High checkout volume? Cache by rounded customer coordinates (about 5 decimal places) and store. Distances only change when the road map changes.
Mistakes to avoid
- Straight-line distance. Systematically under-charges trips with detours. See straight-line vs road distance.
- Charging for the return trip without saying so. If your fee covers the round trip, say so at checkout.
- No maximum. A typo in an address can create a $90 delivery fee. Cap it and flag outliers.
- Not showing the fee early. Show an estimate as soon as the address is entered — surprise fees cause cart abandonment.
FAQ
Should delivery fees use miles or drive time? Distance is easier to explain; time better reflects driver cost. Many businesses use distance tiers for the customer-facing price and track time internally.
How much does it cost to compute a delivery fee? One distance request per quote. At API prices of around a dollar per thousand requests, the cost is a fraction of a cent per order — see pricing.
Can I test these formulas without coding? Yes — try the free distance calculator to get road distance and drive time between two points.