# 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

```python
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`](/docs/sdks/python/endpoints/find-address.md) keyword too, such as `limit`. To narrow results by ZIP or postal code, state, area or address type, see [filter and bias](/docs/sdks/python/filter-and-bias.md).

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

```python
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`.

| Country       | `dataset` | Source                                                               | Differences                                                                                                                  |
| ------------- | --------- | -------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| United States | `usps`    | [USPS ZIP+4](/docs/data/usps.md)                                     | No `native`. The USPS fields, such as `zip_plus_4_code` and `carrier_route_id`, are on the address itself                    |
| Canada        | `cannar`  | [Statistics Canada National Address Register](/docs/data/cannar.md)  | `language` is `en` or `fr`. Each address appears in one language only                                                        |
| Japan         | `upujp`   | [Universal Postal Union address file for Japan](/docs/data/upujp.md) | The API indexes each address once per script, so one address can return three hits: kanji, hiragana and Hepburn romanization |

[Supported countries](/docs/data/supported-countries.md) 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](/docs/sdks/python/errors.md). See [`retrieve_address`](/docs/sdks/python/endpoints/retrieve-address.md) for its parameters.
