JavaScript library
Address search and postcode filling in any website form. The user types an address, suggestions appear below the field, and on selection the postcode, city and other fields are filled in. A few lines of code are enough.
https://api.postapi.lt/js/postapi.min.jsAbout the library
The library is made for online stores and websites where customers enter a delivery address. It uses the PostAPI.lt postcode API and works with addresses in Lithuania, Latvia and Estonia.
- • Search while typing or search with a button (“Find postcode”).
- • Fills the postcode, city, street, house number, building, apartment and municipality fields.
- • Recognises addresses typed without Lithuanian letters, with typos, with an apartment (Laisvės pr. 44-78).
- • Works with the keyboard and screen readers.
Including the script
Load the library on the page with the address form (e.g. before the closing </body>):
<script src="https://api.postapi.lt/js/postapi.min.js"></script>
You can also download the file and host it with your website's static files – then change the src path.
Quick start
Attach the library to the address field and list the fields to fill when an address is selected:
<input id="address" placeholder="Address">
<input id="postcode" placeholder="Postcode">
<input id="city" placeholder="City">
<script src="https://api.postapi.lt/js/postapi.min.js"></script>
<script>
PostAPI.attach("#address", {
key: "YOUR_API_KEY",
fields: { postcode: "#postcode", city: "#city" }
});
</script>Search while typing
Suggestions appear while typing and can be selected with the mouse or keyboard (↑ ↓ and Enter). On selection the listed fields are filled and the onSelect function is called.
PostAPI.attach("#address", {
key: "YOUR_API_KEY",
fields: {
postcode: "#postcode",
city: "#city",
apartment: "#flat"
},
onSelect: function (item) {
console.log(item.postcode_full, item.city);
}
});When suggestions while typing are not needed: the user types the whole address and presses a button. If one address is found the fields are filled immediately, if several – a list is shown, if none – a message.
<input id="address" placeholder="Address">
<button id="find" type="button">Find postcode</button>
<input id="postcode">
<script>
PostAPI.lookup("#address", {
key: "YOUR_API_KEY",
trigger: "#find",
fields: { postcode: "#postcode" }
});
</script>Shipping and billing addresses
If the form has several address blocks (e.g. shipping and billing), attach the library to each address field separately. Each one works on its own and fills only the fields of its block.
PostAPI.attach("#shipping_address", {
key: "YOUR_API_KEY",
fields: { postcode: "#shipping_postcode", city: "#shipping_city" }
});
PostAPI.attach("#invoice_address", {
key: "YOUR_API_KEY",
fields: { postcode: "#invoice_postcode", city: "#invoice_city" }
});Which fields to fill
In the fields option give a field selector for each value. After filling, input and change events are fired, so PrestaShop, WooCommerce, React or Vue forms notice the change.
postcodePostcode (format – postcodeFormat).
cityCity or settlement.
streetStreet.
house_numberHouse number.
number_onlyHouse number without the building part.
housingBuilding (korpusas).
apartmentApartment, if typed in the address (Laisvės pr. 44-78).
municipalityMunicipality.
countryCountry. For a <select> field the matching option is selected.
Address in several fields
If street, house number and city are separate fields, their values are added to the search:
PostAPI.attach("#street", {
key: "YOUR_API_KEY",
sources: ["#house", "#city"],
fields: { postcode: "#postcode" }
});Transforming the value
A field can be a function or have a transform:
fields: {
postcode: {
target: "#zip",
transform: function (value) { return value.replace("LT-", ""); }
},
city: function (value, item) { myCart.city = value; }
}Options
keyAPI key. Visible in the page source – a token is safer (see “Key and token”).
tokenUrlURL on your server that returns a temporary token instead of the key. Refreshed automatically.
countryCountry: LT, LV or EE. Can be changed later: instance.setCountry('LV').
languageLanguage of the messages: lt or en.
limitNumber of suggestions in the list (1–20).
minLengthMinimum number of characters before searching.
debounceMilliseconds to wait after the last keystroke before sending a request.
fieldsWhich form fields to fill when an address is selected (see “Form fields”).
sourcesAdditional fields whose values are added to the search (e.g. house number, city).
postcodeFormatPostcode format: full – LT-01103, digits – 01103.
fillInputWhat goes into the address field: address – “Gedimino pr. 9”, address_city – “Gedimino pr. 9, Vilnius”, none or a function.
wideNumber1 – house number as a prefix (9 → 9, 9A, 9K1), 0 – exact number only.
showStatusShow the status in the list: “Searching…”, “Address not found”, an error.
noResultsTextText when nothing is found, e.g. “Address not found”.
timeoutRequest timeout in milliseconds.
attributionTargetWhere to show the free plan link (default – right below the address field).
injectStylesfalse – you style the list yourself.
styleNonceCSP nonce for the injected styles.
Events and methods
onSelectCalled when an address is selected. item – the selected record with postcode, city, municipality, coordinates.
onErrorCalled on an error. error.code – API error code (1000–1006) or TIMEOUT, NETWORK, HTTP.
onStateChangeState change: idle, loading, results, empty, error, selected.
postapi:selectDOM event on the address field when an address is selected (useful when fields are added dynamically).
var instance = PostAPI.attach("#address", { ... });
instance.setCountry("LV"); // change the country
instance.destroy(); // detach from the field
PostAPI.get("#address"); // instance by field
PostAPI.noConflict(); // if the site already has another PostAPIKey and token
An API key written in the page is visible in its source. A temporary token is safer: your server creates it and passes it to the library, while the key stays on the server. The token is valid for 180 minutes and the library refreshes it automatically.
PostAPI.attach("#address", {
tokenUrl: "/postapi-token.php",
fields: { postcode: "#postcode" }
});openssl_public_encrypt(POSTAPI_KEY . ";" . time(), $encrypted, POSTAPI_PUBLIC_KEY, OPENSSL_PKCS1_OAEP_PADDING);
header("Content-Type: application/json");
echo json_encode(["token" => base64_encode($encrypted)]);Free plan
With a free API key, a link to PostAPI.lt is required on the website. The library shows it automatically below the address field, so nothing else needs to be done:
The link text is in the language of the country being searched (the country option): “Pasta indeksu integrācija” in Latvia, “Postiindeksite integratsioon” in Estonia. After a country change (setCountry) the text is updated immediately.
On a paid plan the link is not shown. If the field shares a row with other fields, the link position can be changed with the attributionTarget option.
Appearance
Adjust the colours and font of the suggestion list to your website with CSS variables:
.postapi-ac {
--postapi-active: #fff3e0; /* highlighted suggestion */
--postapi-border: #e0e0e0;
--postapi-radius: 4px;
--postapi-font: 15px/1.4 Arial, sans-serif;
}Compatibility
- • Browsers: all modern browsers, as well as Internet Explorer 11, iOS Safari 9+, Android 4.4+ – without additional libraries.
- • No dependencies: no jQuery or other libraries needed, size – 9 KB (gzip).
- • No conflicts: a single global name PostAPI, all styles prefixed with postapi-ac – the library does not change your website's look, and your styles do not break the suggestion list.
- • Accessibility: keyboard control, ARIA attributes, the number of results is announced to screen readers.