Everything in this series so far runs on infrastructure we control: OpenTripPlanner and Valhalla on our own server, GTFS from our own database. The moment a journey planner talks about now (this traffic jam, this accident, how long the boda ride will really take) it needs data it doesn't own.
External mobility APIs are where journey planners quietly get worse. They add latency to every request, they cost money per call, they go down, and their idea of a good route isn't yours. This part is about the rules that let Navigo use live traffic from TomTom, plus community reports from riders, without any of those problems reaching the rider.
The first rule shapes everything else.
1. External data decorates. It never decides.
TomTom can compute car and motorcycle routes with live traffic. Navigo doesn't use them. Two reasons, both specific to Nairobi:
TomTom's motorcycle routing uses the Nairobi Expressway, where boda bodas are banned.
Its car routing doesn't know our product rules: offer a "No tolls" option when the fastest route pays a toll, keep alternatives genuinely different, and so on.
So the roads are always chosen by Valhalla (Part 5), and TomTom only times them:

TomTom's routing API can rebuild a route from supporting points, a polyline the route must follow. Each Valhalla route is sent as up to 80 points sampled along its geometry, and TomTom returns three durations for exactly that road: with live traffic, with typical traffic for this hour, and with no traffic.
Why 80? Measured: 150 points gave the same length and the same time, in about 2.2 seconds instead of 0.8. The number of points is a latency knob, and past a point it buys nothing.
The three durations give two honest statements instead of one vague one:
Against empty roads: "this trip has 12 minutes of traffic in it".
Against a usual day at this hour: "it's 8 minutes worse than a normal Tuesday at 18:00", which is what a commuter actually wants to know.
Then the options are re-ranked by live time, because traffic can change which one is fastest. "Fastest" means fastest now.
2. Matatus sit in the same traffic
A matatu on Thika Road is stuck in the same jam as the cars around it, and its GTFS running times don't know that. So the transit side gets the same treatment, under tighter rules:
private const TRAFFIC_LEG_MODES = ['BUS', 'BRT'];
private const TRAFFIC_MIN_LEG_M = 1500; // short legs: not worth a call
private const TRAFFIC_MAX_LEGS = 4; // per search, longest first
private const TRAFFIC_MIN_DELAY_S = 120; // under 2 minutes is noiseOnly departures in the next hour and between 05:30 and 21:00 are timed: a trip planned for next Saturday has no live traffic to show, and at 23:00 the roads are empty anyway. The distinct road legs across all options are collected, the four longest are timed in one parallel batch, and any delay over two minutes lengthens that leg and pushes every later time back. The rider sees it on the leg: "~8 min slower than usual: stationary traffic on Thika Road (A2)".
Four legs per search is a cost decision. Every timed leg is a paid API call. Longest-first spends them where traffic changes the answer most.
3. Colour only what was observed
Before November, routes in the app had traffic colours: green, amber and red sections along the line. They looked convincing. They were generated by the app from the time of day, not measured on any road.
A rider who sees red on Waiyaki Way and leaves twenty minutes early is acting on something we invented. So the colours were deleted, and replaced by a matcher with one rule: every colour comes from a real observation near the route.
/**
* Every colour comes from a real observation near the route. A stretch with
* nothing reported is "low" (green): nothing is guessed from the time of day.
*
* level 0 low (green) · 1 moderate (amber) · 2 heavy (red)
*/
final class TrafficMatcher
{
public const INCIDENT_MATCH_M = 30; // a TomTom incident lies on the road
public const REPORT_MATCH_M = 150; // a rider's report is near the road
private const CELL = 0.0005; // ~55 m grid for the spatial lookup
}Two different distances, because the two sources have different precision. A TomTom incident is a geometry along a specific road, so it has to lie within 30 m of the route. A community report is a tap on a phone by someone in a matatu, so it's allowed 150 m.
The whole route still gets a level, from its own durations: live over free-flow of 1.5 or more is heavy, 1.2 or more is moderate. That comparison is measured too.
The matcher is pure: no I/O, no clock, just geometry in and segments out. That makes it trivially testable, and it means the rule "nothing is guessed" is enforced by code that can't reach a guess.
No observation, no amber or red. The map got quieter. It also started to mean something.
4. Incidents without a request per rider
Incidents are also drawn on the map. The naive version asks TomTom for incidents in each rider's viewport. With a few thousand people looking at Nairobi at 07:30, that's a few thousand identical requests, and every rider waits for one.
Instead:
Incidents are fetched per metro area, not per viewport, and shared by every request in that area.
They're refreshed after the response, once older than five minutes (stale-while-revalidate). A rider never waits for TomTom: they get what's cached, and the refresh happens behind them.
Viewports are capped (half a degree across, 300 incidents), so a zoomed-out map can't trigger a country-wide query.
The general pattern: an external call should never be on the critical path of a request if its data can be a few minutes old. Traffic incidents can.
5. A budget, and a silent fallback
Every external API needs three limits, and the third is the one teams forget:
Limit | Value | What it protects |
|---|---|---|
Per-call timeout | short, per endpoint | Latency of the request |
Cache per result | 5 min for live timings, 6 h for future ones | Cost and latency for repeated trips |
Daily request budget |
| The bill |
Past the daily budget, routes keep their Valhalla times and nothing fails. And that's the general contract for every external call in the system: any error, timeout or exhausted budget returns null, and the caller falls back silently.
/**
* Any error or timeout returns null: callers fall back silently.
*/
public function timeRoutes(array $routes, ?CarbonInterface $departAt = null): array"Silently" is about the rider, not about operations. Every failure is logged as a warning (with the API key redacted from the message), and the budget is a counter per Nairobi day, so an outage or an exhausted budget shows up in the logs instead of in a support ticket. What the rider gets is a journey without live traffic, which is exactly what they'd have got from a planner that never had TomTom at all.
That is the test I'd apply to any optional integration: with the API down, is the product exactly as good as it was before you added it? If not, it isn't optional, and it needs the reliability engineering of a core dependency.
6. "When should I leave?" on demand
Typical-traffic timings unlock a feature that live timings alone can't: comparing departure times. For a car or boda route, the API times the same roads now and at +30, +60 and +90 minutes, and suggests a later departure only if it saves at least 5 minutes and at least 10%:
private const OFFSETS_MIN = [0, 30, 60, 90];
public const MIN_SAVING_S = 300;
public const MIN_SAVING_RATIO = 0.10;Both thresholds matter. A 4-minute saving isn't worth waiting an hour for. A 6-minute saving on a 2-hour drive isn't either.
It runs only when the rider taps the button: four TomTom calls per route is too expensive to spend on every search. Future timings are cached for hours and shared, because "typical traffic at 18:30 on this road" is the same for everyone.
7. Boda bodas: an honest estimate
Boda boda fares are negotiated at the roadside. There's no API to call, and pretending there is (a single precise price) would be wrong. So the planner gives a range, from a formula that's in the config, not hidden in code:
'boda' => [
'base_kes' => 50, // typical = base + per_km × km, at least min
'per_km_kes' => 35,
'min_kes' => 70,
'spread' => 0.2, // range = typical × (1 ± spread), rounded to KES 10
],
The routing side is real: Valhalla's motorcycle costing, with highways avoided and any route that still touches the Expressway (found by road name, since OpenStreetMap doesn't tag it as a toll road) dropped. The fare is clearly labelled as an estimate, in the API response and in the app.
That pairing is the pattern for any mode without a data feed: compute what you can compute exactly (the route), estimate what you can't (the price), and label which is which.
8. Riders as a data source
The most valuable external data isn't from an API at all. Riders report what they see from inside the matatu: a crash, flooding, a police check, a road closure. Those reports enter the same matcher as TomTom's incidents (with the wider 150 m tolerance) and colour the same sections of the same routes.
They also need the same discipline as any external source, applied differently:
They expire. Each report carries an
expires_atthat depends on its type: heavy traffic is useful for an hour, not a day. Queries only ever read live reports, and a scheduled job retires the rest.They're voted on. Other riders confirm or dispute a report, and a report that crosses five confirmations earns its author a one-time validation bonus. Agreement is the signal; one tap is a hint.
They're attributed. A report is shown as a report from riders, never as an official incident.
The rules, condensed
External data decorates; it never decides. Your engine chooses the roads.
Time your own geometry rather than adopting someone else's route.
Colour only what was observed. No observation, no warning colour, and never one guessed from the clock.
Share and pre-fetch data that can be minutes old; keep it off the request's critical path.
Budget every API per call, per result and per day.
Fail silently to the product you had before the API, and log loudly.
Estimate openly when there's no data, and say it's an estimate.
Part 8: Running It, Caching, Testing and Operating a Journey Planner closes the series: the latency and memory budgets of the whole system on one server, how to test a journey planner (golden traces, ranking tests, real trips), what to monitor, and what I'd build next.
Technical references
TomTom Calculate Route with
supportingPoints, and the Traffic Incidents API.Valhalla costing options (
motorcycle,use_highways,use_tolls).The stale-while-revalidate pattern: RFC 5861.





