Skip to content
dhanu docs
Browse pages

SDKs

PHP SDK and Laravel

The official dhanu/dhanu package, the API as plain PHP with a Laravel service provider and facade.

composer require dhanu/dhanu
use Dhanu\Dhanu;

$dhanu = new Dhanu(getenv('DHANU_API_KEY'));

$text = $dhanu->text->generate('Write a haiku about Kathmandu');
echo $text['text'];

$task = $dhanu->tasks->create('Summarise this week\'s tickets', staffId: $staffId);
$page = $dhanu->tasks->messages($task['id'], limit: 50);

$dhanu->text->stream('Explain Bikram Sambat', fn (string $delta) => print($delta));

$file = $dhanu->files->upload(file_get_contents('notes.txt'), 'notes.txt', 'text/plain');
$job = $dhanu->jobs->wait($jobId);
$event = $dhanu->webhooks->verify($rawBody, $request->header('Dhanu-Signature'), getenv('DHANU_WEBHOOK_SECRET'));

Responses are associative arrays with the API’s field names. Failures throw Dhanu\DhanuException with errorCode, status, getMessage() and requestId. Network errors, 408, 429 and 5xx are retried twice honouring Retry-After, and JSON POSTs carry a generated Idempotency-Key. PHP 8.1 or newer; needs only ext-curl and ext-json.

Laravel

The package is auto-discovered. Put DHANU_API_KEY (and optionally DHANU_BASE_URL, DHANU_WEBHOOK_SECRET) in .env, or publish the config:

php artisan vendor:publish --tag=dhanu-config

Then resolve the singleton anywhere, or use the facade for me():

use Dhanu\Dhanu;
use Dhanu\Laravel\Facades\Dhanu as DhanuFacade;

$reply = app(Dhanu::class)->text->generate('Hello');
$me = DhanuFacade::me();

A webhook route in Laravel:

Route::post('/hooks/dhanu', function (Request $request) {
    $event = app(Dhanu::class)->webhooks->verify($request->getContent(), $request->header('Dhanu-Signature'), config('dhanu.webhook_secret'));
    ProcessDhanuEvent::dispatch($event);
    return response()->noContent(200);
});