# idpc find & resolve

Address autocomplete in two steps. `find` returns suggestions and spends no lookup. `resolve` turns one suggestion into a full address and costs a lookup. Use them to pin a specific address from partial information.

`find` is not a free standalone resource. Keys that make repeated autocomplete requests without resolving any are rate limited, then suspended. Narrow searches with `--filter` rather than paging through many queries.

To list every address at a known postcode, use [`idpc postcode`](https://docs.ideal-postcodes.co.uk/docs/cli/postcode) instead.

## `idpc find <query>`

`GET /autocomplete/addresses`.

| Flag                   | Description                                                                                     |
| ---------------------- | ----------------------------------------------------------------------------------------------- |
| `--country <iso3>`     | Search one country, by ISO-3 code (for example GBR, USA). An unsupported code falls back to GBR |
| `--limit <n>`          | Maximum suggestions, 1 to 100 (default 10)                                                      |
| `--filter <key=value>` | Return only suggestions that match. Repeatable                                                  |
| `--bias <key=value>`   | Rank matching suggestions first without excluding the rest. Repeatable                          |

Keys are the API parameter names. An unknown or repeated key fails with `invalid_input` and lists the allowed keys in `details.allowed`.

### Filter keys

| Key                           | Example                                                                 |
| ----------------------------- | ----------------------------------------------------------------------- |
| `postcode`                    | `postcode=BR87RE`                                                       |
| `postcode_outward`            | `postcode_outward=BR8`                                                  |
| `postcode_sector`             | `postcode_sector=BR8 7`                                                 |
| `postcode_area`               | `postcode_area=BR`                                                      |
| `post_town`                   | `post_town=Swanley`                                                     |
| `postcode_type`               | `postcode_type=S` (small user) or `L` (large user)                      |
| `su_organisation_indicator`   | `su_organisation_indicator=Y`                                           |
| `uprn`                        | `uprn=50002012147` (one value only)                                     |
| `country`                     | `country=England`                                                       |
| `dataset`                     | `dataset=paf,mr` (comma separated)                                      |
| `box`                         | `box=-0.2,51.5,0.1,51.4` (top-left lon, lat, bottom-right lon, lat)     |
| `postal_code`                 | Full non-UK postal code, for a US address the ZIP+4 (`941051234`)       |
| `postal_code_2`               | Postal code prefix, for a US address the three digit ZIP prefix (`941`) |
| `postal_code_3`               | Short postal code, for a US address the five digit ZIP (`94105`)        |
| `city`, `state`, `state_code` | Non-UK locality, e.g. `city=San Francisco`, `state_code=CA`             |
| `is_pobox`, `is_business`     | `true` or `false`                                                       |

A comma separated value matches any of its terms, for example `postcode_outward=BR8,DA1`. Different filters combine with AND. The API takes up to 8 filter terms and 5 bias terms.

### Bias keys

Give bias keys without the `bias_` prefix; the CLI adds it.

| Key                                                                            | Example                                                                          |
| ------------------------------------------------------------------------------ | -------------------------------------------------------------------------------- |
| `postcode`, `postcode_outward`, `postcode_sector`, `postcode_area`             | `postcode_outward=BR8`                                                           |
| `post_town`                                                                    | `post_town=London`                                                               |
| `thoroughfare`                                                                 | `thoroughfare=Malyons Road`                                                      |
| `country`                                                                      | `country=Wales`                                                                  |
| `lonlat`                                                                       | `lonlat=0.1779,51.4118,1000` (longitude, latitude, radius in metres up to 50000) |
| `ip`                                                                           | `ip=true` (bias by the caller's IP location)                                     |
| `postal_code`, `postal_code_2`, `postal_code_3`, `city`, `state`, `state_code` | Non-UK equivalents                                                               |

See the [autocomplete API reference](https://docs.ideal-postcodes.co.uk/docs/api/find-address) for how each parameter matches.

### Output

**TTY (human):** prints a numbered list of suggestions with their ids.

**Non-TTY (agent):** emits suggestions as JSON:

```json
{
  "count": 2,
  "suggestions": [
    {
      "id": "mr_55556910",
      "suggestion": "Flat 58, Heronbank, Gibbet Hill Road, Coventry, CV4",
      "udprn": 5770157,
      "umprn": 55556910,
      "urls": {
        "udprn": "/v1/udprn/5770157",
        "umprn": "/v1/umprn/55556910"
      }
    },
    {
      "id": "paf_2670849",
      "suggestion": "9 Malyons Road, Hextable, Swanley, BR8",
      "udprn": 2670849,
      "urls": {
        "udprn": "/v1/udprn/2670849"
      }
    }
  ]
}
```

* The id prefix names the dataset: `paf_` for Royal Mail PAF, `mr_` for Multiple Residence, `pafa_` for PAF Alias. Multiple Residence suggestions also carry a `umprn`.
* A UK suggestion ends at the outward code (`BR8`), not the full postcode. Resolve it, or filter by `postcode`, to pin the full postcode.

## `idpc resolve <id>`

`GET /autocomplete/addresses/{id}/gbr`. Costs a lookup.

Returns the resolved address as `result`, in UK format for addresses in any country. On a TTY, prints the address lines.

## Agent pattern

Check the top suggestion before you resolve it. When there is no exact match, `suggestions[0]` can be a different premise whose number starts with yours, such as unit 2905 for house 290. Compare the house number, or narrow with `--filter`, so you don't spend a lookup on the wrong address.

```bash
# Narrow to a postcode, then resolve the top hit
ID=$(idpc find "9 malyons" --filter postcode=BR87RE | jq -r '.suggestions[0].id')
idpc resolve "$ID"

# Search a wider area, favouring one town
idpc find "high street" --filter postcode_area=BR --bias post_town=Swanley --limit 20
```
