FIELDFLOW

Route optimization for field sales teams. Reps were driving twice the distance they needed to. I built the system that fixed it.

-85%Routing API Costs
100%Data Isolation
OSRMSelf-Hosted Engine
MultiPhase Clustering

THE PROBLEM

The company had an Excel sheet and told people to figure it out

I was working in solar energy sales. Every morning, field reps got a list of addresses and no logic to go with it. No clustering. No optimization. Just addresses in the order someone typed them. People were driving 200km when 80km would have covered the same stops. Burning fuel, burning time, hitting half the clients they could have. Management knew. Nobody fixed it. They had an Excel sheet and told people to figure it out. I cared more than I should have. So I built it.

THE SOLUTION

One button. Optimized routes. Synced back to the CRM automatically.

FieldFlow pulls leads from the CRM through a Chrome extension, clusters them by geography and scheduling constraints, and generates optimized daily routes using real road distances — not straight-line estimates. The routing runs on a self-hosted OSRM instance. Actual roads, actual travel times. The rep opens the app, hits “Generate Routes,” and gets a compact visit sequence that cuts driving to the minimum. If a lead cancels mid-route, the system finds the nearest replacement and re-optimizes on the spot. Every status update syncs back to the CRM automatically. The rep doesn't have to touch two tools to keep everything current.

Engineering Perspective

"Google Maps charges per matrix calculation. At real usage — hundreds of leads, daily route generation — that bill adds up fast. I didn't want the cost model to punish people for using the tool correctly. So I self-hosted OSRM on my own server using OpenStreetMap data. Zero cost per query. I built batching logic that splits large lead sets into geographic quadrants so responses stay under two seconds. It's more work upfront, but the economics are completely different."

EXECUTION LIFECYCLE

1. CRM Ingestion

Custom Chrome extension scrapes CRM pages and pushes to Next.js API. Leads with invalid phone data are automatically flagged for verification.

2. Distance Matrix Calculation

System builds an OSRM distance matrix (Table API) for all eligible leads. Requests >300 leads are batched via geographic quadrant partitioning.

3. Multi-Phase Routing

Phase A: Clusters leads with hard time-constraints. Phase B: Clusters remaining by proximity. Final step optimizes visit order via OSRM Trip API (TSP).

4. Route Versioning & Execution

Routes are assigned to a calendar day. If a lead drops out, system patches the route by identifying proximity-scored filler candidates and re-optimizing.

TECHNICAL ARCHITECTURE

EXTERNAL
Custom CRM
CLIENT
Chrome Extension
APPLICATION LAYER
Next.js 15 App Router
tRPC 11Better Auth
PERSISTENCE
PostgreSQL + Drizzle
Lead States, Route Versions, Billing Tokens
ROUTING ENGINE
Self-Hosted OSRM
Table API (Matrix) & Trip API (TSP)

TECHNICAL DECISIONS

Why I built it this way

What we gainedWhy self-hosted OSRM

Google Maps charges per request. Route matrix calculations at scale means thousands of API calls per day. OSRM runs on my own server, pulls from OpenStreetMap, and costs nothing per query. I wrote batching logic that partitions large lead sets into geographic quadrants to keep response times under 2 seconds regardless of set size.

What we gainedWhy tRPC over REST

With REST, one schema change and something breaks silently. tRPC gives you end-to-end type safety — one change propagates everywhere automatically. "The API returns this field but the frontend expects that field" is not a bug I want to spend time debugging.

What we gainedWhy route versioning

When a lead cancels mid-route, you can't just delete the stop. The original plan needs to stay as history — for billing, for audit, for the rep who needs to explain why they skipped an address. Every change creates a new RouteVersion. The rep only sees the current version.

What we gainedWhy token-based billing

Per-lead pricing means you pay for what you actually use. A token gets consumed on first calculation only — re-optimizing the same lead doesn't cost again. The billing model matches how the tool actually gets used instead of forcing everyone into the same box.

Next.js 15tRPC 11Drizzle ORMPostgreSQLOSRMBetter Auth