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

JavaScript
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

JavaScript
new Findort(key, options)
new Findort(options)
OptionTypeDefaultDescription
keystringAPI key, or pass it as the first argument
baseUrlstringhttps://api.findort.chAPI address
langstringLanguage for all requests: de, fr, it, en
countrystring or string[]Country filter for all searches: 'ch' or ['ch', 'li']
timeoutnumber8000Time limit per request in milliseconds
fetchfunctionglobalThis.fetchYour own fetch function, for example in tests
JavaScript
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.

OptionTypeDescription
limitnumber1 to 20, default 5
langstringoverrides the client's language
countrystring 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 stringstrictly limit to a rectangle
typesstring[] or stringonly these types
signalAbortSignalcancel the request

reverse

JavaScript
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.

JavaScript
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.

JavaScript
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.

JavaScript
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:

TypeScript
import type { Place, PlaceType } from 'findort';

function show(place: Place) {
  return `${place.label} (${place.type})`;
}