RafikiDB PHP SDK#

sdk-php

Use when writing PHP or Laravel code with the RafikiDB PHP SDK (rafikidb/rafikidb on Packagist, repo nasrtechindustry/rafikidb-php). Covers RafikiDB::create, DataBuilder filters/joins/cursor pagination, auth sessions, realtime, storage, env, secrets, webhooks, functions, payments, and Laravel facade/guard integration.

You are working with the rafikidb/rafikidb composer package (PHP 8.1+, only ext-curl and ext-json required). Source lives in the dedicated repo nasrtechindustry/rafikidb-php. Install: composer require rafikidb/rafikidb.

Setup#

use RafikiDB\RafikiDB;

$db = RafikiDB::create(
    projectId: '5c73dab0-...',
    apiKey: 'raf_live_...',
    baseUrl: 'https://api.rafikidb.com/api/v1', // optional
);
PHP
  • Every call returns an Envelope with success, data, message, code, errors.
  • Failures throw RafikiDBException with status, errorCode and errors.
  • Works in vanilla PHP and any framework; Laravel extras are optional.

Data#

$rows = $db->from('profiles')
    ->select('id, nationality')
    ->eq('nationality', 'Tanzania')
    ->gt('age', 18)
    ->in('region', ['dar', 'mba'])
    ->order('created_at', ascending: false)
    ->limit(25)
    ->execute();

// Filters: eq, neq, gt, gte, lt, lte, like, ilike, isNull, isNotNull, in
// Pagination: limit, offset, cursor($lastId) (keyset, large tables)
// Single row: ->get($id); first row or null: ->single(); count: ->head()
PHP

Joins (no SQL, via query engine)#

use RafikiDB\JoinSpec;

$rows = $db->from('messages')
    ->select('id, message, users.full_name')
    ->join(new JoinSpec(
        table: 'users',
        fromColumn: 'user_id',
        toColumn: 'id',
        type: 'LEFT JOIN', // INNER | LEFT | RIGHT | FULL
    ))
    ->limit(20)
    ->execute();
PHP

Joined columns appear as {table}.{column}. Joins route through POST /projects/{id}/query/run.

Writes#

$db->from('profiles')->insert([
    'full_name' => 'Asha Mwinyi',
    'nationality' => 'Tanzania',
]); // single array or list of arrays

$db->from('profiles')->eq('id', $id)->update(['nationality' => 'Kenya']);
$db->from('profiles')->eq('id', $id)->delete();
PHP

update() / delete() execute immediately, so they require an eq('id', ...) filter set before the call, else they throw.

Auth (project users, not dashboard)#

$db->auth->signup('asha@example.com', 'strong-pass1', 'Asha Mwinyi', '+255712345678');
$session = $db->auth->login('asha@example.com', 'strong-pass1'); // alias: signin

$db->auth->otpRequest('+255712345678');
$db->auth->otpVerify('+255712345678', '123456', 'Asha Mwinyi');

$db->auth->resetPassword('asha@example.com');
$db->auth->confirmResetPassword('asha@example.com', '123456', 'brand-new-pass1');

$db->auth->signOut();
$db->auth->refresh($refreshToken);
$db->auth->logout($refreshToken);
PHP
  • Login stores the session on the client; every request then carries the Bearer token so RLS sees the authenticated user.
  • The SDK keeps the session in memory only. To persist across requests, save json_encode($db->client->session()) yourself and restore with $db->client->setSession($decoded).

Realtime#

$sub = $db->realtime->subscribe(
    'messages',
    function (array $event): void {
        // $event['type'] => INSERT | UPDATE | DELETE
        // $event['record'] => the changed row
    },
    ['events' => ['INSERT', 'DELETE']], // optional filter
);
$sub->run();   // blocks; run in a worker process, call ->close() to stop
PHP

Modules#

$db->storage->createBucket(name: 'avatars', slug: 'avatars', isPublic: true);
$db->storage->signedUploadUrl(bucketId: $bucket['id'], objectName: 'user-1.png');

$db->env->set('STRIPE_KEY', 'sk_test_123', environment: 'production');
$db->env->bulkSet('development', ['DEBUG' => 'true']);

$db->secrets->set(key: 'API_SECRET', value: 's3cr3t');
$db->secrets->reveal($secretId);

$db->webhooks->create(name: 'order.created', url: 'https://myapp.com/hooks/orders', events: ['order.created']);
$db->webhooks->listDeliveries($webhookId);

$db->functions->create(name: 'hello', code: '...');
$db->functions->deploy($fnId);
$db->functions->invoke($fnId, body: '{"name":"Asha"}');

$db->payments->stkPush(phone: '+255712345678', amount: 5000, description: 'Order #123');
$db->payments->snippeStatus($reference);
$db->payments->listTransactions();
PHP

Laravel#

  • Auto-discovered provider + facade + config + auth guard.
  • php artisan vendor:publish --tag=rafikidb-config, then .env: RAFIKIDB_PROJECT_ID, RAFIKIDB_API_KEY, RAFIKIDB_URL, RAFIKIDB_AUTH_GUARD=true.
use RafikiDB\Facades\RafikiDB;

$rows = RafikiDB::from('messages')->select('id, message')->limit(10)->execute();
PHP
  • Auth guard: register 'rafikidb' => ['driver' => 'session', 'provider' => 'rafikidb'] in config/auth.php guards and 'rafikidb' => ['driver' => 'rafikidb'] in providers. Login through RafikiDB::auth()->login(...), then auth()->user() resolves the project user in the same request. The guard reads the SDK session; it does not verify passwords itself.

Gotchas#

  • update/delete throw unless an eq('id', ...) filter is set first.
  • Dashboard login is a separate endpoint (/auth/login) - not for app users.
  • composer.lock is not committed in the package; vendor/ is gitignored.