Skip to main content

Autocomplete

find_address turns a partial address into suggestions as the user types. retrieve_address turns the chosen suggestion into a full address. Each search runs in one country, set with context.

autocomplete.pypython
from addresszen import AddressZen

with AddressZen("ak_test") as client:
hits = client.find_address(query="123 Main St", context="USA", limit=5).hits
if hits:
address = client.retrieve_address(hits[0].id)
print(address.line_1, address.city, address.state, address.zip_code)
else:
print("No matches")

Find suggestions​

Call find_address with the partial address as query. The result holds hits, a list of suggestions, each with an id and a suggestion to show the user. An empty list means nothing matched. Show each suggestion as it is: the API may change its format, so do not parse it.

Set context to a three-letter ISO 3166-1 code, such as USA, CAN or JPN. Without it, the search uses your account's default country. find_address takes every other find_address keyword too, such as limit. To narrow results by ZIP or postal code, state, area or address type, see filter and bias.

The SDK runs on your server. Have the browser send each keystroke to an endpoint of yours that calls find_address, so your key never leaves the server. The SDK neither debounces nor caches, so wait for a short pause in typing before you call it.

Switch country​

key_availability lists the countries a key can search. It needs only the API key and costs nothing.

countries.pypython
from addresszen import AddressZen

with AddressZen("ak_test") as client:
key = client.key_availability("ak_test")
for country in key.contexts:
print(country.emoji, country.description, country.iso_3)

# Start with the country that matches the request's IP address, if any.
context = key.context or key.contexts[0].iso_3
hits = client.find_address(query="123 Main St", context=context).hits

# When the user picks another country, search again with its code.
hits = client.find_address(query="100 Queen St W, Toronto", context="CAN").hits

Each entry in contexts has iso_3 (the context code), iso_2, description and emoji. context is the licensed country that best matches the IP address the request came from, or an empty string when none does. When the SDK runs on your server, that is your server's address, so prefer the country your user picks.

Retrieve an address​

Pass the hit's id to retrieve_address.

retrieve_address returns every address in one US-shaped format, whatever the country: up to two address lines and US field names such as zip_code, state and city. A field the country's dataset does not carry is an empty string. dataset names the source, and native carries the dataset's raw record for every dataset except usps.

CountrydatasetSourceDifferences
United StatesuspsUSPS ZIP+4No native. The USPS fields, such as zip_plus_4_code and carrier_route_id, are on the address itself
CanadacannarStatistics Canada National Address Registerlanguage is en or fr. Each address appears in one language only
JapanupujpUniversal Postal Union address file for JapanThe API indexes each address once per script, so one address can return three hits: kanji, hiragana and Hepburn romanization

Supported countries lists every country and its context code.

Cost​

Suggestions are free. Each retrieve_address costs one lookup. The API rate limits, then suspends, keys that find suggestions without retrieving any.

Errors​

An HTTP error raises APIError or a subclass, and a failed connection raises TransportError. See error handling. See retrieve_address for its parameters.