# JavaScript

> A client in a single file with no dependencies. Runs in browsers, Node 18 and later, Deno and Bun, with TypeScript types.

Source: https://findort.ch/en/docs/javascript/

## Install

<p class="install"><span class="sign">npm i findort</span></p>

```js

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](https://findort.ch/en/docs/web-component/) as `findort/search`.

## Creating a client

```js
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

```js
  limit: 5,
  types: ['address', 'poi'],
  near: { lat: 47.3769, lon: 8.5417 },
});
```

Returns a list of [Place](https://findort.ch/en/docs/response/#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](https://findort.ch/en/docs/response/#types) |
| `signal` | AbortSignal | cancel the request |

## reverse

```js
```

Options: `limit`, `lang`, `radius` (kilometres, 0.01 to 50), `types`, `signal`. See [reverse](https://findort.ch/en/docs/reverse/).

## geocode

The best match or `null`. Handy for turning a stored address into coordinates.

```js
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.

```js

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](https://findort.ch/en/docs/errors/).

```js

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

```ts

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