# Autocomplete

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

Autocomplete.cscsharp

```csharp
using IdealPostcodes;

using var client = new IdealPostcodesClient("ak_test");

var hits = (await client.FindAddressAsync(new FindAddressParams { Query = "9 Malyons Road", Limit = 5 })).Result.Hits;
if (hits.Count == 0)
{
    Console.WriteLine("No matches");
    return;
}

var address = (await client.ResolveAddressAsync(hits[0].Id)).Result;
Console.WriteLine($"{address.Line1}, {address.PostTown}, {address.Postcode}");
```

## Find suggestions

Call `FindAddressAsync()` 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.

`FindAddressParams` takes every other [`FindAddressAsync()`](/docs/sdks/dotnet/endpoints/find-address.md) parameter: `Limit`, `Context` to search another country, and the filters and biases, such as `PostcodeOutward` or `BiasPostTown`. 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 `FindAddressAsync()`, so your key never leaves the server. Pass the request's cancellation token, so a keystroke the user has typed past stops its call.

## Switch country

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

Countries.cscsharp

```csharp
var key = (await client.KeyAvailabilityAsync("ak_test")).Result;

foreach (var country in key.Contexts)
{
    Console.WriteLine($"{country.Emoji} {country.Description} {country.Iso3}");
}

var context = key.Context.AsString() is { Length: > 0 } current ? current : key.Contexts[0].Iso3;
var hits = (await client.FindAddressAsync(new FindAddressParams { 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 empty when none does. When the SDK runs on your server, that is your server's address, so take the user's choice over it.

## Resolve a suggestion

Pass the hit's `Id` to `ResolveAddressAsync()`. The result is an `Address` with `Line1`, `Line2`, `Line3`, `PostTown`, `Postcode`, `Uprn` and the other standard fields. See [responses](/docs/sdks/dotnet/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 match its type before reading dataset-specific fields.

Native.cscsharp

```csharp
var address = (await client.ResolveAddressAsync("paf_2670849")).Result;

if (address.Native is PafAddress paf)
{
    Console.WriteLine(paf.DeliveryPointSuffix);
}
```

## Cost

Suggestions are free. Each `ResolveAddressAsync()` 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.

## Errors

An HTTP error throws `ApiException`. See [error handling](/docs/sdks/dotnet/errors.md) and [`ResolveAddressAsync()`](/docs/sdks/dotnet/endpoints/resolve-address.md) for the parameters.
