JavaScript
A client in a single file with no dependencies. Runs in browsers, Node 18 and later, Deno and Bun, with TypeScript types.
Install
npm i findort
import { Findort } from 'findort';
const findort = new Findort('pk_…', { lang: 'en', country: 'ch' });
const places = await findort.search('Zytglogge');In the browser use a public key pk_…, on servers a secret sk_…. The package also contains the Web Component as findort/search.
Creating a client
new Findort(key, options)
new Findort(options)| Option | Type | Default | Description |
|---|---|---|---|
key | string | API key, or pass it as the first argument | |
baseUrl | string | https://api.findort.ch | API address |
lang | string | Language for all requests: de, fr, it, en | |
country | string or string[] | Country filter for all searches: 'ch' or ['ch', 'li'] | |
timeout | number | 8000 | Time limit per request in milliseconds |
fetch | function | globalThis.fetch | Your own fetch function, for example in tests |
search
const places = await findort.search('Bahnhofstr 1 Zür', {
limit: 5,
types: ['address', 'poi'],
near: { lat: 47.3769, lon: 8.5417 },
});Returns a list of Place.
| Option | Type | Description |
|---|---|---|
limit | number | 1 to 20, default 5 |
lang | string | overrides the client's language |
country | string or string[] | overrides the client's country filter |
near | { lat, lon }, [lat, lon] or 'lat,lon' | prefer nearby results |
bbox | [minLon, minLat, maxLon, maxLat] or string | strictly limit to a rectangle |
types | string[] or string | only these types |
signal | AbortSignal | cancel the request |
reverse
const [place] = await findort.reverse(46.9465, 7.4443, { limit: 1, types: 'address' });Options: limit, lang, radius (kilometres, 0.01 to 50), types, signal. See reverse.
geocode
The best match or null. Handy for turning a stored address into coordinates.
const place = await findort.geocode('Bundesplatz 3, Bern');
if (place) console.log(place.location.lat, place.location.lon);geocode takes the same options as search, except limit.
autocomplete
For a search field of your own: autocomplete() waits after the last keystroke (150 ms by default) and cancels older requests. Only the answer to the latest input arrives, even on a flaky network.
import { Findort, autocomplete } from 'findort';
const findort = new Findort('pk_…', { country: 'ch' });
const suggest = autocomplete(findort, 150);
input.addEventListener('input', async () => {
const places = await suggest(input.value, { limit: 6 });
render(places); // older calls resolve with [] instead of stale results
});Errors
When the API returns an error, the client throws FindortError with status (HTTP), code and message. All codes are listed under errors.
import { FindortError } from 'findort';
try {
await findort.search('');
} catch (e) {
if (e instanceof FindortError) console.log(e.status, e.code); // 400 invalid_request
}Network errors and timeouts throw the error from fetch, not FindortError.
TypeScript
Types ship with the package. You can import Place, PlaceType, Address, SearchOptions and ReverseOptions:
import type { Place, PlaceType } from 'findort';
function show(place: Place) {
return `${place.label} (${place.type})`;
}