# PHP SDK

> Install whabuzz/whabuzz with Composer, send your first message in ten lines, verify webhooks.

Canonical: https://docs.whabuzz.com/sdks/php/

The `whabuzz/whabuzz` package for PHP 8.1+. No dependencies: it uses curl, or PHP streams when curl is missing. Request bodies and responses are Meta's own.

```bash
composer require whabuzz/whabuzz
export WHABUZZ_API_KEY="wb_live_..."   # your key
```

## First message {#first-message}

```php
<?php
require 'vendor/autoload.php';

$wb = new WhaBuzz\Client(['apiKey' => getenv('WHABUZZ_API_KEY')]);
$res = $wb->messages->send([
    'from' => '+14692311132', // your connected number
    'to' => getenv('TO'),     // the recipient, E.164
    'type' => 'text',
    'text' => ['body' => 'Hello from WhaBuzz'],
]);
echo $res['messages'][0]['id'], "\n";
```

Outside the 24-hour customer window send a [template](https://docs.whabuzz.com/guides/templates.md) instead.

## Options and resources {#surface}

`new Client(['apiKey', 'baseUrl', 'tenant', 'timeout', 'transport'])`. Pass an idempotency key as the last argument of `send`, `batch` and `create` calls; see [Idempotency](https://docs.whabuzz.com/guides/idempotency.md).

`tenant()`, `messages` (`send`, `batch`, `getBatch`, `listBatchItems`, `cancelBatch`, `markRead`), `templates` (`list`, `create`, `get`), `numbers` (`list`, `get`), `webhookEndpoints` (`list`, `create`, `get`, `update`, `delete`, `verify`, `rotateSecret`), `events` (`list`, `get`, `iterate`), `media->get`. For any other route use `$wb->json('GET', '/v1/...')`.

## Errors {#errors}

Failures throw `WhaBuzz\Exception\WhaBuzzException` with `status`, `source` (`meta`, `whabuzz`, `client`), `errorCode`, `meta` (Meta's error object, verbatim) and `requestId`. See [Errors](https://docs.whabuzz.com/errors.md).

## Verify a webhook {#verify}

```php
use WhaBuzz\WebhookVerifier;

$raw = file_get_contents('php://input'); // the exact bytes received
$event = WebhookVerifier::verify($raw, getallheaders(), getenv('WHABUZZ_WEBHOOK_SECRET'));
echo $event['type'];
```

Throws `WebhookVerificationException` (with a `reason`) when the signature, timestamp (300 s) or header is wrong. Details of the scheme: [Webhooks](https://docs.whabuzz.com/guides/webhooks.md#guarantees).
