apipurse / apipurse-sdk
APIPurse real-time cost tracking SDK for PHP — a manual trackCompletion() client. Not a network proxy: nothing routes through APIPurse's servers.
Requires
- php: >=8.1
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Dependency-free PHP client for /api/track/completion — the real-time
tracking endpoint. Uses only file_get_contents() with a stream context —
no curl extension or Composer packages required. Requires PHP 8.1+ (for
readonly properties).
Verified end-to-end on 26 July 2026 (PHP 8.5): php -l passes and a
real trackCompletion() call against production
(https://apipurse-theta.vercel.app) returned ok=true with a real
cost_usd.
Why no automatic wrapOpenAI()/wrapAnthropic() here
The JavaScript/Python/Ruby SDKs offer automatic wrappers because those languages let you reassign a method on an existing object instance at runtime. PHP has no built-in equivalent (that needs the non-default Runkit extension) — so instead of a fragile workaround, this SDK gives you one line to call yourself, right after your own completion call.
Install
Not yet published to Packagist. Reference this path directly in your own
composer.json:
"repositories": [{ "type": "path", "url": "../sdk-php" }], "require": { "apipurse/apipurse-sdk": "*" }
Usage
<?php use Apipurse\ApiPurse; $apipurse = new ApiPurse( ingestToken: getenv("APIPURSE_INGEST_TOKEN"), providerId: getenv("APIPURSE_PROVIDER_ID"), baseUrl: "https://your-apipurse-domain.com" ); // Right after your own OpenAI/Anthropic call: $completion = $client->chat()->completions()->create($params); $result = $apipurse->trackCompletion( $completion->model, $completion->usage->promptTokens, // adjust to your SDK version's property names $completion->usage->completionTokens ); if (!$result->ok) { // log $result->error if you want visibility — trackCompletion() // already swallowed it instead of throwing, since $silent is true by default. }
Publishing to Packagist
Not done here — needs a Packagist.org account with this package's repo
registered. Tag a release (git tag v0.1.0) and Packagist picks it up
automatically once the repo is submitted.