For Laravel address validation, create a custom validation rule that calls the AutoPostcode API from a service class, then apply it in a Form Request. The rule passes only when the postcode resolves to a real Royal Mail PAF address (~31M UK delivery points, ~1.8M postcodes), so invalid addresses never reach your database.
How do I call the PAF API from a service?
Wrap the HTTP call in a service using Laravel's Http client and a config-based key:
// app/Services/AddressLookup.php
use Illuminate\Support\Facades\Http;
class AddressLookup
{
public function exists(string $postcode): bool
{
$res = Http::withToken(config('services.autopostcode.key'))
->get('https://api.autopostcode.com/v1/lookup', ['postcode' => $postcode]);
return $res->ok() && count($res->json('addresses', [])) > 0;
}
}How do I write the custom rule?
Create a rule that delegates to the service so it stays thin and testable:
// app/Rules/ValidUkPostcode.php
use Illuminate\Contracts\Validation\ValidationRule;
use App\Services\AddressLookup;
class ValidUkPostcode implements ValidationRule
{
public function validate(string $attr, mixed $value, \Closure $fail): void
{
if (! app(AddressLookup::class)->exists($value)) {
$fail('The :attribute is not a recognised UK postcode.');
}
}
}How do I apply it in a Form Request?
Add the rule to your Form Request and Laravel runs it automatically:
// app/Http/Requests/StoreOrderRequest.php
public function rules(): array
{
return [
'postcode' => ['required', 'string', new ValidUkPostcode()],
];
}Format check vs PAF validation
| Check | Catches | Reliability |
|---|---|---|
| Regex format only | Malformed postcodes | Low — accepts fake but valid-looking |
| PAF existence check | Non-existent addresses | High — real delivery points |
For a framework-agnostic PHP example see PHP postcode lookup, power the check with the UK address validation API, and get set up fast with the API quickstart.
A UK-first, affordable alternative
AutoPostcode uses the same Royal Mail PAF data as Loqate, Fetchify and Ideal Postcodes, with a REST API that slots neatly into Laravel's validation pipeline.
Frequently asked questions
How do I validate UK addresses in Laravel?
Create a custom validation rule that calls the AutoPostcode API from a service class, then apply the rule in your form request. The rule passes only when the postcode resolves to a real Royal Mail PAF address, so invalid addresses never reach your database.
Where should the API call live in Laravel?
Put the HTTP call in a dedicated service class using Laravel's Http client, and inject it into your custom rule. This keeps the rule thin, testable and easy to mock.
How do I store the API key in Laravel?
Add it to your .env as AUTOPOSTCODE_KEY and reference it via a config file, never hard-coded. It stays server-side, so it is safe to use in requests.
Can I use it in a Form Request?
Yes. Add the custom rule to the rules() array of a Form Request so validation runs automatically before your controller executes, returning standard Laravel validation errors.
Should I cache lookups in Laravel?
Yes. Postcode results are stable, so caching responses with Laravel's cache reduces repeat API calls and speeds up validation, subject to your data licence terms.
Is the data Royal Mail PAF?
Yes. AutoPostcode is built on Royal Mail PAF, covering ~31M UK delivery points across ~1.8M postcodes, so validation checks against authoritative UK data.
Ready to get started?
Add Royal Mail PAF-verified UK address lookup to your site in minutes — start free, no card required.
