Carpathian Maps

Connect

Two things connect to Carpathian Maps: the map, which any browser or app draws from the public map files, and directions, which your backend asks for with the key.

The map

The map files are public and need no key. Point any MapLibre client at the map base URL:

https://maps.carpathian.ai/map
FileHolds
tiles.jsonTileJSON that points a map client at the tiles below
tiles/{z}/{x}/{y}.mvtOne vector tile per request, which the browser keeps in its cache between page loads
planet.pmtilesThe same tiles in one PMTiles archive, read with HTTP Range requests
fonts/{fontstack}/{range}.pbfThe label fonts
sprites/v4/light, sprites/v4/darkThe map icons

MapLibre GL JS

Install MapLibre and the Protomaps map style:

npm install maplibre-gl @protomaps/basemaps

Then draw the map:

import maplibregl from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import { layers, namedFlavor } from "@protomaps/basemaps";

const base = "https://maps.carpathian.ai/map";

const map = new maplibregl.Map({
  container: "map",
  center: [-93.625, 41.5868],
  zoom: 12,
  style: {
    version: 8,
    glyphs: base + "/fonts/{fontstack}/{range}.pbf",
    sprite: base + "/sprites/v4/light",
    sources: {
      protomaps: {
        type: "vector",
        url: base + "/tiles.json",
        attribution:
          '<a href="https://protomaps.com">Protomaps</a> &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
      },
    },
    layers: layers("protomaps", namedFlavor("light"), { lang: "en" }),
  },
});

For a dark map, use sprites/v4/dark and namedFlavor("dark"). The map page here runs MapLibre GL JS 5.24 and Protomaps Basemaps 5.7, so those versions are known to work with these files.

MapLibre reads tiles.json and then asks for each tile at tiles/{z}/{x}/{y}.mvt. Each tile is cached for a day, so a reload or a second visit draws from the browser's cache. No PMTiles library or protocol is needed.

A client that reads PMTiles itself can still use planet.pmtiles with the pmtiles library and url: "pmtiles://" + base + "/planet.pmtiles". Browsers and app web views do not keep its Range requests between page loads, so they fetch the map again on every reload.

Credits

Keep the OpenStreetMap credit visible on every map. The ODbL requires it, and the attribution above shows it.

Other sites

A browser on another website reads the map files only if that website is listed on one of your apps in Carpathian. Add every address the map is shown on, for example https://app.example.com. A browser on an unlisted website gets 403, and the browser console shows a CORS error.

To check that the tiles answer:

curl https://maps.carpathian.ai/map/tiles.json

Directions

Routes and isochrones need a key, sent as a bearer token. Get one in Carpathian: open Services, turn on Carpathian Maps, then create an app. The key is shown once. Keep it on your backend and never ship it to a browser or app. Its limits and pricing are in the Usage policy.

curl https://maps.carpathian.ai/v1/route \
  -H "Authorization: Bearer $MAPS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"origin":{"lat":41.5868,"lng":-93.625},"destination":{"lat":41.6005,"lng":-93.6091},"mode":"walk"}'

mode is walk, bike, drive or transit. Directions work only inside the areas listed under Coverage. The API reference has every field of the answer.

From a backend

Your users' browsers call your backend, and your backend calls this server with the key and passes the answer on. Your backend decides who may ask for directions and how often; this server only knows the key.

Node 18 or later:

async function route(origin, destination, mode) {
  const res = await fetch("https://maps.carpathian.ai/v1/route", {
    method: "POST",
    headers: {
      Authorization: "Bearer " + process.env.MAPS_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ origin, destination, mode }),
  });
  const body = await res.json();
  if (!res.ok) throw new Error(body.error);
  return body.route;
}

Python, standard library only. urlopen raises HTTPError on any status other than 2xx:

import json, os, urllib.request

def route(origin, destination, mode):
    req = urllib.request.Request(
        "https://maps.carpathian.ai/v1/route",
        data=json.dumps({"origin": origin, "destination": destination, "mode": mode}).encode(),
        headers={"Authorization": "Bearer " + os.environ["MAPS_API_KEY"], "Content-Type": "application/json"},
    )
    with urllib.request.urlopen(req, timeout=20) as r:
        return json.load(r)["route"]

Draw a route

path is [[lat, lng], ...], and GeoJSON wants [lng, lat], so swap each pair. Add the layer once the map has loaded:

map.addSource("route", {
  type: "geojson",
  data: {
    type: "Feature",
    geometry: { type: "LineString", coordinates: route.path.map(([lat, lng]) => [lng, lat]) },
  },
});
map.addLayer({ id: "route", type: "line", source: "route", paint: { "line-color": "#2563eb", "line-width": 5 } });

Isochrone geometries are already GeoJSON in [lng, lat] order and go straight into a fill layer.

Check the connection

curl https://maps.carpathian.ai/healthz

{"valhalla": true, "transit": true, "otp": true} means both route engines answer: Valhalla for walk, bike and drive, MOTIS for transit (otp is its old name, kept for older clients). A 503 from /v1/route means the engine for that mode is down or rebuilding, so retry later. Every status is listed under Errors.