# `client.find_address`

Find Address

Returns address suggestions for a partial address, ordered by relevance. Use it to power real-time address autofill.

Consider our address autocomplete JavaScript libraries, which add address lookup to a form without calling this API directly.

## API Usage

Implementing our Address Autocomplete API involves:

1. Fetch address suggestions with `/autocomplete/addresses`
2. Acquire the complete address using the ID from the suggestion

Step 2 decrements your lookup balance.

Step 1 is not a free standalone resource. We rate limit and then suspend integrations that repeatedly make autocomplete requests without a paid Step 2 request.

## Context

`context` limits the search, usually to a single country. It defaults to `GBR`, and an unrecognised context falls back to that default. If your key is not licensed for the datasets covering the context, the request is rejected.

Querying a full postcode within a supported context returns the entire address list for that postcode.

## Query Filters

Refine results by appending filters to your querystring, e.g. `postcode=sw1a2aa` for postcode `SW1A 2AA`. Invalid filters return an empty set without affecting your lookup count.

To apply multiple filter terms, use a comma-separated list, e.g. `postcode_outward=e1,e2,e3` combines result sets for E1, E2 and E3. Unless otherwise specified, all filters support multiple terms.

Filters combine with `AND` logic, for instance `su_organisation_indicator=Y&postcode_area=n`. The maximum is **8** filter terms.

## Address Bias

Preface bias searches with `bias_` to boost certain address results. Unlike filters, biasing allows unmatched addresses to appear with lower priority.

For example, use `bias_postcode_area=SW,SE` to favour addresses in the `SW` and `SE` postcode areas. Invalid bias terms have no effect.

Multiple bias terms are allowed unless stated otherwise, with a combined maximum of **5**.

## Suggestion Format

The suggestion format is subject to change. We recommend using the suggestion as-is to avoid integration issues.

## Rate Limiting and Cost

The default rate limit is 3,000 requests per 5 minutes, counted per key and IP address. The `X-RateLimit-Limit`, `X-RateLimit-Remaining` and `X-RateLimit-Reset` headers report where you stand.

Autocomplete API usage does not impact your balance, but resolving a suggestion to a full address requires a paid request. Autocomplete requests without subsequent paid requests may lead to rate limiting or suspension.

## Endpoint

`GET /autocomplete/addresses`

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

## Signature

```ruby
def find_address(
  query: nil,
  dataset: nil,
  context: nil,
  limit: nil,
  bias_lonlat: nil,
  bias_ip: nil,
  box: nil,
  postcode_outward: nil,
  postcode: nil,
  postcode_area: nil,
  postcode_sector: nil,
  post_town: nil,
  uprn: nil,
  country: nil,
  postcode_type: nil,
  su_organisation_indicator: nil,
  bias_postcode_outward: nil,
  bias_postcode: nil,
  bias_postcode_area: nil,
  bias_postcode_sector: nil,
  bias_post_town: nil,
  bias_thoroughfare: nil,
  bias_country: nil,
  postal_code: nil,
  postal_code_2: nil,
  postal_code_3: nil,
  city: nil,
  state: nil,
  state_code: nil,
  bias_postal_code: nil,
  bias_postal_code_2: nil,
  bias_postal_code_3: nil,
  bias_city: nil,
  bias_state: nil,
  bias_state_code: nil,
  is_pobox: nil,
  is_business: nil
)
```

## Arguments

