PHP library
A server-side client for the Voidhash REST API.
The mobile SDKs decide what to show on the device. They cannot protect anything a customer can reach by calling your API directly, such as an export endpoint or a paid model. Those checks belong on your server, and this library is how you make them.
voidhash/voidhash-php is a client for the Voidhash REST API. It authenticates every request with a
project secret key, and acts on the project the key belongs to.
Install the library
Install the package with Composer.
composer require voidhash/voidhash-phpThe library needs PHP 8.2 or newer with the curl, json, and mbstring extensions. Every common
PHP build already includes them.
Create a secret key
In Studio, open Settings → API keys and create a secret key. The raw value is shown only once.
Store the key in an environment variable or your secret manager. A secret key grants full access to the project. Never put it in a mobile app, a web bundle, or a repository.
Create the client
Create one client with your secret key and share it across your application.
use Voidhash\VoidhashClient;
$voidhash = VoidhashClient::create(getenv("VOIDHASH_SECRET_KEY"));create takes the secret key and these options.
| Option | Default | Description |
|---|---|---|
projectId | the key's project | The project every call acts on. Set it to skip looking it up. |
baseUrl | https://api.voidhash.com | Overrides the API origin. |
headers | [] | Extra headers the library adds to every request from this client. |
The library sends the secret key as Authorization: Bearer <secretKey>. create makes no request.
It throws Voidhash\Exception\ApiException with the code configuration_error when the secret key
is blank or headers contains Authorization or x-secret-key in any casing.
A secret key belongs to one project, so you never pass the project's id to a method. Without
projectId, the library reads the key's project from
GET /api/v1/session on the first call that needs it
and keeps it. When the credential can use several projects, set projectId; otherwise those calls
throw a configuration_error.
With the client configured, continue with checking access or receiving webhooks.
Resource methods
Each API resource is a property of the client, such as persons or webhooks. The example below
checks a perk and then registers a webhook endpoint.
use Voidhash\Generated\Core\Model\CreateWebhookBodyJsonEncoding;
$hasPremium = $voidhash->persons->entitlements->hasActivePerk([
"distinctId" => "user_123",
"perkSlug" => "premium",
]);
$endpoint = $voidhash->webhooks->endpoints->create(
(new CreateWebhookBodyJsonEncoding())
->setName("production-backend")
->setUrl("https://api.example.com/webhooks/voidhash")
->setEvents(["subscription.created", "purchase.completed"]),
);Methods return models generated from the API schemas, with a getter for each field of the JSON response documented in the API reference. Lists return every item, following the pages for you.