JavaScript SDK - Installation#
@rafikidb/sdk is a typed client for the RafikiDB API: data, auth, realtime,
storage, environment variables, secrets, webhooks, edge functions and
payments.
- Zero runtime dependencies (native
fetch) - ESM + CJS + TypeScript types
- Optional React hook (
@rafikidb/sdk/react) - Browser and Node.js (18+)
Install#
npm install @rafikidb/sdk
BashThe React useAuth hook is a subpath of the same package - do not install
it separately:
import { useAuth } from '@rafikidb/sdk/react';
TypeScriptInitialize#
import { createClient } from '@rafikidb/sdk';
const db = createClient({
projectId: '5c73dab0-...',
apiKey: 'raf_live_...',
baseUrl: 'https://api.rafikidb.com/api/v1', // optional
persistSession: true, // optional: survive refreshes
});
TypeScript| Option | Type | Description |
|---|---|---|
projectId | string | Required. Used by auth/realtime URLs |
apiKey | string | Required. Sent as X-AFRIBASE-API-Key |
baseUrl | string | Default http://localhost:8080/api/v1 |
persistSession | boolean | Save/restore the user session in localStorage |
fetch | typeof fetch | Custom fetch implementation |
Responses#
Every method returns the API envelope unchanged:
const res = await db.from('profiles').select().limit(10);
// { success: true, message: 'Request was successful', data: [...] }
TypeScriptFailures throw RafikiDBError:
import { RafikiDBError } from '@rafikidb/sdk';
try {
await db.from('profiles').insert({});
} catch (err) {
if (err instanceof RafikiDBError) {
err.message; err.status; err.code; err.errors;
JSON.stringify(err); // fully serializable
}
}
TypeScriptNext: Data.