Docs · Libraries

JavaScript autocomplete

One script tag adds as-you-type address completion to any form — New Zealand (LINZ). No dependencies, no build step. Suggestions are free; resolving the address the user picks costs one credit.

Quick start

Mark up your form, then initialise the widget with a domain token (a browser-restricted API key).

HTML

<label for="address_line_1">Address line 1</label>
<input id="address_line_1" type="text">

<label for="address_line_2">Address line 2</label>
<input id="address_line_2" type="text">

<label for="suburb">Suburb</label>
<input id="suburb" type="text">

<label for="state">State</label>
<input id="state" type="text">

<label for="postcode">Postcode</label>
<input id="postcode" type="text">

JavaScript

<script src="https://cdn.ipsofax.com/scripts/ipsofax-autocomplete-1.2.1.js"></script>

<script>
ipsofax.autocomplete('address_line_1', 'DOMAIN TOKEN',
{
    dataset: 'nz-linz',
    output_fields: {
        address_line_1: 'address_line_1',
        address_line_2: 'address_line_2',
        locality_name: 'suburb',
        postcode: 'postcode'
    }
});
</script>

Try it live on the libraries page.

Dedicated search box

Prefer a search box that completes the lines below it? Bind the widget to that input and choose what the box shows after selection:line1 (default), formatted, or clear.

JavaScript

// A dedicated search box above the address fields
ipsofax.autocomplete('address_search', 'DOMAIN TOKEN', {
    dataset: 'nz-linz',
    search_display: 'formatted',   // show the full resolved label in the box
    output_fields: {
        address_line_1: 'address_line_1',
        address_line_2: 'address_line_2',
        locality_name: 'suburb',
        state: 'state',
        postcode: 'postcode'
    }
});

Postcode lookup

A structured alternative to type-ahead: pass the 4-digit postcode and, optionally, a street and premise (number or building name). You can still use the combined form — 6011MOL 42. Listing is free; resolving the chosen address costs 1 credit.

JavaScript

const { addresses } = await ipsofax.postcodeLookup({
    postcode: '6011',
    street: 'MOL',     // optional
    premise: '42',   // optional
}, 'DOMAIN TOKEN', { dataset: 'nz-linz', limit: 25 });

for (const a of addresses) console.log(a.formatted);   // free

const picked = await ipsofax.resolve(addresses[0].id, 'DOMAIN TOKEN', { dataset: 'nz-linz' });  // 1 credit

Reverse geocoding

nearest() returns the closest addresses to a point, nearest first with distance in metres. One credit when addresses come back; nothing nearby is free.

JavaScript

const { results } = await ipsofax.nearest(
    -41.2866, 174.7756, 'DOMAIN TOKEN', { dataset: 'nz-linz', limit: 5 });

for (const a of results)
    console.log(a.distance_m + ' m', a.formatted);   // 1 credit

Options

OptionDefaultDescription
output_fields—Map of Ipsofax field → your form field (id or name).
baseUrlsame originAPI base, e.g. https://api.ipsofax.com or http://localhost:8080 for the local gateway.
datasetnz-linzDataset code; pass it explicitly for each request.
country—Optional country shorthand; wins over dataset.
limit8Suggestions per request (1–25).
minLength2Characters before searching.
debounceMs150Keystroke settling time.
search_displayline1line1 | formatted | clear — what the bound input shows after a pick.
placeholder—Hint text for the search input.
on_select—function(resolveResponse, suggestion) after a successful resolution.
on_error—function(message) on network/API errors.
autoFocusfalseFocus the bound input on init.

Output fields

Keys on the left are what the widget writes; values are your form field ids or names. Fields a dataset does not have (e.g. state in NZ) are left untouched.

KeyValueNotes
address_line_1number + streete.g. “1 QUEEN STREET”
address_line_2unit / buildingsub_building_name, else building_name
locality_name, localitysuburb / town
dependent_localitysuburb / dependent localitywhen the dataset has one
street, building_number, building_name, sub_building_nameparts
statestate / regionwhen the dataset has one
postcodepostcodefour digits
address_iddataset idLINZ id for New Zealand
formattedfull labelcomma-joined, for inputs
labelstructured labeldataset-correct lines + locality / dependent_locality / state / postcode; no separators
latitude, longitudegeocodewhen the dataset has one

What is charged

  • Typing (/v1/<dataset>/suggest) — free, rate limited.
  • Selecting a concrete address (/v1/<dataset>/resolve) — 1 credit.
  • Selecting a street/locality grouping — free; it just completes the label.

See what is a lookup? for the full model.

Accessibility & devices

The bound input is an ARIA combobox (listbox popup, active descendant). Keyboard: ↓/↑ to move, Enter to select, Esc to close. The dropdown is fixed-positioned so it survives scrollable layouts, and rows are full-width for touch.

Errors

Network or API errors are shown inside the dropdown; wire on_error to log or instrument them. Nothing throws into your page.

Versioning

The filename carries the version (ipsofax-autocomplete-1.2.1.js) so you can pin an exact build. New releases add new files; old URLs keep working.

Attribution

  • NZ results: “NZ Addresses © Toitū Te Whenua LINZ” (CC BY 4.0).