# Autocomplete

`FindAddress()` turns a partial address into suggestions as the user types. `RetrieveAddress()` turns the chosen suggestion into a full address. Each search runs in one country, set with `Context`.

autocomplete.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

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

	find, err := client.FindAddress(ctx, &addresszen.FindAddressParams{Query: "1010 Cauthen Ln", Limit: 5})
	if err != nil {
		log.Fatal(err)
	}
	if len(find.Result.Hits) == 0 {
		log.Fatal("no matches")
	}

	res, err := client.RetrieveAddress(ctx, find.Result.Hits[0].ID, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s, %s\n", res.Result.Line1, res.Result.LastLine)
}
```

## Find suggestions

Call `FindAddress()` with the partial address as `Query`. The response holds `Result.Hits`, a slice of `AddressSuggestion` with an `ID` and a `Suggestion` to show. An empty slice means nothing matched. The suggestion format may change, so show it as it arrives rather than parsing it.

`FindAddressParams` takes every other [`FindAddress()`](/docs/sdks/go/endpoints/find-address.md) parameter: `Limit`, `Context` and the filters and biases, such as `StateCode`, `PostalCode` or `BiasLonlat`. Filters restrict results and biases promote them. Several terms for one filter go comma separated, such as `StateCode: "NM,TX"`. To favor addresses near the caller's IP address, set `BiasIP` to `"true"`.

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

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. `KeyAvailability()` lists the countries a key can search. It needs no credentials and costs nothing.

countries.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

func main() {
	ctx := context.Background()
	client := addresszen.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, &addresszen.FindAddressParams{Query: "1010 Cauthen Ln", 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.

## Retrieve the full address

Pass the hit's `ID` to `RetrieveAddress()`. The result is an `Address` in one US-shaped format, whatever the country: `Line1`, `Line2`, `LastLine`, `City`, `State`, `ZipCode`, `ZipPlus4Code`, `CountryISO` and the other standard fields. A field the country's dataset does not carry is an empty string. See [responses](/docs/sdks/go/responses.md) for how to read it.

`Dataset` names the source. A United States address comes from `usps` and has no native record, so its `Native` is nil. An address from another country's dataset carries that dataset's raw record in `Native`, so check it before reading dataset-specific fields.

native.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

func main() {
	client := addresszen.NewClient("ak_test")
	res, err := client.RetrieveAddress(context.Background(), "usps_Z108703633|123||1119", nil)
	if err != nil {
		log.Fatal(err)
	}
	address := res.Result
	if address.Native == nil {
		fmt.Println("No native record for", address.Dataset)
		return
	}
	fmt.Printf("%T\n", address.Native.Variant())
}
```

## Cost

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

## Search places

`FindPlace()` and `ResolvePlace()` work the same way for places: countries, administrative areas, capitals and other administrative seats. `FindPlace()` returns up to 10 hits. `AsPlaceSuggestion()` on a hit returns a `PlaceSuggestion` with an `ID` and a `DescriptiveName` to show. `ResolvePlace()` turns an `ID` into a `Place`.

places.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

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

	find, err := client.FindPlace(ctx, &addresszen.FindPlaceParams{Query: "Alamogordo", CountryISO: "USA"})
	if err != nil {
		log.Fatal(err)
	}
	if len(find.Result.Hits) == 0 {
		return
	}
	hit := find.Result.Hits[0].AsPlaceSuggestion()
	if hit == nil {
		return
	}
	res, err := client.ResolvePlace(ctx, hit.ID, nil)
	if err != nil {
		log.Fatal(err)
	}
	place := res.Result
	fmt.Println(place.DescriptiveName, place.Latitude, place.Longitude)
}
```

Place suggestions are free. Each `ResolvePlace()` costs one lookup, and the API rate limits, then suspends, integrations that search without resolving.

## Errors

An HTTP error returns `*APIError`. An unknown suggestion `ID` returns status `404`. A failed connection returns the `net/http` error, wrapped. See [error handling](/docs/sdks/go/errors.md) and [`RetrieveAddress()`](/docs/sdks/go/endpoints/retrieve-address.md) for the parameters.
