# POST /v1/proximity

> Distance and drive time between places, the nearest airports, substations and hospitals, drive-time screens and labor sheds.

Source: https://docs.invariant-ai.com/api/proximity

`POST https://api.invariant-ai.com/v1/proximity`

Proximity answers questions about the relationship between places: how far, how long a drive, which is closest, which sites clear a drive-time bar, and how many workers live within a commute. One body; `op` picks the operation.

| `op` | Answers |
| --- | --- |
| `distance` | Distance (and, driving, duration) for every origin and destination pair. |
| `nearest` | The closest records of a curated set: airports, substations, power plants, rail, ports, urban areas, hospitals. |
| `screen` | Which origins are within a drive-time band of any of up to 10 anchors, and how close the others came. |
| `labor_shed` | Civilian labor force and population of the Census tracts within a drive time of one place. |

```bash
curl -s https://api.invariant-ai.com/v1/proximity \
  -H "X-API-Key: $INVARIANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"op": "nearest", "origin": "39.0438,-77.4874", "set": "@substations", "n": 2, "mode": "straightline"}'
```

```json
{
  "op": "nearest",
  "origin": {"query": "39.0438,-77.4874", "lat": 39.0438, "lng": -77.4874, "formatted_address": null, "accuracy_type": null, "accuracy": null, "error": null},
  "candidates": [
    {"name": "ASHBURN", "lat": 39.051392, "lng": -77.49837, "attributes": {"max_voltage_kv": 230.0},
     "distance_miles": 0.789, "distance_km": 1.2698, "duration_seconds": null, "duration_minutes": null},
    {"name": null, "lat": 39.016046, "lng": -77.480385, "attributes": {"max_voltage_kv": 230.0},
     "distance_miles": 1.9514, "distance_km": 3.1404, "duration_seconds": null, "duration_minutes": null}
  ],
  "applied_filters": null,
  "paid_driving_calcs": 0,
  "billed_results": 0,
  "notes": ["coverage: US + Canada"]
}
```

## Driving and straight line

- **`mode: "straightline"`** (on `distance` and `nearest`) is the WGS84 ellipsoidal distance, computed locally. Durations are `null`.
- **Driving** (the default for `distance` and `nearest`, always for `screen` and the routed part of `labor_shed`) uses this deployment's road-routing engine. Durations reflect typical traffic, not real-time conditions, and every driving response says so in `notes`. Driven distances are rounded to 0.1 km and durations to whole seconds.
- **Snap guard.** A driven leg shorter than 95% of the straight line is not a real route: the engine snapped an unreachable point (an island, open water) to the nearest road. It comes back with `flag: "unreachable_or_snapped"` and null durations.

**Driving availability.** Driving needs a routing engine. If a deployment has none, driving requests answer `503 proximity_unconfigured`; straight-line work and labor-shed estimates still work.

## Locators

Every origin, destination and anchor is either a `"lat,lng"` string or a US street address with a city and state or a ZIP. It is never a place name: `"JFK Airport"` is refused. Reach named infrastructure through `nearest`, or send its coordinate.

