# `client.postcodes`

Lookup Postcode

Returns the complete list of addresses for a postcode. Postcode searches are space and case insensitive.

Each request looks up one postcode. To extract the addresses for several postcodes, send one request per postcode.

Use it to power postcode driven address searches, like [Postcode Lookup](https://docs.ideal-postcodes.co.uk/docs/postcode-lookup).

Postcode lookup covers the United Kingdom, the Republic of Ireland, the Netherlands and Singapore. The API detects the format of the postcode you submit. UK and Irish postcodes are searched by default. For a Dutch or Singapore postcode, set `context` to `NLD` or `SGP`, or to `GLOBAL` to accept any supported format.

UK postcodes need PAF, Multiple Residence, Not Yet Built, PAF Alias, PAF Welsh, AddressBase or AddressBase Premium on your key. Eircodes need ECAD or ECAF. Dutch postcodes need Kadaster. Singapore postcodes need HERE Asia Pacific. Without a matching licence the request is rejected.

An unfound postcode costs no lookup. A postcode that returns addresses costs one.

## Postcode Not Found

Invalid postcodes do not affect your lookup balance. The API returns a `404` response with this body:

```json
{
  "code": 4040,
  "message": "Postcode not found",
  "suggestions": ["SW1A 0AA"]
}
```

### Suggestions

If a postcode cannot be found, the API returns up to 5 of the closest matching postcodes. It corrects common errors first (e.g. mixing up `O` and `0` or `I` and `1`).

If the suggestion list is small (fewer than 3), the correct postcode is likely to be among them. Notify the user or trigger new searches immediately.

The suggestion list is empty if the postcode has deviated too far from a valid postcode format.

## Multiple Residence

A small number of postcodes return more than 100 premises. The API returns 100 addresses per page, so use `page` to paginate the result set.

## Endpoint

`GET /postcodes/{postcode}`

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

## Signature

```ruby
def postcodes(
  postcode,
  filter: nil,
  page: nil,
  tags: nil,
  remove_organisation: nil,
  dataset: nil,
  context: nil
)
```

## Arguments

| Name       | Type                | Required | Description                                                                                                         |
| ---------- | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
| `postcode` | `String`            | yes      | Postcode to retrieve                                                                                                |
| `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            |
| --------------------- | ----------------------- | -------- | ---------------------- |
| `filter`              | `String, Array<String>` | no       | Restrict Result Fields |
| `page`                | `Integer`               | no       | Page                   |
| `tags`                | `String, Array<String>` | no       | Tags                   |
| `remove_organisation` | `String`                | no       | Remove Organisation    |
| `dataset`             | `Array<String>`         | no       | Filter by Dataset      |
| `context`             | `String`                | no       | Context                |

## Response

`IdealPostcodes::Responses::PostcodeResponse`. 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

postcodes.rbruby

```ruby
require "ideal_postcodes"

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

response = client.postcodes("BR8 7RE")

response.result.each do |address|
  puts "#{address.line_1}, #{address.post_town}, #{address.postcode}"
end
```
