Skip to content
Documentation menu
docs / api / post-v1-cluster
Optimisation and jobs

POST/v1/cluster

Access and price

AuthAn API key, as Authorization: Bearer snk_… or ?api_key=snk_…. Metered. Needs a full key; a browser (maps) key is refused.
Price1 call per started block of 100 locations, at most 5000 locations. Class: standard, or premium for truck and ADR work. Published as unit_rules.cluster in /pricing.json.
PremiumWhen the body asks for truck routing: "costing": "truck", a top-level adr object or "hazmat": true

What it does

Capacity-constrained clustering.

An API key, as Authorization: Bearer snk_… or ?api_key=snk_…. Metered. Price: 1 call per started block of 100 locations, at most 5000 locations.

Call it

bash
curl -fsS -X POST "https://api.mapmap.ai/v1/cluster" \
  -H "Authorization: Bearer $MAPMAP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"locations":[{"id":"d-1001","lat":51.28,"load":3,"lon":1.08},{"id":"d-1002","lat":51.29,"load":5,"lon":1.11},{"id":"d-1003","lat":51.51,"load":2,"lon":-0.09}],"max_cluster_size":{"locations":150}}'

Request body

application/json

locations ([{id, lat, lon, load?}], ids unique, at most 5,000), plus EXACTLY ONE of clusters (how many groups to produce, balanced by stop count) or max_cluster_size ({"locations": n} or {"load": x}, a per-cluster ceiling from which the count is derived). Optional territories ([{id, polygon}], GeoJSON [lon, lat] rings, same shape as on POST /optimise) keeps clusters from straddling a round: each territory is clustered on its own, and so are the stops inside none of them. Optional seed (default 42) drives the k-means++ seeding; the same request with the same seed always returns the same clusters

Example: From website/public/docs/optimisation.md:837

json
{
  "locations": [
    {
      "id": "d-1001",
      "lat": 51.28,
      "load": 3,
      "lon": 1.08
    },
    {
      "id": "d-1002",
      "lat": 51.29,
      "load": 5,
      "lon": 1.11
    },
    {
      "id": "d-1003",
      "lat": 51.51,
      "load": 2,
      "lon": -0.09
    }
  ],
  "max_cluster_size": {
    "locations": 150
  }
}

Responses

200clusters[] with id, member locations (your ids), count, summed load, centroid and the territory each belongs to; parameters echoing the seed, the cluster count, the iterations run and whether it converged; balance naming the constraint applied and whether it had to be relaxed; and a basis stating that this is straight-line clustering, with the pointer to POST /optimise for road-network order
FieldTypeRequiredDescription
balanceobjectnoHow the clusters were balanced, the largest cluster's size and load, and whether a limit was relaxed.
basisstringnoWhat the distances are: straight lines on a local projection, not road distances.
clustersobject[]noOne entry per cluster: id, centroid, count, load and the member locations.
parametersobjectnoThe solve: clusters, locations, iterations, converged, seed and territory counts.

Example: Recorded from the hosted gateway on 2026-11-05 with a test key: POST /v1/cluster, two Brighton drops and one London drop; lists trimmed to their first items

json
{
  "balance": {
    "basis": "locations",
    "largest_cluster_load": 10,
    "largest_cluster_locations": 3,
    "max_load_per_cluster": null,
    "max_locations_per_cluster": 150,
    "note": "every cluster is inside the ceiling.",
    "relaxed": false
  },
  "basis": "Straight-line clustering. Distances are measured between coordinates on a local planar projection, NOT along the road network: no road, river, motorway junction or one-way system is consulted, and two stops either side of an estuary look adjacent. Use this to decide which stops belong together, then POST /optimise per cluster to decide the order, because that is where the routing engine's own matrix, your costing and any truck or ADR constraints actually apply. Clustering here commissions no engine work and makes no claim about drive time.",
  "clusters": [
    {
      "centroid": {
        "lat": 51.05493333333334,
        "lon": -0.12546666666666667
      },
      "count": 3,
      "id": 0,
      "load": 10,
      "locations": [
        "d-1001",
        "d-1002",
        "d-1003"
      ],
      "territory": null
    }
  ],
  "parameters": {
    "clusters": 1,
    "converged": true,
    "iterations": 2,
    "locations": 3,
    "locations_outside_all_territories": 0,
    "seed": 42,
    "territories": 0
  }
}
400Invalid request: neither or both sizing fields, a duplicate or empty location id, an invalid coordinate or load, an invalid territory, more clusters than locations, or a ceiling no clustering could satisfy

application/problem+json · Problem

401Missing or invalid API key. Also: No key, or a missing, malformed, unknown, revoked or expired one (unauthorized); the body carries signup, docs and a one-call hint for a free key

application/problem+json · Problem

402Beyond the included volume with no way to pay this call: an x402 body naming how to pay (provisional key past its allowance, prepaid balance below the price, or a rejected X-PAYMENT). Not problem+json

application/json · X402PaymentRequired

403The key may not call this: a browser key outside its scope (scope-insufficient), a request from an origin the key does not allow (origin-not-allowed), or an account restriction (forbidden)

application/problem+json · Problem

413The request declares a body larger than 1048576 bytes (plain text, not problem+json)

text/plain

422More than 5,000 locations, or a locations x clusters assignment larger than 500,000

application/problem+json · Problem

429Quota or rate limit exceeded. Also: Per-key rate limit (rate-limited, with Retry-After), or monthly quota exhausted with no payment path (quota-exceeded)

application/problem+json · Problem

504The request did not finish within the gateway's request deadline (request-timeout)

application/problem+json · Problem

Error bodies follow the shared problem model documented on API conventions.

Also under Optimisation and jobs

Generated from https://api.mapmap.ai/openapi.json on 2026-11-12 · operationId post_cluster