# Autocomplete

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

autocomplete.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	ctx := context.Background()
	client := idealpostcodes.NewClient("ak_test")

	find, err := client.FindAddress(ctx, &idealpostcodes.FindAddressParams{Query: "9 Malyons Road", Limit: 5})
	if err != nil {
		log.Fatal(err)
	}
	if len(find.Result.Hits) == 0 {
		log.Fatal("no matches")
	}
	hit := find.Result.Hits[0].AsUkAddressSuggestion()
	if hit == nil {
		log.Fatal("not a UK address")
	}

	res, err := client.ResolveAddress(ctx, hit.ID, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s, %s, %s\n", res.Result.Line1, res.Result.PostTown, res.Result.Postcode)
}
```

## Find suggestions

Call `FindAddress()` with the partial address as `Query`. The response holds `Result.Hits`, one per suggestion. An empty list means nothing matched.

A hit is one of two types. `AsUkAddressSuggestion()` returns a UK suggestion, with a `UDPRN`. `AsAddressSuggestion()` returns a suggestion outside the UK. Each has an `ID` to resolve and a `Suggestion` to show, and the accessor for the other type returns nil.

`FindAddressParams` takes every other [`FindAddress()`](/docs/sdks/go/endpoints/find-address.md) parameter: `Limit`, `Context` to search another country, and the filters and biases. 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.

## Switch country

`KeyAvailability()` lists the countries a key can search. It needs no credentials and costs nothing.

countries.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	ctx := context.Background()
	client := idealpostcodes.NewClient("ak_test")

	key, err := client.KeyAvailability(ctx, "ak_test")
	if err != nil {
		log.Fatal(err)
	}
	for _, country := range key.Result.Contexts {
		fmt.Println(country.Emoji, country.Description, country.ISO3)
	}

	iso3 := key.Result.Context.String()
	if iso3 == "" && len(key.Result.Contexts) > 0 {
		iso3 = key.Result.Contexts[0].ISO3
	}
	find, err := client.FindAddress(ctx, &idealpostcodes.FindAddressParams{Query: "1 Main Street", Context: iso3})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(len(find.Result.Hits), "suggestions")
}
```

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 `ResolveAddress()`. The result is an `Address` with `Line1`, `Line2`, `Line3`, `PostTown`, `Postcode`, `UPRN` and the other standard fields. See [responses](/docs/sdks/go/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 nil otherwise, so check it before reading dataset-specific fields.

native.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.ResolveAddress(context.Background(), "paf_2670849", nil)
	if err != nil {
		log.Fatal(err)
	}
	if native := res.Result.Native; native != nil {
		if paf := native.AsPafAddress(); paf != nil {
			fmt.Println(paf.DeliveryPointSuffix)
		}
	}
}
```

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

## Errors

An HTTP error returns `*APIError`. A failed connection returns the `net/http` error, wrapped. See [error handling](/docs/sdks/go/errors.md) and [`ResolveAddress()`](/docs/sdks/go/endpoints/resolve-address.md) for the parameters.
