PHP SDK - Installation#

rafikidb/rafikidb is a typed client for the RafikiDB API: data, auth, realtime, storage, environment variables, secrets, webhooks, edge functions and payments.

  • PHP 8.1+, only ext-curl and ext-json required
  • Laravel-native: auto-discovered service provider, facade, config and auth guard
  • Same envelope and error model as the REST API

Install#

composer require rafikidb/rafikidb
Bash

Initialize#

use RafikiDB\RafikiDB;

$db = RafikiDB::create(
    projectId: '5c73dab0-...',
    apiKey: 'raf_live_...',
    baseUrl: 'https://api.rafikidb.com/api/v1', // optional
);
PHP
OptionTypeDescription
projectIdstringRequired. Used by auth/realtime URLs
apiKeystringRequired. Sent as X-AFRIBASE-API-Key
baseUrl?stringDefault http://localhost:8080/api/v1

Responses#

Every call returns the API envelope unchanged:

$rows = $db->from('profiles')->select('id')->limit(10)->execute();

$rows->success; // true
$rows->message; // "Request was successful"
$rows->data;    // array of rows
$rows->errors;  // []
PHP

Failures throw RafikiDBException:

use RafikiDB\RafikiDBException;

try {
    $db->from('profiles')->insert([]);
} catch (RafikiDBException $e) {
    $e->getMessage(); // "Nationality is required"
    $e->status;       // 400
    $e->errorCode;    // "invalid_input"
    $e->errors;       // ["Nationality is required"]
}
PHP

Laravel#

The service provider is auto-discovered. Publish the config and set .env:

php artisan vendor:publish --tag=rafikidb-config
Bash
RAFIKIDB_PROJECT_ID=5c73dab0-...
RAFIKIDB_API_KEY=raf_live_...
RAFIKIDB_URL=http://localhost:8080/api/v1
RAFIKIDB_AUTH_GUARD=true
Bash

Use the facade:

use RafikiDB\Facades\RafikiDB;

$rows = RafikiDB::from('messages')->select('id, message')->limit(10)->execute();
PHP

Auth guard#

Register the guard and provider in config/auth.php:

'guards' => [
    'rafikidb' => ['driver' => 'session', 'provider' => 'rafikidb'],
    // ...existing guards
],
'providers' => [
    'rafikidb' => ['driver' => 'rafikidb'],
    // ...existing providers
],
PHP

Sign users in through the SDK and let the guard resolve them:

$result = RafikiDB::auth()->login($request->email, $request->password);

if ($result->success) {
    $request->session()->regenerate();
    return redirect()->intended('/dashboard');
}

$user = auth()->user();   // the project user
$user->full_name;         // magic accessors
$user->rafikidbUser();    // the raw user array
PHP

The guard reads the SDK session; it does not verify passwords itself.

Next: Data.