# `client.AddressCleanseAsync()`

Cleanse Address

Returns the closest matching address for a freeform address input, with Match Level indicators describing how closely each element of the suggested address matches the input. The more impaired the input address, the harder it is to cleanse.

A cleanse that returns a match costs a lookup. A no-match response is free.

## Countries

Cleanse defaults to the UK. Pass `context` with an ISO 3166-1 alpha-3 country code to cleanse an address elsewhere, e.g. `context=USA` or `context=FRA`. The address datasets your key is licensed for decide which countries it can cleanse.

The response shape is the same for every country: a standardised address in `match`, the raw source record in `match.native` and the same confidence, fit and Match Level indicators. The Match Levels and confidence score are at their most discriminating for the UK, where cleanse runs against purpose-built indexes; elsewhere they are computed from the country's address dataset.

## Confidence Score

Each incorrect, missing or misspelled element subtracts from the overall confidence score.

### Deciding on an Acceptable Confidence Score Threshold

Inputs differ widely between address cleanse projects. Within a project, though, they tend to repeat the same errors. Some datasets are keyed in by hand and prone to typos. Others have a persistently missing datapoint such as organisation name or postcode. There is no absolute Confidence Score threshold. Set the acceptable score project by project, based on the systematic errors in the data and your business goals.

To set a threshold, load a subset of the dataset into a spreadsheet application like Excel and sort on the score. Scrolling from top to bottom shows matches from best to worst. As you reach the lower quality searches you can judge roughly:

* Which confidence scores indicate ambiguous matches (i.e. up to building level only)
* Which confidence scores indicate a poor or no match (i.e. the nearest matching address is too far from the input address)

Depending on your business goals, you can also use the Match Levels to determine an acceptable match. You may need to match only up to the thoroughfare or building name, or accurate organisation names may matter.

## Endpoint

`POST /cleanse/addresses`

See the [API reference](/docs/api/address-cleanse.md) for this endpoint.

## Signature

```csharp
Task<CleanseResponse> AddressCleanseAsync(AddressCleanseBody body, AddressCleanseParams? query = null, CancellationToken cancellationToken = default)
```

## Arguments

| Name                | Type                   | Required | Description                                                           |
| ------------------- | ---------------------- | -------- | --------------------------------------------------------------------- |
| `body`              | `AddressCleanseBody`   | yes      | The JSON request body. Fields under [Request Body](#request-body).    |
| `query`             | `AddressCleanseParams` | no       | Query parameters. Fields under [Query Parameters](#query-parameters). |
| `cancellationToken` | `CancellationToken`    | no       | Cancels the request.                                                  |

## Query Parameters

Set these on the `query` argument. Each property is the name in PascalCase, such as `PostTown` for `post_town`; required fields are constructor arguments.

| Name                  | Type     | Required | Description                                                          |
| --------------------- | -------- | -------- | -------------------------------------------------------------------- |
| `tags`                | `string` | no       | Tags                                                                 |
| `remove_organisation` | `string` | no       | Remove Organisation                                                  |
| `context`             | `string` | no       | Identify the country of the address to cleanse. Defaults to UK (GBR) |

## Request Body

Set these on the `body` argument. Each property is the name in PascalCase, such as `PostTown` for `post_town`; required fields are constructor arguments. A property left `null` is not sent.

| Name        | Type     | Required | Description                                         |
| ----------- | -------- | -------- | --------------------------------------------------- |
| `query`     | `string` | yes      | Freeform address input to cleanse                   |
| `postcode`  | `string` | no       | Optionally specify the postal code for the address. |
| `post_town` | `string` | no       | Optionally specify the city or town of the address. |
| `county`    | `string` | no       | Optionally specify the county of the address.       |

## Response

`CleanseResponse`. Fields are properties in PascalCase, such as `response.Result`; fields this SDK version does not know are kept in `AdditionalProperties`. See [responses](/docs/sdks/dotnet/responses.md).

## Errors

An HTTP error throws `ApiException` with the `StatusCode`, the API error `Code` and the raw `Body`. A success response whose body cannot be read throws `UnserializableResponseException`. Network failures and cancellation throw `HttpClient`'s own `HttpRequestException` and `OperationCanceledException`. See [error handling](/docs/sdks/dotnet/errors.md).

## Example

address-cleanse.cscsharp

```csharp
using IdealPostcodes;

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

var response = await client.AddressCleanseAsync(new AddressCleanseBody("9 Malyons Rd, Swanley, BR8")
{
    Postcode = "BR8 7RE",
});

if (response.Result is GbrCleanseMatch result)
{
    Console.WriteLine($"{result.Match.Line1}, {result.Match.Postcode}");
    Console.WriteLine($"Confidence: {result.Confidence}");
    Console.WriteLine($"Postcode match: {result.PostcodeMatch}");
}
else if (response.Result is GbrCleanseNoMatch noMatch)
{
    Console.WriteLine($"No match for {noMatch.Query}");
}
```
