Autocomplete: a search box that completes
One call serves a search box: GET /v1/autocomplete 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
{
"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}, 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
displayNameas it is. It tells look-alikes apart: there are several Washington Townships in Pennsylvania, andWashington Township, Berks County, PAnames one. - A listing found by its MLS number may have a hidden address. It then comes with
address: nullandaddressDisplay: "withheld": show the MLS number, and no address. A hidden address never matches inaddresses. - 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 (andschoolDistricts) keep working during a data stop;addressesandmlsNumbersare listing content, so a request that asks for them, or leavestypesout, answers500 data_staleas a whole while the stop is on. schoolDistrictsis Premium. Withtypesleft out, a key on a Basic account simply gets the other four groups; asking for the group by name on Basic is403 tier_insufficient.
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:
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, 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_limitedcarriesRetry-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.