# HighLevel PHP SDK

Source: https://marketplace.gohighlevel.com/docs/sdk/php

The `gohighlevel/api-client` composer package is the officially supported SDK for PHP 7.4+ projects. It wraps the full HighLevel API with PSR-18 friendly services, automatic OAuth token rotation, webhook helpers, and pluggable session storage.

## Installation

```bash
composer require gohighlevel/api-client
```

## Quick Start

### Initialize with a Private Integration Token

```php
<?php

require_once __DIR__ . '/vendor/autoload.php';

use HighLevel\HighLevel;
use HighLevel\HighLevelConfig;

$config = new HighLevelConfig([
    'privateIntegrationToken' => $_ENV['GHL_PIT'],
]);

$ghl = new HighLevel($config);
```

### Initialize with OAuth credentials

```php
use HighLevel\HighLevel;
use HighLevel\HighLevelConfig;

$config = new HighLevelConfig([
    'clientId' => $_ENV['GHL_CLIENT_ID'],
    'clientSecret' => $_ENV['GHL_CLIENT_SECRET'],
]);

$ghl = new HighLevel($config);
```

### Make your first API call

```php
use HighLevel\Services\Contacts\Models\SearchBodyV2DTO;

$body = new SearchBodyV2DTO([
    'locationId' => 'zBG0T99IsBgOoXUrcROH',
    'pageLimit' => 1,
]);

$contactsResponse = $ghl->contacts->searchContactsAdvanced($body);

error_log(json_encode($contactsResponse, JSON_PRETTY_PRINT));
```

## Session storage

Use `HighLevel\Storage\MongoDBSessionStorage` provided by SDK to use mongo as storage or extend it to store tokens in MySQL, PostgreSQL, Redis, etc:

```php
<?php
use HighLevel\HighLevel;
use HighLevel\Storage\MongoDBSessionStorage;

$sessionStorage = new MongoDBSessionStorage(
    $config['mongo_url'],
    $config['mongo_db_name'],
    $config['collection_name']
);

$ghl = new HighLevel([
    'clientId' => $config['client_id'],
    'clientSecret' => $config['client_secret'],
    'sessionStorage' => $sessionStorage,
    'logLevel' => 'warn'
]);
```

## Webhook support

SDK provides webhook support which can be used as shown below. It will handle INSTALL and UNINSTALL events sent by HighLevel. It will generate token and store it in the db.

```php
$payload = $request->getBody()->getContents();
$signature = $request->getHeaderLine('x-wh-signature');

$ghl->getWebhookManager()->processWebhook(
    $payload, // pass raw request body as string here
    $signature,
    $_ENV['WEBHOOK_PUBLIC_KEY'],
    $_ENV['GHL_CLIENT_ID']
);
```

Call `verifySignature` directly when you just need validation:

```php
$ghl->getWebhookManager()->verifySignature(
    $payload,
    $signature,
    $_ENV['WEBHOOK_PUBLIC_KEY']
);
```

## Additional resources

You can find some SDK & additional examples here:

[SDK](https://github.com/GoHighLevel/highlevel-api-php)

[packagist](https://packagist.org/packages/gohighlevel/api-client)

[Examples](https://github.com/GoHighLevel/ghl-sdk-examples/tree/main/php)