Addresses go through the same accuracy gate as [`POST /v1/geocode`](https://docs.invariant-ai.com/api/geocode). At most 25 locators in one request may be addresses; resolve larger batches with geocode first and send coordinates.

Each locator is echoed back with how it resolved:

```json
{"query": "JFK Airport", "lat": null, "lng": null, "formatted_address": null, "accuracy_type": null, "accuracy": null, "error": "low_confidence_resolution"}
```

`error` is `null`, `low_confidence_resolution` (matched only an area), `unresolvable_input` (nothing matched) or `geocoding_failed` (the lookup failed; retry). A locator that fails is left out of the results and named in `notes`. If every origin (or every `screen` anchor) fails, the request is refused with `422 unresolvable_input`, listing up to 10 failures with their `index`.

## distance

```json
{"op": "distance", "origins": ["40.630973,-73.97228"], "destinations": ["40.6413,-73.7781"], "mode": "driving"}
```

Up to 500 origins and 500 destinations, at most 10,000 pairs. Each leg carries `origin_index`, `destination_index`, `distance_miles`, `distance_km`, `duration_seconds`, `duration_minutes` and `flag`, plus `resolved_origins` and `resolved_destinations`. `units` is accepted and reserved; both units are always returned.

## nearest

```json
{"op": "nearest", "origin": "39.0438,-77.4874", "set": "@hospitals", "n": 3, "mode": "driving"}
```

`n` is 1 to 25. Each set is searched within 160 km; nothing within range is an empty list and a note, not an error.

| `set` | Data | Default | `filters` |
| --- | --- | --- | --- |
| `@airports` | FAA airports (current 28-day NASR cycle) | Operational airports; private use excluded, unrated included | none |
| `@substations` | HIFLD electric substations | `max_voltage_kv` of 115 or more; unrated ones are never included | `{"min_kv": 230}` |
| `@power_plants` | EIA power plants | all | none |
| `@rail` | BTS North American rail network (active lines) | nearest point of each line | none |
| `@ports` | BTS principal ports | nearest point of each port area | none |
| `@urban_areas` | Census 2020 urban areas | nearest boundary point (the origin itself when inside) | none |
| `@hospitals` | CMS-certified hospitals | Acute care and critical access hospitals with an emergency department | `type` (comma-separated: `acute`, `critical_access`, `psychiatric`, `childrens`, `rural_emergency`, `long_term`, `va`, `dod`, `cancer`), `has_emergency` (`"true"`/`"false"`), `min_beds`, `record_kind` |

A `type` or `has_emergency` filter replaces the hospital default. Hospital candidates carry `ccn`, `hospital_type`, `beds`, `has_emergency`, `emergency_evidence`, `coord_source` and `record_kind`. `applied_filters` echoes what you sent; with no filters it is `null` even though the defaults above apply.

In driving mode the nearest `min(25, 5 × n)` records by straight line are routed and ranked by duration; records without a usable route are dropped and counted in `notes`. `billed_results` is the number of candidates returned.

`"@places"` (brand and category place records) is not available yet and answers `503 proximity_data_unavailable`.

## screen

```json
{"op": "screen", "origins": ["40.70,-73.95", "40.75,-73.80", "40.90,-73.70"], "anchors": ["40.6413,-73.7781"], "max_minutes": 30, "min_minutes": null}
```

Every origin is routed to every anchor (up to 500 origins, 10 anchors, 10,000 pairs). An origin whose best drive to any anchor is within `[min_minutes, max_minutes]` is a survivor (`origin_index`, `best_anchor_index`, `best_duration_seconds`, `best_duration_minutes`). Every other origin is in `screened_out` with its own best duration, so you can see how close it came; a null duration means no anchor was reachable by road.

## labor\_shed

For a site comparison with runnable requests and a Census reference study, see [Estimate a labor shed by drive time](https://docs.invariant-ai.com/estimate-labor-shed-by-drive-time).

For a warehouse shortlist, [compare mapped freight access and confirm the truck gate](https://docs.invariant-ai.com/screen-warehouse-rail-and-truck-access). The driving request has no vehicle height, weight, axle or trailer parameters.

```json
{"op": "labor_shed", "origin": "32.9,-96.9", "minutes": 45}
```

The civilian labor force (ACS 2020-2024 5-year, B23025) and the 2020 Census population of every tract whose population-weighted centroid is within `minutes` of driving (5 to 90).

Tracts are first sorted by geometry alone, at no cost: those within a straight-line distance coverable at 20 mph count, those beyond 75 mph cannot be reached, and only the tracts between (the annulus) are routed. `tracts_matrix_queried` is the annulus; `tracts_unreachable` counts annulus tracts with no usable route (they are excluded). An annulus above 3,000 tracts is refused with `422 shed_too_large`. A tract with no labor-force estimate adds nothing to `civilian_labor_force`; its population still counts.

- `"estimate": true` returns `annulus_tracts` without routing anything.
- `"include": ["demographics"]` adds ACS age, household, tenure and income sums over the same tracts (`sums`, `shares`, the tract list). A tract with any missing cell is left out of every sum and listed in `missing_tract_geoids`. `pop_total` and the four `medians` come back `null` for now.

## Errors

| HTTP | `error` | Retry | When |
| --- | --- | --- | --- |
| 400 | `invalid_request` | no | Body does not match the op, or breaks a limit (pairs, address count, `min_minutes` above `max_minutes`, a filter the set does not take, `radius_m` or `"return": "count"` with a curated set). |
| 400 | `coord_out_of_bounds` | no | A `nearest` origin outside the US envelope. |
| 401 | `auth_missing`, `auth_invalid` | no | See [Authentication](https://docs.invariant-ai.com/authentication). |
| 422 | `unresolvable_input` | no | Every locator of a required role failed. |
| 422 | `unknown_set` | no | `set` is not one of the sets above. |
| 422 | `shed_too_large` | no | The labor-shed annulus is above 3,000 tracts. |
| 429 | `proximity_busy` | yes | At capacity. Honour `Retry-After`. |
| 502 | `upstream_transient` | yes | The routing engine failed for a moment. Honour `Retry-After`. |
| 502 | `upstream_error` | no | The routing engine refused the request. |
| 503 | `proximity_unconfigured` | no | Driving was asked for and this deployment has no routing engine. |
| 503 | `proximity_data_unavailable` | depends | A set's data could not be read (the message names the cause), or `@places`. |
| 504 | `proximity_deadline_exceeded` | yes | The request took longer than 60 s. |

`max_cost_usd` is accepted for compatibility; proximity calls are not billed per calculation.
