Docs › SDK & CLI › PHP

PHP SDK

Full reference for the NotiLens PHP SDK. Requires PHP 8.0+.

Installation

Install

composer require notilens/notilens
Setup

Initialize

use NotiLens\NotiLens;

$nl = NotiLens::init('my-app', token: 'YOUR_TOKEN', secret: 'YOUR_SECRET');

// Or use environment variables: NOTILENS_TOKEN / NOTILENS_SECRET
$nl = NotiLens::init('my-app');

$scraper = NotiLens::init('scraper', token: 'TOKEN_A', secret: 'SECRET_A');
$mailer  = NotiLens::init('mailer',  token: 'TOKEN_B', secret: 'SECRET_B');
Notify

Notify

Real-world uses: fire when an e-commerce order lands, a scheduled report finishes, or a system threshold is hit.

$nl->notify('order.placed', 'Order #1234');
$nl->notify('disk.space.full', 'Only 1GB left', level: 'warning');
$nl->notify('report.ready', 'Report ready', downloadUrl: 'https://example.com/r.pdf');
Task Lifecycle

Task Events

Real-world uses: wrap an AI research agent (start → progress → complete/fail), a nightly invoice import (loop per batch → complete), or a user-triggered export (start → output_generated → complete).

$run = $nl->task('email');
$run->queue();
$run->start();

$run->progress('Fetching records...');
$run->loop('Processing batch 2');
$run->retry();
$run->pause('Rate limited');
$run->resume('Resuming');
$run->wait('Awaiting tool');
$run->error('Non-fatal error');

$run->complete('All done');
$run->fail('Unrecoverable');
$run->timeout('Exceeded 30s');
$run->cancel('User cancelled');
$run->terminate('OOM');
Metrics

Metrics

Real-world uses: track LLM token spend across a multi-step run. Monitor records in a nightly ETL — NotiLens alerts if count drops vs baseline. Watch API error rate across runs.

$run->metric('tokens', 350);
$run->metric('tokens', 210);   // now 560
$run->metric('cost', 0.0012);
$run->metric('model', 'gpt-4');

$run->resetMetrics('tokens');
$run->resetMetrics();
Full Example

Complete Working Example

use NotiLens\NotiLens;

$nl  = NotiLens::init('summarizer', token: 'TOKEN', secret: 'SECRET');
$run = $nl->task('report');
$run->start();

try {
    $run->progress('Fetching PDF');
    $result = $llm->complete($prompt);
    $run->metric('tokens', $result->usage->totalTokens);
    $run->metric('cost', $result->usage->cost);
    $run->outputGenerated('Summary ready');
    $run->complete('Done');
} catch (\Throwable $e) {
    $run->fail($e->getMessage());
}
force_send

force_send Parameter

Controls whether a notification bypasses ML-based filtering (true) or routes through it (false).

MethodDefault
notify() · fail() · timeout() · terminate()true
inputRequired() · outputGenerated() · outputFailed()true
All other task lifecycle methodsfalse — ML filtered

Override

$run->progress("Fetching...", forceSend: true);
$run->fail("Error", forceSend: false);
$nl->notify("order.placed", "Done", forceSend: false);