Skip to main content

Overview

Go client for the JustRouting API — routing, distance matrices, map matching, trips, nearest-road lookup, vehicle routing optimization, and geocoding across Southeast Asia. No dependencies outside the standard library.

Install

Quickstart

The coordinates above are in Singapore. See Coordinates must share a country.

Services

A Client exposes eight services.

Routes

Routes.Get returns the best route. Routes.GetAll additionally returns alternatives and the snapped input waypoints.
Route.Geometry holds whichever encoding you asked for:

Matrix

Travel time and distance between many points at once.
The accessors return false for unreachable pairs. The API reports those as null, which is deliberately kept distinct from a genuine zero — that is why Durations and Distances hold *float64.

Map Matching

Snap a noisy GPS trace onto the road network and get the route that was driven.
Match embeds Route, so every route field (Distance, Duration, Geometry, Legs, …) is promoted, plus the engine’s Confidence (0–1). MapMatching.GetAll additionally returns Tracepoints — the input coordinates snapped to the road network, aligned with your input; an entry is nil when the engine could not match that point. Useful options: Timestamps (UNIX seconds per point), Radiuses (max snap distance, one value per point — the default is only a few metres, so noisy GPS points usually need it), Gaps ("split"/"ignore"), Tidy, Waypoints (indices to use as waypoints), and Snapping ("default"/"any").

Trip

Visit a set of points in the fastest possible order (a travelling-salesman heuristic).
By default the trip returns to its starting point; pass Roundtrip (a bool pointer) to change that, and Source/Destination ("any", "first"/"last") to pin the ends. Trip.GetAll also returns Waypoints in the order the trip visits them.

Nearest

Find the road segment closest to a coordinate.
Pass Number to get the second-, third-, … nearest segments; Nearest.GetAll returns all of them. The returned Waypoint includes the OSM Nodes of the matched segment.

Geocode

Geocode.Search converts an address into coordinates, by free text or by structured fields (exactly one of the two):
Results are ordered best first; an empty Results list simply means nothing matched. The client always requests format=json, regardless of the API’s default response format. Use Filters (repeatable, e.g. countrycode:sg) to restrict results and Bias (e.g. proximity:103.8,1.3) to prefer places near a point. GeocodeResult.Location() returns a Point in the usual [longitude, latitude] order, ready to feed into Routes, Matrix, or Optimization. Errors from the geocoding upstream are classified by HTTP status like any other failure (401 → ErrUnauthorized, 429 → ErrRateLimited, 502 → ErrUpstreamUnavailable).

Optimization

Assign tasks to a fleet and order each vehicle’s stops.
Use Shipments instead of Jobs for pickup-and-delivery pairs that must be served in order by the same vehicle.

Health

The only call that works without an API key, which makes it a useful connectivity check.

Error handling

Every failure is an *Error. Classify it with errors.Is against the package sentinels rather than matching on message text:
Reach for the concrete type when you need the status code or raw body:

Configuration

NewClient never returns an error. If an option is invalid, the failure is retained and returned by the first API call, so a misconfigured client fails loudly at the point of use instead of silently falling back to a default.

Retries

Rate limits (429), server errors (5xx), and transport failures are retried with exponential backoff and jitter; a Retry-After header takes precedence when present. Other 4xx responses are returned immediately — they would fail identically on a retry and would still consume quota. Retries are bounded by the context, not the HTTP client’s timeout. A timeout on your http.Client applies to each individual attempt; use a context deadline to bound the whole call:

Things to know

Coordinates are [longitude, latitude]

This is the GeoJSON order, and the reverse of the “lat, lng” used by most map UIs. Swapped coordinates are usually caught locally — a longitude in the latitude slot fails the [-90, 90] check before a request is sent — but a swap that stays in range will silently route somewhere unexpected. Point is defined over []float64, so both of these work:

Coordinates must share a country

Every coordinate in a single request must fall within one country; the API routes each request to a per-country engine. A Singapore → Kuala Lumpur request fails with ErrCrossCountry. Supported countries: Brunei, Cambodia, Indonesia, Laos, Malaysia, Myanmar, the Philippines, Singapore, Thailand, and Vietnam.

Plan limits

Exceeding a size limit returns ErrPlanLimitExceeded; exhausting the daily allowance returns ErrQuotaExceeded.

Examples

Runnable programs live in examples/:

Development

The default suite runs entirely against httptest servers — no network access and no API key. Integration tests hit a live API and are behind a build tag, so they never run by accident:

License

MIT