Skip to content

PHP SDK

The PHP SDK is a typed client over the V2 API. One Client, a resource per API area, and an Eloquent-flavoured query builder for analytics. Laravel apps get a facade and session-id plumbing on top; see Laravel.

Terminal window
composer require snipform/php-sdk

Requires PHP 8.2+, Guzzle 7, and symfony/http-foundation 6 or 7. The Laravel layer is optional and activates when illuminate/support (10 to 13) is installed.

use SnipForm\SnipForm;
$snipform = SnipForm::client('YOUR_PROPERTY_TOKEN');
$metrics = $snipform->signals()->last7Days()->metrics();
echo "Sessions: {$metrics->sessions}, bounce: {$metrics->bounceRate}%";

The token is a property-scoped API token created in the dashboard under Property → API. Everything the client does is scoped to that property, and the token’s scopes decide which calls succeed. See Authentication & Tokens.

SnipForm::client($token, $options) is shorthand for new SnipForm\Client($token, $options). Options:

OptionDefaultNotes
base_urlhttps://api.snipform.io
path_prefix/v2/Older self-hosted deployments may serve under /api/v2/
timeout30Seconds per request
verify_ssltrueSet false only for local self-signed certificates

Every request carries User-Agent: snipform-php-sdk/0.0.13.

Each $snipform->resource() call returns a fresh instance, so chains never leak state into one another.

CallCoversReference
signals()Sessions, metrics, graph, liveQuerying Signals
session()Event and acquisition writes on one sessionResources
contacts()Identify, list, update, delete contactsResources
conversions()Definitions and funnel analyticsResources
linkGroups(), links(), clicks()Short linksResources
attribution()Channel preview and UTM presetsResources
properties()Property overviewBelow
$property = $snipform->properties()->overview();
$property->id; // string
$property->name; // string
$property->domain; // string
$property->hasSignals; // bool - tracking has fired at least once
$property->state; // string|null
$property->stateName; // string|null - human label
$property->counts; // ['sessions' => 188862, 'forms' => 4, 'pages' => 2, ...]

Every typed return value extends SnipForm\Data\SnipFormDTO: public readonly properties, toArray(), and JsonSerializable.

$metrics->toArray(); // associative array of the public fields
json_encode($metrics); // same thing

DTOs carry only the fields the SDK surfaces. When you need something else from the response, switch the chain to raw mode:

$snipform->properties()->asRaw()->overview(); // array
$snipform->signals()->last28Days()->asRaw()->metrics(); // array with analytics, meta, options
$snipform->conversions()->asRaw()->find($id); // array

asRaw() works on every resource and is forwarded into builders started from it (conversions()->asRaw()->create()->...->save() returns an array). The raw array is the data block of the API envelope.

List endpoints return a PaginatedCollection. Iterating walks every page; the other helpers fetch what they need and no more.

$sessions = $snipform->signals()->last28Days()->sessions();
foreach ($sessions as $session) { ... } // page after page, transparently
$sessions->first(); // first row, one request
$sessions->count(); // total from the paginator meta
$sessions->all(); // every row in memory - be careful

->page($n) fetches one page and returns a SnipForm\Data\Page with the rows plus the paginator meta:

$page = $snipform->signals()->sessions(20)->page(2);
$page->items; // SessionRow[] (arrays in raw mode)
$page->currentPage; // 2
$page->lastPage; // 5
$page->total; // 230
$page->perPage; // 20
$page->from; // 21
$page->to; // 40
$page->hasMore(); // bool
$page->isFirstPage();
$page->isLastPage();
$page->next(); // Page 3, or null on the last page - one request
$page->prev(); // Page 1, or null on the first page
$page->first();
$page->last();
$page->pageLink($page->nextPageUrl); // follow any paginator URL
$page->raw(); // the full paginator JSON, including `links`

A Page is iterable, countable and array-accessible, so foreach ($page as $row), count($page) (rows on this page) and $page[0] all work.

DTOs, PaginatedCollection and Page are all JsonSerializable, so a Laravel or PSR-7 controller can return them as-is:

public function dashboard(\SnipForm\Client $snipform)
{
return $snipform->signals()->last7Days()->metrics(); // typed JSON
}
public function sessions(\SnipForm\Client $snipform, Request $request)
{
return $snipform->signals()
->last28Days()
->sessions(20)
->page((int) $request->input('page', 1)); // Laravel paginator JSON
}

A PaginatedCollection serialises as page 1 in Laravel’s paginator shape (data, current_page, last_page, total, next_page_url, …). Return a Page for a specific page.

Everything the SDK throws extends SnipForm\Exceptions\SnipFormException.

ExceptionWhenBefore HTTP?
InvalidPeriodExceptionA string passed to period() is not a known periodYes
IncompatibleFieldOperatorA SessionField case used with an operator its type does not supportYes
MissingSessionIdExceptionsession()->event() or acquisition() could not resolve a session idYes
AuthenticationException401 or 403: bad token, wrong property, missing scopeNo
ApiExceptionAny other 4xx or 5xx with a structured bodyNo
SnipFormExceptionTransport failure, JSON decode failure, anything elseNo
use SnipForm\Exceptions\ApiException;
use SnipForm\Exceptions\AuthenticationException;
use SnipForm\Exceptions\SnipFormException;
try {
$rows = $snipform->signals()->last7Days()->sessions()->all();
} catch (AuthenticationException $e) {
// token revoked, expired, or missing signals:read
} catch (ApiException $e) {
$e->status; // int - HTTP status
$e->errors; // ['field' => ['message', ...]] for 422s, otherwise []
$e->body; // the full response body
} catch (SnipFormException $e) {
// network, timeout, malformed response
}

Validation errors are folded into the message, so a 422 reads as The given data was invalid. period: must be one of ... in the stack trace without unpacking $e->errors.

  • Querying Signals: periods, clauses, sessions, metrics, graph and live.
  • Resources: events, contacts, conversions, short links, attribution.
  • Laravel: the facade, session-id delivery, dispatch modes, auto-identify.