# Autocomplete

`findAddress()` turns a partial address into suggestions as the user types. `resolveAddress()` turns the chosen suggestion into a full address.

autocomplete.phpphp

```php
use IdealPostcodes\IdealPostcodes;

$client = IdealPostcodes::client('ak_test');

$hits = $client->findAddress(['query' => '9 Malyons Road', 'limit' => 5])->result->hits;
if ($hits === []) {
    exit('No matches');
}

$address = $client->resolveAddress($hits[0]->id)->result;
echo $address->line1, ', ', $address->postTown, ', ', $address->postcode, PHP_EOL;
```

## How do I find suggestions?

Call `findAddress()` with the partial address as `query`. The response holds `result->hits`, a list of suggestions with an `id` and a `suggestion` to show. An empty list means nothing matched.

The same array takes any other [`findAddress()`](/docs/sdks/php/endpoints/find-address.md) parameter: `limit`, `context` to search another country, and the filters and biases, such as `postcode_outward` or `bias_post_town`. See the [API reference](/docs/api/find-address.md) for each one.

The SDK runs on your server. Have the browser send each keystroke to an endpoint of yours that calls `findAddress()`, so your key never leaves the server.

## How do I let users switch country?

`keyAvailability()` lists the countries a key can search. It needs only the API key and costs nothing.

countries.phpphp

```php
use IdealPostcodes\IdealPostcodes;

$client = IdealPostcodes::client('ak_test');
$key = $client->keyAvailability('ak_test')->result;

foreach ($key->contexts as $country) {
    echo $country->emoji, ' ', $country->description, ' ', $country->iso3, PHP_EOL;
}

$context = $key->context !== '' ? $key->context : $key->contexts[0]->iso3;
$hits = $client->findAddress(['query' => '1 Main Street', 'context' => $context])->result->hits;
```

Each entry in `contexts` has `iso3` (the `context` code), `iso2`, `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 take the user's choice over it.

## How do I get the full address for a suggestion?

Pass the hit's `id` to `resolveAddress()`. The result is an `Address` with `line1`, `line2`, `line3`, `postTown`, `postcode`, `uprn` and the other standard fields. See [responses](/docs/sdks/php/responses.md) for how to read it.

`native` holds the raw record from the address's source dataset, when the API returns one. It is `null` otherwise, so narrow it before reading dataset-specific fields.

native.phpphp

```php
use IdealPostcodes\Types\PafAddress;

$address = $client->resolveAddress('paf_2670849')->result;

if ($address->native instanceof PafAddress) {
    echo $address->native->deliveryPointSuffix, PHP_EOL;
}
```

## What does autocomplete cost?

Suggestions are free. Each `resolveAddress()` costs one lookup. The API rate limits, then suspends, integrations that request suggestions without resolving any. The default limit is 3,000 suggestion requests per 5 minutes for each key and IP address.

## How are errors reported?

An HTTP error throws `ApiException`. A failed connection throws `TransporterException`. See [error handling](/docs/sdks/php/errors.md). See [`resolve`](/docs/sdks/php/endpoints/resolve-address.md) for the parameters.
