# Laravel

[`addresszen/laravel`](https://packagist.org/packages/addresszen/laravel) configures the [PHP SDK](/docs/sdks/php.md) 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.

## Install

```bash
composer require addresszen/laravel
```

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

## Configure

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

```bash
ADDRESSZEN_API_KEY=ak_test
ADDRESSZEN_MANAGEMENT_KEY=uk_...
```

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

```bash
php artisan vendor:publish --tag=addresszen-config
```

| Option           | Purpose                                                                                                                        |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `api_key`        | Required. Your API key, or a licensee key beginning `sl_`, from `ADDRESSZEN_API_KEY`                                           |
| `management_key` | Your Management Key for key, licensee and configuration operations, from `ADDRESSZEN_MANAGEMENT_KEY`. Leave it unset elsewhere |
| `base_uri`       | Defaults to `https://api.addresszen.com/v1`. Also read from `ADDRESSZEN_BASE_URI`                                              |
| `http_client`    | Container binding of a PSR-18 client to send requests with. See below for the default                                          |
| `headers`        | Headers 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.

## Use the client

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

app/Http/Controllers/AddressController.phpphp

```php
namespace App\Http\Controllers;

use AddressZen\Client;
use AddressZen\Types\UsaVerifyMatch;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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

    public function verify(Request $request): JsonResponse
    {
        $result = $this->client->addressVerify(['query' => $request->string('address')->value()])->result;

        return response()->json([
            'verified' => $result instanceof UsaVerifyMatch,
            'result' => $result,
        ]);
    }

    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:

```php
use AddressZen\Laravel\Facades\AddressZen;

$hits = AddressZen::findAddress(['query' => '1010 Cauthen Ln'])->result->hits;
$valid = AddressZen::emailValidation(['query' => $email])->result->deliverable;
```

Each API operation is one method on the client and the facade, such as `addressVerify()` or `keyUsage()`. The [endpoints reference](/docs/sdks/php/endpoints.md) lists them all.

## HTTP client

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/addresszen.phpphp

```php
'http_client' => App\Http\AddressZenHttpClient::class,
```

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

## Test code that uses the SDK

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

```bash
composer require --dev php-http/mock-client
```

tests/Feature/AddressControllerTest.phpphp

```php
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'], $verifyBody));
$this->app->instance(ClientInterface::class, $mock);

$this->getJson('/verify?address=1010 Cauthen Ln, Alamogordo, NM 88310')->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, `AddressZen::swap($client)` replaces the whole client with one you built from the SDK's `AddressZen\AddressZen::factory()` and a mock.

Errors are the SDK's own exceptions. See [error handling](/docs/sdks/php/errors.md).
