Metadata-Version: 2.5
Name: mapricot
Version: 0.15.0
Summary: Official client for the Mapricot API: geocoding, places, routing, and IP geolocation.
Project-URL: Homepage, https://mapricot.com/
Project-URL: Documentation, https://mapricot.com/docs/python/
Project-URL: Source, https://github.com/mapricothq/mapricot-python
Project-URL: Issues, https://github.com/mapricothq/mapricot-python/issues
License-Expression: MIT
License-File: LICENSE
Keywords: geocoding,ip-geolocation,maps,places,routing
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# Mapricot for Python

The official Python client for the [Mapricot API](https://mapricot.com/): address and place search, reverse geocoding, nearby places, routing, and IP geolocation, with 250,000 free requests every 30 days.

```sh
pip install mapricot
```

It needs Python 3.11 or later and has no dependencies.

## Quick start

Create a server key in the [dashboard](https://app.mapricot.com/) and pass it to the client. Keep it out of your source code, for example by reading it from an environment variable as here.

```python
import os

from mapricot import Mapricot

client = Mapricot(os.environ["MAPRICOT_KEY"])

data = client.geocoding.search("128 SW 3rd Ave, Portland", limit=1)
place = data["results"][0]
print(place["label"], place["lat"], place["lon"])
```

## Methods

Methods are grouped by product, the same way as the API's paths. Each returns the API's JSON as a typed dict, with the same field names as the [API reference](https://mapricot.com/docs/api-reference/).

| Method | Endpoint |
| --- | --- |
| `client.geocoding.search(q, address=, locality=, county=, region=, postal_code=, focus=, country=, limit=, lang=)` | [`GET /geocoding/v1/search`](https://mapricot.com/docs/geocoding/#find-an-address) |
| `client.geocoding.search_batch(queries)` | [`POST /geocoding/v1/search/batch`](https://mapricot.com/docs/geocoding/#search-a-list-of-addresses) |
| `client.geocoding.autocomplete(q, focus=, country=, limit=, lang=)` | [`GET /geocoding/v1/autocomplete`](https://mapricot.com/docs/geocoding/#suggest-as-people-type) |
| `client.geocoding.reverse(lat, lon, limit=, lang=)` | [`GET /geocoding/v1/reverse`](https://mapricot.com/docs/geocoding/#find-the-address-at-a-point) |
| `client.places.nearby(lat, lon, category=, radius=, limit=, lang=)` | [`GET /places/v1/nearby`](https://mapricot.com/docs/places/#list-places-around-a-point) |
| `client.places.get(id, lang=)` | [`GET /places/v1/{id}`](https://mapricot.com/docs/places/#look-up-a-saved-place) |
| `client.routing.route(waypoints, mode=, geometries=, steps=, lang=, costing=)` | [`POST /routing/v1/route`](https://mapricot.com/docs/routing/#get-a-route) |
| `client.routing.optimize(waypoints, mode=, geometries=, steps=, lang=, costing=)` | [`POST /routing/v1/optimize`](https://mapricot.com/docs/routing/#optimize-the-order-of-stops) |
| `client.routing.isochrone(location, minutes, mode=, costing=, generalize=)` | [`POST /routing/v1/isochrone`](https://mapricot.com/docs/routing/#get-an-isochrone) |
| `client.ip_geolocation.lookup(ip)` | [`GET /ip-geolocation/v1/{ip}`](https://mapricot.com/docs/ip-geolocation/#look-up-an-address) |
| `client.account()` | `GET /account`, which checks a key and is never billed |

`focus` and each waypoint are `{"lat": …, "lon": …}`. `country` and `category` take a list or a comma-separated string.

```python
nearby = client.places.nearby(45.52, -122.68, category=["food:coffee_shop"], radius=1000)
route = client.routing.route([{"lat": 45.5189, "lon": -122.6793}, {"lat": 45.5214, "lon": -122.6828}], mode="walk")
print(route["routes"][0]["durationSeconds"])
```

When you already have an address in parts, such as a spreadsheet's columns, pass them in place of `q`. Search ignores a blank part:

```python
data = client.geocoding.search(address="10 Downing Street", locality="London", postal_code="SW1A 2AA", country="GB")
```

`search_batch` runs up to 100 searches in one request. Each query is a dict of `search`'s parameters, with `q` or the address parts named as in the API, such as `postalCode`, and `responses` answers them in the same order, each with `results` and `attribution`, or an `error` with the `code` and `message` that search would get, without raising. Each completed search is billed as one request; errors aren't.

```python
batch = client.geocoding.search_batch([{"q": "128 SW 3rd Ave, Portland", "limit": 1}, {"q": "10 Downing Street, London", "limit": 1}])
for response in batch["responses"]:
    print(response["error"]["message"] if "error" in response else response["results"][0]["label"])
```

A route's path comes as an encoded `polyline` with five decimal places, which polyline decoders assume. Pass `geometries="geojson"` to get it as a GeoJSON `LineString` in `geometry` instead, which maps draw without decoding, or `geometries="polyline6"` for six decimal places.

Pass `steps=True` and each leg's `steps` has its turn-by-turn directions: an `instruction`, its distance and time, and its `lat` and `lon`. They're in English unless you pass `lang`, such as `lang="fr"`.

`routing.optimize` takes the same arguments as `routing.route` and returns the route that visits the waypoints in a fast order, keeping the first and last in place. It adds `waypointOrder`: the indexes of your waypoints in visiting order, which `legs` follow. Every pair of waypoints must be within 400 km of each other for `drive`, or 200 km for `walk` and `bicycle`.

`costing` changes how a route, optimized route, or isochrone picks its roads, with Valhalla's [costing options](https://mapricot.com/docs/routing/#costing-options) for the mode, such as `costing={"useTolls": 0, "useHighways": 0}` to drive avoiding tolls and highways where it can.

An isochrone is a GeoJSON `FeatureCollection` with one polygon per time, largest first, so a map that draws them in order shows the smaller ones on top:

```python
area = client.routing.isochrone({"lat": 45.5189, "lon": -122.6793}, [5, 10], mode="walk")
print([feature["properties"]["minutes"] for feature in area["features"]])
```

`generalize` smooths each outline to within that many meters, from 10 to 5000, so it has fewer points. The API picks it from the travel time when you leave it out. A long drive's outlines are detailed and large, and `generalize=300` makes them much smaller while keeping their shape at city scale.

If your app shows results, show the credits in each response's `attribution`.

## Errors and retries

An error response raises `MapricotError`, with the HTTP `status`, the API's error `code`, a `message`, and the `request_id`:

```python
from mapricot import MapricotError

try:
    client.geocoding.search("")
except MapricotError as error:
    print(error.status, error.code, error.message)  # 400 invalid_request The service rejected the request.
```

The client retries `503` responses and `429 concurrency_exceeded`, up to twice, waiting longer each time. Errors aren't billed. Set `max_retries=` and `timeout=` (seconds) on `Mapricot(...)` to change this. The [API reference](https://mapricot.com/docs/api-reference/#errors) lists every status.

## Links

- [Mapricot](https://mapricot.com/)
- [Python docs](https://mapricot.com/docs/python/)
- [API reference](https://mapricot.com/docs/api-reference/)
- [Pricing](https://mapricot.com/pricing/)
