# Address cleanse

`AddressCleanse()` matches a freeform UK address to the closest address on file and scores the match. Use it to clean addresses you already hold, such as a CRM export.

cleanse.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.AddressCleanse(context.Background(), idealpostcodes.AddressCleanseBody{
		Query: "9 Malyons Rd, Swanley, BR8 7RE",
	}, nil)
	if err != nil {
		log.Fatal(err)
	}
	if match := res.Result.AsGbrCleanseMatch(); match != nil && match.Confidence > 0 {
		fmt.Printf("%s, %s (%v)\n", match.Match.Line1, match.Match.Postcode, match.Confidence)
		return
	}
	fmt.Println("No match")
}
```

## Match or no match

Check the type and the score. A match is a `GbrCleanseMatch` with a `Confidence` above 0 and the matched `Address` in `Match`. A no-match is a `GbrCleanseNoMatch`, with every score at 0, `Match` empty and every match level at `NO_MATCH`. `res.Result.Variant()` returns either, for a type switch.

## Separate postcode

If your records keep the postcode or post town in their own fields, set `Postcode` and `PostTown` on the body alongside `Query` rather than joining them into it. Both are optional, so set them with `new(value)`. The last argument takes the query parameters: `Tags`, `Context` and `RemoveOrganisation`.

## Addresses outside the UK

Set `Context` to the country's ISO 3166-1 alpha-3 code. Cleanse defaults to the UK. The address datasets your key is licensed for decide which countries it can cleanse.

cleanse\_usa.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.AddressCleanse(context.Background(),
		idealpostcodes.AddressCleanseBody{Query: "1600 Pennsylvania Avenue NW, Washington, DC 20500"},
		&idealpostcodes.AddressCleanseParams{Context: "usa"})
	if err != nil {
		log.Fatal(err)
	}
	if match := res.Result.AsGbrCleanseMatch(); match != nil {
		fmt.Println(match.Match.Postcode)
	}
}
```

The result has the same shape for every country, so a match elsewhere is still a `GbrCleanseMatch` and the check above works unchanged.

## Organisation in address lines

Set `RemoveOrganisation` to `"true"`. The organisation and department names are dropped from the address lines and the remaining lines move up. Use it when you deliver to people rather than businesses.

cleanse\_lines.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.AddressCleanse(context.Background(),
		idealpostcodes.AddressCleanseBody{Query: "10 Downing Street, London, SW1A 2AA"},
		&idealpostcodes.AddressCleanseParams{RemoveOrganisation: "true"})
	if err != nil {
		log.Fatal(err)
	}
	if match := res.Result.AsGbrCleanseMatch(); match != nil {
		fmt.Println(match.Match.Line1) // "10 Downing Street", not the organisation
	}
}
```

`OrganisationName`, `DepartmentName` and the `Native` record are unchanged. If removing the names would leave no address lines, they are kept.

## Confidence score

It depends on your data. Each wrong, missing or misspelt element lowers the score, and one source tends to repeat the same mistakes. Cleanse a sample, sort it by `Confidence` and set your threshold where the matches start to go wrong.

The result also scores each element separately, in `OrganisationMatch`, `PremiseMatch`, `ThoroughfareMatch`, `LocalityMatch`, `PostTownMatch` and `PostcodeMatch`. The [API reference](/docs/api/address-cleanse.md) explains each level.

## Cost

One lookup for a match. A no-match costs nothing.

See [`AddressCleanse()`](/docs/sdks/go/endpoints/address-cleanse.md) for the body and parameters.