| Name       | Type                | Required | Description                                                                                                         |
| ---------- | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
| `keywords` | `keyword arguments` | no       | One per [query parameter](#query-parameters), named as the API names them. A keyword left out or `nil` is not sent. |

## Query Parameters

Pass these as keyword arguments.

| Name                        | Type                    | Required | Description                                                                                                                                                                   |
| --------------------------- | ----------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `query`                     | `String`                | no       | Address Query                                                                                                                                                                 |
| `dataset`                   | `Array<String>`         | no       | Filter by Dataset                                                                                                                                                             |
| `context`                   | `String`                | no       | Context                                                                                                                                                                       |
| `limit`                     | `Integer`               | no       | Limit                                                                                                                                                                         |
| `bias_lonlat`               | `String`                | no       | Bias by Geolocation                                                                                                                                                           |
| `bias_ip`                   | `String`                | no       | Bias by Geolocation of IP                                                                                                                                                     |
| `box`                       | `String, Array<String>` | no       | Filter by Bounding Box                                                                                                                                                        |
| `postcode_outward`          | `String`                | no       | Filter by Outward Code                                                                                                                                                        |
| `postcode`                  | `String`                | no       | Filter by postcode Restrict result set to matching postcodes only. Can be combined with query to perform a postcode and building number or name search.                       |
| `postcode_area`             | `String`                | no       | Filter by Postcode Area                                                                                                                                                       |
| `postcode_sector`           | `String`                | no       | Filter by Postcode Sector                                                                                                                                                     |
| `post_town`                 | `String`                | no       | Filter by Town or City                                                                                                                                                        |
| `uprn`                      | `Integer`               | no       | Filter by UPRN                                                                                                                                                                |
| `country`                   | `String`                | no       | Filter by country                                                                                                                                                             |
| `postcode_type`             | `String`                | no       | Filter by Postcode Type                                                                                                                                                       |
| `su_organisation_indicator` | `String`                | no       | Filter by Organisation Indicator                                                                                                                                              |
| `bias_postcode_outward`     | `String`                | no       | Bias by Outward Code Boosts addresses with a matching outward code. The outward code is the first half of a postcode. For instance, the outward code of `SW1A 2AA` is `SW1A`. |
| `bias_postcode`             | `String`                | no       | Bias by postcode Boost addresses which match postcode. Can be combined with query to perform a postcode and building number or name search.                                   |
| `bias_postcode_area`        | `String`                | no       | Bias by Postcode Area                                                                                                                                                         |
| `bias_postcode_sector`      | `String`                | no       | Bias by Postcode Sector                                                                                                                                                       |
| `bias_post_town`            | `String`                | no       | Bias by Town or City                                                                                                                                                          |
| `bias_thoroughfare`         | `String`                | no       | Bias by Street                                                                                                                                                                |
| `bias_country`              | `String`                | no       | Bias by Country                                                                                                                                                               |
| `postal_code`               | `String`                | no       | Filter by postal code                                                                                                                                                         |
| `postal_code_2`             | `String`                | no       | Filter by postal code prefix                                                                                                                                                  |
| `postal_code_3`             | `String`                | no       | Filter by short postal code                                                                                                                                                   |
| `city`                      | `String`                | no       | Filter by city                                                                                                                                                                |
| `state`                     | `String`                | no       | Filter by state                                                                                                                                                               |
| `state_code`                | `String`                | no       | Filter by state code                                                                                                                                                          |
| `bias_postal_code`          | `String`                | no       | Bias by postal code                                                                                                                                                           |
| `bias_postal_code_2`        | `String`                | no       | Bias by postal code prefix                                                                                                                                                    |
| `bias_postal_code_3`        | `String`                | no       | Bias by short postal code                                                                                                                                                     |
| `bias_city`                 | `String`                | no       | Bias by city                                                                                                                                                                  |
| `bias_state`                | `String`                | no       | Bias by state                                                                                                                                                                 |
| `bias_state_code`           | `String`                | no       | Bias by state code                                                                                                                                                            |
| `is_pobox`                  | `String`                | no       | Filter by PO Box                                                                                                                                                              |
| `is_business`               | `String`                | no       | Filter by business address                                                                                                                                                    |

## Response

`IdealPostcodes::Responses::AutocompleteResponse`. Read fields with readers named as the API names them, such as `response.result`, or with `response["result"]`. `to_h` returns the decoded body.

## Errors

An HTTP error raises `IdealPostcodes::ApiError` with the HTTP `status`, the API error `code` and the decoded `body`. A transport failure raises `IdealPostcodes::TransportError`. See [error handling](/docs/sdks/ruby/errors.md).

## Example

find-address.rbruby

```ruby
require "ideal_postcodes"

client = IdealPostcodes::Client.new(api_key: "ak_test")

response = client.find_address(query: "9 Malyons Road", limit: 5)

response.result.hits.each do |hit|
  puts "#{hit.id}: #{hit.suggestion}"
end
```
