Python

A client for Python 3.9 and later that only needs the standard library. One module, no dependencies.

Install

pip install findort

Python
import os
from findort import Findort

findort = Findort(os.environ["FINDORT_KEY"], lang="en")
place = findort.geocode("Bundesplatz 3, Bern", country="ch")

if place:
    print(place.label, place.location.lat, place.location.lon)

On servers use a secret key sk_….

Creating a client

Python
Findort(key=None, *, base_url="https://api.findort.ch", lang=None, country=None, timeout=8.0, transport=None)
ArgumentTypeDefaultDescription
keystrAPI key
base_urlstrhttps://api.findort.chAPI address
langstrLanguage for all requests: de, fr, it, en
countrystr or listCountry filter for all searches: "ch" or ["ch", "li"]
timeoutfloat8.0Time limit in seconds
transportcallableYour own HTTP function for tests: (url, headers) -> (status, body_bytes)
Python
places = findort.search(
    "Bahnhofstr 1 Zür",
    limit=5,
    types=["address", "poi"],
    near=(47.3769, 8.5417),
)
for place in places:
    print(place.label)
ArgumentTypeDescription
limitint1 to 20, default 5
langstrlanguage for this request only
countrystr or listcountry filter for this request only
near(lat, lon)prefer nearby results
bbox(minLon, minLat, maxLon, maxLat)strictly limit to a rectangle
typesstr or listonly these types

reverse

Python
places = findort.reverse(46.9465, 7.4443, limit=1, types="address")
print(places[0].label if places else "nothing found")

Arguments: limit, lang, radius (kilometres, 0.01 to 50), types.

geocode

The best match as a Place, or None. Takes the same arguments as search.

Python
place = findort.geocode("Zytglogge, Bern")

Place

Place is an immutable dataclass.

FieldType
id, label, name, type, categorystr
addressdict with the address fields
locationLocation with lat and lon
bboxtuple as (minLon, minLat, maxLon, maxLat) or None

Plus the shortcuts street, housenumber, postcode, city (city or locality) and country_code. They are None when the value is missing.

Location.distance_to() returns the straight-line distance in kilometres:

Python
bern = findort.geocode("Bahnhof Bern")
thun = findort.geocode("Bahnhof Thun")
print(f"{bern.location.distance_to(thun.location):.1f} km")

Errors

On an error response the client raises FindortError with status (HTTP) and code. If the server is unreachable, code is network_error and status is 0.

Python
import time
from findort import FindortError

try:
    findort.search("Bern")
except FindortError as e:
    if e.code == "rate_limited":
        time.sleep(2)

All codes are listed under errors.