> ## Documentation Index
> Fetch the complete documentation index at: https://developer.keverd.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> keverd/keverd — verify and webhooks

# PHP SDK

```bash theme={null}
composer require keverd/keverd
```

PHP 8.1+ with `curl` and `json`.

## Initialize + verify

```php theme={null}
<?php

use Keverd\Keverd;

$keverd = new Keverd([
    'secretKey' => getenv('KEVERD_SECRET_KEY'), // kv_sk_…
]);

$result = $keverd->verify($eventId);

if (($result['action'] ?? null) === 'block') {
    // reject
} elseif (($result['action'] ?? null) === 'allow') {
    // continue
}
```

HTTP under the hood:

```http theme={null}
POST https://api.keverd.com/v2/verify
X-Secret-API-Key: kv_sk_…
Content-Type: application/json
```

```json theme={null}
{
  "event_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```

## Verify response (JSON)

`$keverd->verify()` returns an associative array matching [POST /v2/verify](https://developer.keverd.com/api-reference/verify.md):

```json theme={null}
{
  "event_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "fingerprint": "kvd_…",
  "visitor_id": "kvd_…",
  "risk_score": 42,
  "recommendation": "allow",
  "action": "allow",
  "reason": ["VPN_DETECTED"],
  "reasons": [
    {
      "code": "VPN_DETECTED",
      "explanation": "The connection appears to route through a VPN.",
      "evidence": {}
    }
  ],
  "score": 0.42,
  "trust_score": 70,
  "reputation_score": null,
  "signals": {},
  "smart_signals": {
    "vpn_detected": true,
    "action": "allow",
    "risk_score": 42
  },
  "created_at": "2026-07-25T12:00:00Z",
  "country_code": "KE",
  "country_name": "Kenya",
  "page_url": "https://example.com/login",
  "page_origin": "https://example.com",
  "location": { "ip": "203.0.113.10", "country_code": "KE" },
  "ip_info": { "asn": "13335", "asn_name": "Example ISP", "datacenter": false },
  "times_seen": 3,
  "first_seen": "2026-07-01T09:00:00Z",
  "last_seen": "2026-07-25T12:00:00Z",
  "is_new": false,
  "device_history": {
    "times_seen": 3,
    "first_seen": "2026-07-01T09:00:00Z",
    "last_seen": "2026-07-25T12:00:00Z",
    "is_new": false,
    "unique_ip_count": 2,
    "unique_country_count": 1,
    "last_seen_events": []
  }
}
```

## Laravel

Browser → your route (JSON):

```json theme={null}
{
  "eventId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "email": "user@example.com",
  "password": "…"
}
```

```php theme={null}
use Keverd\Keverd;
use Illuminate\Http\Request;

Route::post('/login', function (Request $request) {
    $keverd = new Keverd(['secretKey' => config('services.keverd.secret')]);
    $risk = $keverd->verify($request->input('eventId'));

    if (($risk['action'] ?? null) === 'block') {
        return response()->json([
            'error' => 'Blocked',
            'reasons' => $risk['reasons'] ?? [],
        ], 403);
    }

    return response()->json([
        'ok' => true,
        'visitorId' => $risk['visitor_id'] ?? null,
        'timesSeen' => $risk['times_seen'] ?? null,
        'isNew' => $risk['is_new'] ?? null,
    ]);
});
```

## Webhooks

Inbound JSON matches the [webhook envelope](https://developer.keverd.com/server/webhooks). Verify signature on the **raw** body.

```php theme={null}
Route::post('/webhooks/keverd', function (Request $request) {
    $keverd = new Keverd(['secretKey' => config('services.keverd.secret')]);

    $event = $keverd->webhooks->constructEvent(
        $request->getContent(), // raw body
        $request->header('X-Keverd-Signature', ''),
        config('services.keverd.webhook_secret'),
    );

    if ($event['type'] === 'verification.completed') {
        $verification = $event['data']['object']; // same JSON as verify()
    }

    return response()->noContent();
});
```

You can also call `Keverd\Webhooks::constructEvent(...)`.

## Errors

All SDK failures extend `Keverd\KeverdError`. Catch the specific subclass when you need different handling.

### `Keverd\KeverdError`

Base exception for client-side failures before a successful API response, or when the request never completes.

**Thrown when**

* Missing config (e.g. no `secretKey`)
* Invalid arguments (e.g. empty `eventId`)
* Network failures
* Request timeouts

### `Keverd\KeverdApiError`

Thrown when the API returns a **non-2xx** HTTP status. Extends `KeverdError`.

**Thrown when**

* Auth fails (`401` / `403`)
* Bad request (`400`)
* Quota exceeded (`402`)
* Server errors (`5xx`)

Inspect the status code and response body on the exception to decide whether to retry or surface an error.

### `Keverd\KeverdSignatureError`

Thrown by `$keverd->webhooks->constructEvent(...)` when webhook signature verification fails. Extends `KeverdError`.

**Thrown when**

* Missing `X-Keverd-Signature` or webhook secret
* Malformed signature header
* Timestamp outside the 300s tolerance
* HMAC mismatch (wrong secret, or body was parsed/modified before verify)
* Payload is not valid JSON

Treat these as **untrusted requests** — return `400` and do not process the event. See [Webhooks](https://developer.keverd.com/server/webhooks).

| Class | Typical cause |
| - | - |
| `Keverd\KeverdError` | Validation, network, timeout |
| `Keverd\KeverdApiError` | Non-2xx from API — `402` = quota |
| `Keverd\KeverdSignatureError` | Bad or missing webhook signature |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.