# azn 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.

## `azn find <query>`

`GET /autocomplete/addresses`.

| Flag                   | Description                                                                                                                                                                                                                                                                          |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `--country <iso3>`     | Search one country, by ISO-3 code (for example USA, CAN). Defaults to USA. A value that is not three letters, such as `CA`, fails with `invalid_input`; an unsupported three-letter code falls back to USA. [Supported Countries](/docs/data/supported-countries.md) lists the codes |
| `--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                                                                 |
| ------------------------- | ----------------------------------------------------------------------- |
| `postal_code`             | Full postal code, for a US address the ZIP+4 (`940431351`)              |
| `postal_code_2`           | Postal code prefix, for a US address the three digit ZIP prefix (`940`) |
| `postal_code_3`           | Short postal code, for a US address the five digit ZIP (`94043`)        |
| `city`                    | `city=Mountain View`                                                    |
| `state`                   | `state=California`                                                      |
| `state_code`              | `state_code=CA`                                                         |
| `is_pobox`, `is_business` | `true` or `false`                                                       |
| `box`                     | `box=-123,38,-122,37` (top-left lon, lat, bottom-right lon, lat)        |
| `dataset`                 | Comma separated dataset codes                                           |

A comma separated value matches any of its terms, for example `state_code=CA,NV`. 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                                                                           |
| ----------------------------------------------- | --------------------------------------------------------------------------------- |
| `postal_code`, `postal_code_2`, `postal_code_3` | `postal_code_3=94043`                                                             |
| `city`, `state`, `state_code`                   | `city=Mountain View`                                                              |
| `lonlat`                                        | `lonlat=-122.084,37.422,1000` (longitude, latitude, radius in metres up to 50000) |
| `ip`                                            | `ip=true` (bias by the caller's IP location)                                      |

See the [autocomplete API reference](https://docs.addresszen.com/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": 1,
  "suggestions": [
    {
      "suggestion": "1600 Amphitheatre Pkwy, Mountain View, CA, 94043",
      "urls": {},
      "id": "usps_Z227612380|1600||1351"
    }
  ]
}
```

Ids contain `|`, so quote them in the shell.

## `azn resolve <id>`

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

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

```json
{
  "result": {
    "line_1": "1600 Amphitheatre Pkwy",
    "line_2": "",
    "city": "Mountain View",
    "state": "California",
    "state_abbreviation": "CA",
    "zip_code": "94043",
    "plus_4_code": "1351",
    "zip_plus_4_code": "94043-1351",
    "last_line": "Mountain View CA 94043-1351",
    "country": "United States",
    "country_iso": "USA"
  }
}
```

Outside the US the same fields carry the local address. The full postal code is in `zip_plus_4_code`, `zip_code` and `plus_4_code` are empty, and `state` holds the province or region. The country's own record is under `result.native`, documented on each dataset page; for Canada it includes `civic_no`, `mail_postal_code`, `latitude` and `longitude`.

```json
{
  "result": {
    "line_1": "255 Bremner Blvd",
    "city": "Toronto",
    "state": "Ontario",
    "state_abbreviation": "ON",
    "zip_code": "",
    "zip_plus_4_code": "M5V 3M9",
    "country": "Canada",
    "country_iso": "CAN",
    "native": { "civic_no": "255", "mail_postal_code": "M5V3M9" }
  }
}
```

## 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 ZIP code, then resolve the top hit
ID=$(azn find "1600 amphitheatre" --filter postal_code_3=94043 | jq -r '.suggestions[0].id')
azn resolve "$ID"

# Search a state, favouring one city
azn find "1600 amphitheatre" --filter state_code=CA --bias city="Mountain View" --limit 20
```
