Integrations · Installation guide

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.

v1.3.0https://api.postapi.lt/js/postapi.min.js
9 KB
gzip, no dependencies
IE11+
and older phones
LT · LV · EE
addresses
Getting started

About 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.
Installation

Including the script

Load the library on the page with the address form (e.g. before the closing </body>):

HTML
<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.

Getting started

Quick start

Attach the library to the address field and list the fields to fill when an address is selected:

HTML + JavaScript
<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>
JavaScript

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.

JavaScript
PostAPI.attach("#address", {
  key: "YOUR_API_KEY",
  fields: {
    postcode: "#postcode",
    city: "#city",
    apartment: "#flat"
  },
  onSelect: function (item) {
    console.log(item.postcode_full, item.city);
  }
});
JavaScript

Search with a button

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.

HTML + JavaScript
<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>
Several fields

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.

JavaScript
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" }
});
If the form is reloaded (e.g. with AJAX) and the field is attached again, the previous attachment is removed automatically – the suggestion list is not duplicated.
Form fields

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.

postcode
LT-01103

Postcode (format – postcodeFormat).

city
Vilnius

City or settlement.

street
Gedimino pr.

Street.

house_number
5K2

House number.

number_only
5

House number without the building part.

housing
2

Building (korpusas).

apartment
78

Apartment, if typed in the address (Laisvės pr. 44-78).

municipality
Vilniaus m. sav.

Municipality.

country
LT

Country. 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:

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

JavaScript
fields: {
  postcode: {
    target: "#zip",
    transform: function (value) { return value.replace("LT-", ""); }
  },
  city: function (value, item) { myCart.city = value; }
}
Settings

Options

key
–

API key. Visible in the page source – a token is safer (see “Key and token”).

tokenUrl
–

URL on your server that returns a temporary token instead of the key. Refreshed automatically.

country
LT

Country: LT, LV or EE. Can be changed later: instance.setCountry('LV').

language
lt

Language of the messages: lt or en.

limit
10

Number of suggestions in the list (1–20).

minLength
2

Minimum number of characters before searching.

debounce
200

Milliseconds to wait after the last keystroke before sending a request.

fields
{}

Which form fields to fill when an address is selected (see “Form fields”).

sources
[]

Additional fields whose values are added to the search (e.g. house number, city).

postcodeFormat
full

Postcode format: full – LT-01103, digits – 01103.

fillInput
address

What goes into the address field: address – “Gedimino pr. 9”, address_city – “Gedimino pr. 9, Vilnius”, none or a function.

wideNumber
null

1 – house number as a prefix (9 → 9, 9A, 9K1), 0 – exact number only.

showStatus
false

Show the status in the list: “Searching…”, “Address not found”, an error.

noResultsText
null

Text when nothing is found, e.g. “Address not found”.

timeout
10000

Request timeout in milliseconds.

attributionTarget
–

Where to show the free plan link (default – right below the address field).

injectStyles
true

false – you style the list yourself.

styleNonce
–

CSP nonce for the injected styles.

For developers

Events and methods

onSelect
item, instance

Called when an address is selected. item – the selected record with postcode, city, municipality, coordinates.

onError
error, instance

Called on an error. error.code – API error code (1000–1006) or TIMEOUT, NETWORK, HTTP.

onStateChange
state, instance

State change: idle, loading, results, empty, error, selected.

postapi:select
event.detail

DOM event on the address field when an address is selected (useful when fields are added dynamically).

Methods
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 PostAPI
Security

Key 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.

JavaScript
PostAPI.attach("#address", {
  tokenUrl: "/postapi-token.php",
  fields: { postcode: "#postcode" }
});
PHP – /postapi-token.php
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)]);
Public key for tokens – postapi-public.pem.
License

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:

Gedimino pr. 9
Pašto kodų integracija - PostAPI.lt

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.

Design

Appearance

Adjust the colours and font of the suggestion list to your website with CSS variables:

CSS
.postapi-ac {
  --postapi-active: #fff3e0;     /* highlighted suggestion */
  --postapi-border: #e0e0e0;
  --postapi-radius: 4px;
  --postapi-font: 15px/1.4 Arial, sans-serif;
}
Variables: --postapi-background, --postapi-border, --postapi-radius, --postapi-shadow, --postapi-color, --postapi-muted, --postapi-active, --postapi-link, --postapi-font.
Technical details

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.
API documentation