# A job lane for problems bigger than a request deadline

`POST /optimise` takes at most 200 unique locations and `POST /matrix` at most 10,000 elements. Neither number is a limit on what the solver or the engine can do: both are what finishes inside the gateway's 30-second request deadline. So the same three problems now have an asynchronous lane at `POST /v1/jobs/optimise`, `POST /v1/jobs/replan` and `POST /v1/jobs/matrix`, with the same request bodies, ten times the locations, four times the matrix elements, and either free polling or a signed webhook when the answer is ready.

## Why is the synchronous cap not the real cap?

Because it measures our patience with an open HTTP connection, not the solver's ability. A fleet planning tomorrow's rounds does not need an answer in thirty seconds. It needs a bigger answer. Holding a request open while a 2,000-stop problem solves is a bad deal for everyone: it ties up a connection, it fails badly on a network blip, and it gives a caller no way to ask how it is going.

The caps that apply on each lane:

| Lane | Endpoint | Cap | Setting |
|---|---|---|---|
| Optimise, synchronous | `POST /optimise` | 200 unique locations | none |
| Optimise, asynchronous | `POST /v1/jobs/optimise` | 2,000 unique locations | `SN_OPTIMISE_MAX_LOCATIONS_ASYNC` |
| Re-plan, synchronous | `POST /optimise/replan` | 200 unique locations, on the remaining problem | none |
| Re-plan, asynchronous | `POST /v1/jobs/replan` | 2,000 unique locations, likewise | `SN_OPTIMISE_MAX_LOCATIONS_ASYNC` |
| Matrix, synchronous | `POST /matrix` | 10,000 elements | none |
| Matrix, asynchronous | `POST /v1/jobs/matrix` | 40,000 elements | `SN_MATRIX_MAX_ELEMENTS_ASYNC` |

Crossing a synchronous cap now returns a `422` that names the asynchronous lane in its `detail` and in an `async_lane` field, rather than telling you to make your problem smaller.

## How much of my request has to change?

The URL, and nothing else. The body is exactly the synchronous body, plus an optional `webhook_url`.

```sh
curl -fsS -X POST "https://api.mapmap.ai/v1/jobs/optimise" \
  -H "Authorization: Bearer $MAPMAP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "vehicles": [{"id": 1, "start": {"lat": 51.5074, "lon": -0.1278}}],
    "jobs": [
      {"id": 10, "location": {"lat": 52.4862, "lon": -1.8904}},
      {"id": 11, "location": {"lat": 53.4808, "lon": -2.2426}}
    ],
    "costing": "auto",
    "webhook_url": "https://hooks.example.com/mapmap"
  }'
```

The `202` carries the id, what it will cost and where to fetch it:

```json
{
  "id": "0f7c2a1e-9f2b-4c31-9a52-8f2a0c5b1d44",
  "kind": "optimise",
  "status": "queued",
  "result_url": "https://api.mapmap.ai/v1/jobs/0f7c2a1e-9f2b-4c31-9a52-8f2a0c5b1d44",
  "units_charged": 10
}
```

`status` moves `queued` to `running` to `succeeded` or `failed`. While it is either of the first two, the record carries no `result` field and no `error` field at all, so an unfinished job can never be misread as an empty answer. Once it succeeds, `result` carries exactly the body the synchronous endpoint would have returned, because the two lanes run the same code.

## What does polling cost?

Nothing. `GET /v1/jobs/{id}` and `GET /v1/jobs` draw no quota units, in the same way as `GET /v1/keys/self` and `GET /v1/usage`. Polling is how the lane is meant to be used, so charging for it would be charging for our own architecture. Poll as often as you find useful.

A job belongs to the key that submitted it, or to any key on the same account, which means a fleet rotating keys keeps sight of its own jobs. Anybody else's job is a `404`, never a `403`, because a `403` would confirm that the id exists.

Units are charged at submission, on exactly the formula the synchronous endpoint uses. A 200-location problem costs the same on either lane. Moving work off the request path buys a bigger problem and a job id, never a discount. If a job fails, every unit its submission drew is returned to your included volume and any prepaid credit it debited is returned to your ledger; the record still reports `units_charged` alongside `refunded: true`, so the two together are an audit trail rather than a number that quietly changed. The one exception is a submission paid with an x402 [machine payment](/docs/x402): an on-chain settlement cannot be reversed, so those return quota units only. A submission refused before a worker ever looks at it costs nothing at all.

## Why is the webhook empty?

Because a 2,000-location plan is megabytes, and an endpoint that may be slow, may be down and may be logging request bodies is the wrong place to send one. The delivery says only that the job finished and where to fetch it.

```json
{
  "job_id": "0f7c2a1e-9f2b-4c31-9a52-8f2a0c5b1d44",
  "kind": "optimise",
  "status": "succeeded",
  "result_url": "https://api.mapmap.ai/v1/jobs/0f7c2a1e-9f2b-4c31-9a52-8f2a0c5b1d44"
}
```

You fetch the result from `result_url` with your own API key, when you are ready. The target must be `https` and must resolve to a public address: loopback, private, link-local (cloud metadata addresses included) and non-resolvable hosts are refused at submission with a `400`, as are URLs carrying credentials. The gateway will not be used to reach inside a network on a caller's behalf.

Deliveries are signed. Mint a secret once with `POST /v1/keys/self/webhook-secret`, and every delivery then carries `X-MapMap-Signature`, the lowercase hex HMAC-SHA256 of the raw request body. Submitting a `webhook_url` on a key with no secret is a `409` rather than an unsigned delivery, because a notification anybody who learns your URL could forge is not worth sending. Two rules make or break the verification: sign the raw bytes you received rather than a re-encoding of the parsed object, and compare in constant time. The [verification snippets](/docs/api-reference#verifying-a-webhook) for Node and Python are in the reference.

## What the lane does not give you

**No retry queue.** Delivery is attempted three times with exponential backoff. After that the job records `webhook_status: "delivery_failed"` and the gateway stops trying. The job itself is unaffected, so poll it. Treat the webhook as an optimisation over polling, not as a guarantee.

**Seven days of retention.** A finished job's result is kept for seven days (`SN_JOBS_TTL_DAYS`), then the row is deleted and the id is a `404`. It is never a job reporting success with nothing in it. Queued and running jobs are never swept by age.

**Ten jobs at once.** One account may have 10 jobs queued or running (`SN_JOBS_MAX_PENDING`). Over that, submission answers `429` with `urn:sn-gateway:problem:jobs-pending-limit`, naming the cap and what is currently pending. It is a capacity limit rather than a billing one, so the body points at `/v1/jobs` instead of a top-up page: waiting is the fix, not paying.

**Self-hosters have a second cap to raise.** Lifting `SN_OPTIMISE_MAX_LOCATIONS_ASYNC` above 1,000 needs matching changes on your VROOM sidecar, because `vroom-express` ships `maxlocations: 1000` and a `1mb` request-size limit, and large jobs otherwise fail before the solver sees them. Optimisation on a deployment with no VROOM sidecar answers `503` rather than pretending.

## Try it

If your day is genuinely one problem, submit it to the lane. If it is really six rounds that fit, the cheaper answer is to split it: `POST /v1/cluster` groups stops into balanced clusters, and a 900-stop day becomes six 150-stop optimisations that each fit the synchronous cap comfortably. Clustering is the right answer when the rounds are real, and the asynchronous lane is the right answer when they are not. The [optimisation guide](/docs/optimisation) covers both, and the [asynchronous jobs reference](/docs/api-reference#asynchronous-jobs) has the full job record shape.
