|
| 1 | +# closecity — Python client for the Close API |
| 2 | + |
| 3 | +Travel times from every US census block to nearby points of interest, by walking, |
| 4 | +biking, and public transit — the data behind [close.city](https://close.city), |
| 5 | +over the [Close API](https://api.close.city). |
| 6 | + |
| 7 | +**Documentation:** https://henryspatialanalysis.github.io/closecity-python/ |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install closecity |
| 11 | +pip install "closecity[geo]" # + GeoPandas output (to_geopandas) |
| 12 | +``` |
| 13 | + |
| 14 | +## Quickstart |
| 15 | + |
| 16 | +```python |
| 17 | +from closecity import Client |
| 18 | + |
| 19 | +# The API key (ck_live_… / ck_test_…) is created at https://account.close.city. |
| 20 | +with Client("ck_live_your_key_here") as close: |
| 21 | + # Fastest travel time to each category from a census block. |
| 22 | + summary = close.block_summary("410390020001010", mode = ["walk", "transit"]) |
| 23 | + for row in summary.results: |
| 24 | + print(row["dest_type_id"], row["mode"], row["travel_time"]) |
| 25 | + |
| 26 | + # Metering is surfaced on every metered reply. |
| 27 | + print(summary.tokens_charged, "charged;", summary.tokens_remaining, "left") |
| 28 | +``` |
| 29 | + |
| 30 | +Free routes (catalog, place lookup, health) need no key: |
| 31 | + |
| 32 | +```python |
| 33 | +from closecity import Client |
| 34 | +close = Client() |
| 35 | +print(close.modes().data["modes"]) |
| 36 | +print(close.last_updated().data["last_updated"]) |
| 37 | + |
| 38 | +# Resolve a city name to its census place GEOID + centroid: |
| 39 | +print(close.places("Providence").data["places"][0]) |
| 40 | +``` |
| 41 | + |
| 42 | +## Pagination |
| 43 | + |
| 44 | +List endpoints return a `Paginator` that follows the opaque keyset cursor for you. |
| 45 | +Iterate it for every record, or use `.pages()` for per-page metadata: |
| 46 | + |
| 47 | +```python |
| 48 | +# Every POI near a point, across all pages: |
| 49 | +for poi in close.pois_search(lat = 44.05, lon = -123.09, radius_m = 2000): |
| 50 | + print(poi["name"], poi["address"]["city"]) |
| 51 | + |
| 52 | +# Or page-by-page, watching the token balance drop: |
| 53 | +for page in close.block_pois("410390020001010", limit = 500).pages(): |
| 54 | + print(len(page.results), "rows;", page.tokens_remaining, "tokens left") |
| 55 | +``` |
| 56 | + |
| 57 | +Cursors are opaque and signed — never construct or modify them. |
| 58 | + |
| 59 | +## Conditional requests (free revalidation) |
| 60 | + |
| 61 | +Metered `GET`s return an `ETag`. Send it back to revalidate for free — a `304` |
| 62 | +costs no tokens, even at a zero balance: |
| 63 | + |
| 64 | +```python |
| 65 | +first = close.block_summary("410390020001010") |
| 66 | +again = close.block_summary("410390020001010", if_none_match = first.etag) |
| 67 | +if again.not_modified: |
| 68 | + ... # your cached copy is still current; nothing was charged |
| 69 | +``` |
| 70 | + |
| 71 | +## Isochrones |
| 72 | + |
| 73 | +```python |
| 74 | +iso = close.isochrone(block = "410390020001010", contours = [15, 30, 45], |
| 75 | + mode = "walk", direction = "to") |
| 76 | +for feature in iso.data["features"]: |
| 77 | + print(feature["properties"]["contour"], feature["properties"]["reachable_blocks"]) |
| 78 | +``` |
| 79 | + |
| 80 | +## Errors |
| 81 | + |
| 82 | +Errors are [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457) `problem+json`, |
| 83 | +mapped to exceptions. Catch a precise class or the `CloseAPIError` base; the stable |
| 84 | +machine key is `err.slug`: |
| 85 | + |
| 86 | +```python |
| 87 | +from closecity import CloseAPIError, TokensExhaustedError, RateLimitedError |
| 88 | + |
| 89 | +try: |
| 90 | + close.block_summary("000000000000000") |
| 91 | +except TokensExhaustedError: |
| 92 | + ... # buy more tokens at account.close.city |
| 93 | +except RateLimitedError as err: |
| 94 | + time.sleep(err.retry_after or 1) |
| 95 | +except CloseAPIError as err: |
| 96 | + print(err.slug, err.status, err.title, err.request_id) |
| 97 | +``` |
| 98 | + |
| 99 | +## Spatial output |
| 100 | + |
| 101 | +With `pip install "closecity[geo]"`, turn any reply into a GeoDataFrame — POIs |
| 102 | +become points, isochrones become polygons, and block replies join census-block |
| 103 | +boundaries: |
| 104 | + |
| 105 | +```python |
| 106 | +pts = close.pois_search(lat = 41.823, lon = -71.412, radius_m = 1500).to_geopandas() |
| 107 | +iso = close.isochrone(block = "440070036001010", minutes = 15).to_geopandas() |
| 108 | +``` |
| 109 | + |
| 110 | +## Reference |
| 111 | + |
| 112 | +- Documentation: https://henryspatialanalysis.github.io/closecity-python/ |
| 113 | +- Interactive API: https://api.close.city/docs |
| 114 | +- Machine-readable contract: https://api.close.city/openapi.json |
| 115 | + |
| 116 | +## Development |
| 117 | + |
| 118 | +```bash |
| 119 | +pip install -e '.[dev,geo]' |
| 120 | +pytest # unit tests (no network — httpx MockTransport) |
| 121 | +ruff check src tests |
| 122 | +``` |
0 commit comments