# Address verification

`AddressVerify()` checks a US address against the USPS Coding Accuracy Support System (CASS). It returns the address corrected and standardized, with its ZIP+4 code and USPS delivery data.

verify.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

func main() {
	client := addresszen.NewClient("ak_test")
	res, err := client.AddressVerify(context.Background(), addresszen.AddressVerifyBody{
		Query: "1010 Cauthen Ln, Alamogordo, NM 88310",
	}, nil)
	if err != nil {
		log.Fatal(err)
	}
	if nm := res.Result.AsUsaVerifyNoMatch(); nm != nil {
		fmt.Println("No match:", nm.MatchInformation)
		return
	}
	m := res.Result.AsUsaVerifyMatch()
	if m == nil {
		return
	}
	if cass := m.Match.AsUsaCassVerifiedAddress(); cass != nil {
		fmt.Println(m.ZipCode, "dpv="+cass.Dpv, "rdi="+cass.Rdi)
		return
	}
	if addr := m.Match.AsAddress(); addr != nil {
		fmt.Println(m.ZipCode, addr.Dataset)
	}
}
```

The address goes in the request body, `AddressVerifyBody`. Query parameters, such as `Context` and `Tags`, go in an optional `*AddressVerifyParams`, the last argument. Your key needs address verification enabled, or the call returns an `*APIError` with status `401`.

## Read a match

A match is a `UsaVerifyMatch`. The standardized address is at the top level, in `AddressLineOne`, `AddressLineTwo`, `City`, `State` and `ZipCode`. The full USPS record behind it is in `Match`: `Match.AsUsaCassVerifiedAddress()` returns it as a `*UsaCassVerifiedAddress`. The example above returns ZIP+4 `88310-5631`. The [API reference](/docs/api/address-verify.md) lists every field, including county, congressional district, time zone and coordinates.

## Check deliverability

Check `Dpv` on the CASS record, the USPS Delivery Point Validation result. `Y` means USPS confirms the address as a delivery point. `D` means the apartment or suite number is missing, `S` means it is extra or incorrect and `N` means USPS does not confirm the address.

`Confidence` measures something else, how well your input matched an address, and the two can disagree. `123 Main St, Springfield, CO 81073` scores a `Confidence` of `1` but a `Dpv` of `N`: the street exists, but USPS does not confirm the number.

## Handle no match

An address that cannot be verified still returns without error, as a `UsaVerifyNoMatch` with `Match` nil, a `Count` of `0`, empty address fields and the reason in `MatchInformation`, such as `Address not found` or `House number not valid for this street`. The wording may change, so do not parse it. `AsUsaVerifyMatch()` returns nil for it, and `res.Result.Variant()` returns either type for a type switch.

CASS corrects what it can first: an address with the wrong city or state for its ZIP code still verifies, with the corrected values.

## Fit and confidence

Both run from 0 to 1. `Fit` compares only the parts you sent, so `1010 Cauthen Ln` with a ZIP code and nothing else can score a `Fit` of 1. `Confidence` also counts the parts you left out, so the same input scores below 1.

## Send the address in parts

Put the street line in `Query` and set `ZipCode`, or `City` and `State`. These fields are optional, so set them with `new(value)`.

verify\_parts.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

func main() {
	client := addresszen.NewClient("ak_test")
	res, err := client.AddressVerify(context.Background(), addresszen.AddressVerifyBody{
		Query:   "1010 Cauthen Ln",
		ZipCode: new("88310"),
	}, nil)
	if err != nil {
		log.Fatal(err)
	}
	if m := res.Result.AsUsaVerifyMatch(); m != nil {
		fmt.Println(m.AddressLineOne, m.ZipCode)
	}
}
```

`ZipCode` accepts `88310-5631`, `883105631` or `88310`. `State` takes the two-letter abbreviation.

## Verify outside the United States

Set `Context` on `AddressVerifyParams` to the country's ISO 3166-1 alpha-3 code. Outside the United States `Match.AsAddress()` returns an `*Address`, the same type `RetrieveAddress()` returns, with a `Dataset` and a `Native` record from that country's source.

verify\_elsewhere.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	addresszen "go.addresszen.com/sdk"
)

func main() {
	client := addresszen.NewClient("ak_test")
	res, err := client.AddressVerify(context.Background(),
		addresszen.AddressVerifyBody{Query: "55 Rue du Faubourg Saint-Honoré, 75008 Paris"},
		&addresszen.AddressVerifyParams{Context: "FRA"})
	if err != nil {
		log.Fatal(err)
	}
	if m := res.Result.AsUsaVerifyMatch(); m != nil {
		if addr := m.Match.AsAddress(); addr != nil {
			fmt.Println(addr.Line1, addr.Dataset)
		}
	}
}
```

The top-level fields, `AddressLineOne` to `CountryISO2`, are filled for every country. See [responses](/docs/sdks/go/responses.md) for every type a result can take.

## Cost

Every verification the CASS engine answers costs one lookup, matched or not. A query that cannot be parsed into an address costs nothing. A verification still running after 9.5 seconds returns an `*APIError` with status `429`.

See [`AddressVerify()`](/docs/sdks/go/endpoints/address-verify.md) for the parameters.
