# Ideal Postcodes PHP SDK

[`ideal-postcodes/sdk`](https://packagist.org/packages/ideal-postcodes/sdk) calls the Ideal Postcodes UK address API from PHP 8.2 or later, with a typed method for each operation over any PSR-18 HTTP client.

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
composer require ideal-postcodes/sdk
```

search.phpphp

```php
use IdealPostcodes\IdealPostcodes;

$client = IdealPostcodes::client('ak_test');
$response = $client->findAddress(['query' => '9 Malyons Road']);

foreach ($response->result->hits as $hit) {
    echo $hit->suggestion, PHP_EOL;
}
```

Each API operation is one method on the client, named from its operation ID, such as `postcodes()` or `findAddress()`. Every method returns a read-only object built from the API response.

## Where does the address data come from?

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.

## Why use the SDK?

* **Any HTTP client.** The SDK sends requests through the PSR-18 client already in your project, found by `php-http/discovery`. Guzzle and Symfony HttpClient both work. There is no bundled transport to configure.
* **Generated from the OpenAPI spec.** Every response is a typed, read-only object. Every dataset's native record has its own class, from `PafAddress` to `UspsAddress`.
* **Every current API operation.** Address search, [postcode lookup](/docs/sdks/php/postcode-lookup.md), [UPRN lookup](/docs/sdks/php/uprn-lookup.md), [autocomplete](/docs/sdks/php/autocomplete.md), [address cleanse](/docs/sdks/php/address-cleanse.md), places, [email](/docs/sdks/php/email-validation.md) and [phone validation](/docs/sdks/php/phone-validation.md), plus [key](/docs/sdks/php/keys.md), [licensee](/docs/sdks/php/licensees.md) and configuration management. Each one has its own [reference page](/docs/sdks/php/endpoints.md).
* **Static analysis friendly.** Parameters are PHPStan array shapes, enums are literal types and the source passes PHPStan at its strictest level.

## What can coding agents use?

* **Agent skill.** `npx skills add ideal-postcodes/skills` installs the `ideal-postcodes-php-sdk` skill, built from these pages.
* **[Endpoints](/docs/sdks/php/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/php/errors.md`](/docs/sdks/php/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.

## How do I authenticate?

Pass your API key to `IdealPostcodes::client()`. Key, licensee and configuration operations also need a Management Key, passed as the second argument. Keep the Management Key on your server. See [setup](/docs/sdks/php/setup.md).

## How are errors reported?

An HTTP error throws `ApiException` with the HTTP status and the API error code and message. A failed connection throws `TransporterException`. Both implement `IdealPostcodesException`, so one catch covers the SDK. See [error handling](/docs/sdks/php/errors.md).
