Skip to main content

Laravel

ideal-postcodes/laravel configures the PHP SDK from your .env file and lets you inject the client into a controller, or call it through a facade. It supports Laravel 12 and 13 on PHP 8.2 or later.

How do I install the package?​

composer require ideal-postcodes/laravel

Package discovery registers the service provider and the IdealPostcodes facade. Nothing else to add.

How do I configure it?​

Set your API key, and the Management Key if you manage keys, licensees or configurations from this application. Both belong in .env, never in a committed file.

.envbash
IDPC_API_KEY=ak_test
IDPC_MANAGEMENT_KEY=uk_...

The other options live in a config file you publish only when you need to change one:

php artisan vendor:publish --tag=ideal-postcodes-config
OptionPurpose
api_keyRequired. Your API key, or a licensee key beginning sl_, from IDPC_API_KEY
management_keyYour Management Key for key, licensee and configuration operations, from IDPC_MANAGEMENT_KEY. Leave it unset elsewhere
base_uriDefaults to https://api.ideal-postcodes.co.uk/v1. Also read from IDPC_BASE_URI
http_clientContainer binding of a PSR-18 client to send requests with. See below for the default
headersHeaders sent with every request. A User-Agent naming your application is sent ahead of the package and SDK identifiers

The package checks the key on first use, not at boot, so php artisan config:cache and package:discover run without one.

How do I use the client?​

Type-hint IdealPostcodes\Client. It is bound in the container as a singleton, also under IdealPostcodes\Contracts\ClientContract and app('ideal-postcodes').

app/Http/Controllers/AddressController.phpphp
namespace App\Http\Controllers;

use IdealPostcodes\Client;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

final class AddressController
{
public function __construct(private readonly Client $client) {}

public function byPostcode(string $postcode): JsonResponse
{
$lookup = $this->client->lookup($postcode);

return response()->json([
'found' => $lookup->found,
'addresses' => $lookup->addresses,
'suggestions' => $lookup->suggestions,
]);
}

public function suggest(Request $request): JsonResponse
{
return response()->json($this->client->findAddress(['query' => $request->string('query')->value()])->result->hits);
}
}

Response objects are JsonSerializable, so response()->json() renders them with the API's field names.

The facade suits a job, a command or a Blade component:

use IdealPostcodes\Laravel\Facades\IdealPostcodes;

$lookup = IdealPostcodes::lookup('BR8 7RE');
$valid = IdealPostcodes::emailValidation(['query' => $email])->result->deliverable;

Each API operation is one method on the client and the facade, such as addressCleanse() or keyUsage(). The endpoints reference lists them all.

Which HTTP client sends the requests?​

Laravel ships Guzzle, and the SDK finds it through php-http/discovery, so nothing needs configuring. When your application binds Psr\Http\Client\ClientInterface, the package uses that binding instead. To use a specific client, set http_client to its binding:

config/ideal-postcodes.phpphp
'http_client' => App\Http\IdealPostcodesHttpClient::class,

Timeouts belong on that client. The SDK never retries, because a timed-out lookup may already be charged.

How do I test code that uses the SDK?​

Bind a mock PSR-18 client that answers from fixtures. php-http/mock-client returns queued responses.

composer require --dev php-http/mock-client
tests/Feature/AddressControllerTest.phpphp
use GuzzleHttp\Psr7\Response;
use Http\Mock\Client as MockClient;
use Psr\Http\Client\ClientInterface;

$mock = new MockClient();
$mock->addResponse(new Response(200, ['Content-Type' => 'application/json'], $postcodeBody));
$this->app->instance(ClientInterface::class, $mock);

$this->getJson('/addresses/BR8 7RE')->assertOk();

Bind the mock before the test's first request, because the client is built once per application and keeps the HTTP client it was given. For a facade, IdealPostcodes::swap($client) replaces the whole client with one you built from IdealPostcodes::factory() and a mock.

Errors are the SDK's own exceptions. See error handling.