# Ideal Postcodes Ruby SDK

[`ideal_postcodes`](https://rubygems.org/gems/ideal_postcodes) calls the Ideal Postcodes UK address API from Ruby 3.3 or later, with one method per operation and no runtime dependencies.

Use the `ak_test` key for limited testing. It is capped at 5 requests per day. A new account comes with a free test balance for fuller testing.

```bash
gem install ideal_postcodes
```

search.rbruby

```ruby
require "ideal_postcodes"

client = IdealPostcodes::Client.new(api_key: "ak_test")
response = client.find_address(query: "9 Malyons Road")

response.result.hits.each { |hit| puts hit.suggestion }
```

Each API operation is one method on the client, named from its operation ID, such as `postcodes` or `find_address`. Path parameters are positional and everything else is a keyword argument. Every method returns an object built from the API response.

## Data sources

UK addresses come from Royal Mail's Postcode Address File (PAF) and Ordnance Survey AddressBase, depending on the datasets on your key. Each address carries a `dataset` field naming its source, such as `paf` or `abp`. Its `native` record holds the raw fields from that source.

## Features

* **No dependencies.** The SDK sends requests with Net::HTTP and parses them with the standard library's JSON. To use another HTTP client, pass your own [transport](/docs/sdks/ruby/setup.md#custom-transport).
* **Generated from the OpenAPI spec.** Every response is an object with one reader per field. Every dataset's native record has its own class, from `PafAddress` to `UspsAddress`.
* **Every current API operation.** Address search, [postcode lookup](/docs/sdks/ruby/postcode-lookup.md), [UPRN lookup](/docs/sdks/ruby/uprn-lookup.md), [autocomplete](/docs/sdks/ruby/autocomplete.md), [address cleanse](/docs/sdks/ruby/address-cleanse.md), places, [email](/docs/sdks/ruby/email-validation.md) and [phone validation](/docs/sdks/ruby/phone-validation.md), plus [key](/docs/sdks/ruby/keys.md), [licensee](/docs/sdks/ruby/licensees.md) and configuration management. Each one has its own [reference page](/docs/sdks/ruby/endpoints.md).
* **RBS signatures.** The gem ships a signature for every method and reader, for Steep, Ruby LSP and other tools that read RBS.

## Coding agents

* **Agent skill.** `npx skills add ideal-postcodes/skills` installs the `ideal-postcodes-ruby-sdk` skill, built from these pages.
* **[Endpoints](/docs/sdks/ruby/endpoints.md).** Every operation, grouped by API area, with its parameters, response class and a tested example.
* **Markdown pages.** Add `.md` to any page URL for plain markdown, such as [`/docs/sdks/ruby/errors.md`](/docs/sdks/ruby/errors.md).
* **[llms.txt](https://docs.ideal-postcodes.co.uk/llms.txt).** An index of every page on this site.
* **[OpenAPI spec](https://openapi.ideal-postcodes.co.uk/openapi.json).** The API contract the SDK is generated from.
* **[Build with AI](/docs/guides/llms.md).** Every asset for AI agents.

## Authentication

Pass your API key to `IdealPostcodes::Client.new` as `api_key:`. Key, licensee and configuration operations also need a Management Key, passed as `management_key:`. Keep the Management Key on your server. See [setup](/docs/sdks/ruby/setup.md).

## Errors

An HTTP error raises `IdealPostcodes::ApiError` with the HTTP status and the API error code and message. A failed connection raises `IdealPostcodes::TransportError`. Both inherit from `IdealPostcodes::Error`, so one `rescue` covers the SDK. See [error handling](/docs/sdks/ruby/errors.md).

## Upgrading from 2.x

Version 3 is a new client. The old `IdealPostcodes.api_key =` setting and module methods such as `IdealPostcodes::Postcode.lookup` are gone: create a client and call its methods instead. Responses are objects with readers, not hashes, though `to_h` returns the hash and `[]` reads a field by its API name. `AuthenticationError`, `TokenExhaustedError` and the other 2.x error classes give way to one `IdealPostcodes::ApiError`, whose `code` says which error the API returned. Version 3 needs Ruby 3.3 or later.
