# Autocomplete: a search box that completes One call serves a search box: [`GET /v1/autocomplete`](../reference/endpoints/autocomplete.md) answers what the visitor may mean, in groups, from the second character on. This guide shows what to send, and what to do with each kind of suggestion. ``` GET /v1/autocomplete?q=ardm ``` ```json { "data": { "places": [ { "placeId": "pl_…", "kind": "cdp", "displayName": "Ardmore (CDP), Montgomery County, PA", "state": "PA", "center": { "lat": 40.01, "lng": -75.29 } } ], "postalCodes": [], "addresses": [ { "listingKey": "805454359664", "mlsNumber": "PAMC2141234", "status": "active", "address": { "line1": "12 ARDMORE AVE", "unit": null, "city": "Ardmore", "state": "PA", "postalCode": "19003" } } ], "mlsNumbers": [] }, "meta": { "requestId": "req_…", "environment": "test" } } ``` Each group holds at most ten suggestions. Show them under headings, in the order you like. ## What to do with a suggestion | Group | The visitor chose | Then | | --- | --- | --- | | `places` | a town, a county, a township, a community, a subdivision | Search it by its ID: `GET /v1/listings?location.placeIds=pl_…`. Move your map to the place with [`GET /v1/places/{placeId}`](../reference/endpoints/get-place.md), which gives its `bbox` | | `postalCodes` | a ZIP code | Search it by `location.placeIds` with its `placeId`, or by `location.postalCodes=19003` | | `addresses` | one listing, by its address | Go to that listing's page, by its `listingKey` | | `mlsNumbers` | one listing, by a current or past MLS number | Go to that listing's page, by its `listingKey` | | `schoolDistricts` (Premium) | a school district | Search it by `location.placeIds` with its `placeId` | - **Show `displayName` as it is.** It tells look-alikes apart: there are several Washington Townships in Pennsylvania, and `Washington Township, Berks County, PA` names one. - **A listing found by its MLS number may have a hidden address.** It then comes with `address: null` and `addressDisplay: "withheld"`: show the MLS number, and no address. A hidden address never matches in `addresses`. - **Keep the place's ID, not its name,** in your page's address and in a saved search. A name can change; the ID stays. A place ID is the same in the sandbox and on live. ## Ask for the groups you show `types` names the groups you want, and leaves the rest out of the answer: ``` GET /v1/autocomplete?q=1908&types=postalCodes,places GET /v1/autocomplete?q=PAMC21&types=mlsNumbers ``` - A box that searches areas only needs `types=places,postalCodes`. Those two (and `schoolDistricts`) keep working during a [data stop](../errors/data_stale.md); `addresses` and `mlsNumbers` are listing content, so a request that asks for them, or leaves `types` out, answers `500 data_stale` as a whole while the stop is on. - `schoolDistricts` is Premium. With `types` left out, a key on a Basic account simply gets the other four groups; asking for the group by name on Basic is [`403 tier_insufficient`](../errors/tier_insufficient.md). ## Nearest first `near=lng,lat` orders each group by distance from a point: the center of the map on screen, or the visitor's own position if they shared it. ``` GET /v1/autocomplete?q=spring&near=-75.30,40.02 ``` ## In the browser A search box runs in the browser, so call with your **publishable** key from one of your account's website addresses: ```js const box = document.querySelector('#search'); let timer = null; let running = null; box.addEventListener('input', () => { clearTimeout(timer); const q = box.value.trim(); if (q.length < 2) return showSuggestions(null); // nothing is searched under two characters timer = setTimeout(async () => { running?.abort(); // an older answer must not overwrite a newer one running = new AbortController(); try { const r = await fetch(`https://api.apidata.one/v1/autocomplete?${new URLSearchParams({ q })}`, { headers: { Authorization: 'Bearer pk_test_…' }, signal: running.signal, }); if (r.ok) showSuggestions((await r.json()).data); } catch { // aborted, or offline: leave what is shown } }, 200); }); ``` - **Wait for a pause in typing** (200 ms here) and **abort the call you no longer need.** A call for every key press uses up your key's [limit](../concepts/rate-limits.md), which your visitors share by address. - What the visitor typed is taken as it is: `%` and `_` are ordinary characters, and matching ignores upper and lower case. - A [`429 rate_limited`](../errors/rate_limited.md) carries `Retry-After`. For a search box it is enough to skip that answer: the next key press asks again. ## What it does not search Autocomplete finds places, ZIP codes and listings. It is not a geocoder: an address that is not a listing's finds nothing. Neighborhoods and individual schools are not places yet.