SDK Node.js – SoftAfrik Push API
Client Node.js officiel pour interagir avec SoftAfrik Push API : utilisateurs, devices, notifications push.
1. Installation
Le SDK Node.js est livré comme un module autonome. Vous pouvez le copier dans votre projet ou utiliser le ZIP fourni.
/your-project
/sdk
/node
softafrik-push.js
README.md
examples/
basic_push.js
NPM (optionnel)
Tu pourras publier plus tard sur NPM si tu veux.
npm install ./sdk/node
2. Classe Node.js officielle
Voici l’implémentation officielle du client Node.js. À placer dans sdk/node/softafrik-push.js.
// sdk/node/softafrik-push.js
import fetch from "node-fetch";
export default class SoftAfrikPushClient {
constructor(apiKey, baseUrl = "https://softafrik-push-api.brillanciel.com") {
this.apiKey = apiKey;
this.baseUrl = baseUrl.replace(/\/$/, "");
}
async registerUser(externalUserRef, meta = {}) {
return this._post("/api/users/register", {
external_user_ref: externalUserRef,
meta: meta,
});
}
async registerDevice(puid, appName, platform, deviceToken, deviceUid = null, lang = null, country = null) {
const payload = {
puid,
app_name: appName,
platform,
device_token: deviceToken,
};
if (deviceUid) payload.device_uid = deviceUid;
if (lang) payload.lang = lang;
if (country) payload.country = country;
return this._post("/api/devices/register", payload);
}
async unregisterDevice(deviceToken) {
return this._post("/api/devices/unregister", {
device_token: deviceToken,
});
}
async pushToUser(puid, title, body, data = {}) {
return this._post("/api/push/to-user", {
puid,
title,
body,
data,
});
}
async pushToDevice(deviceToken, title, body, data = {}) {
return this._post("/api/push/to-device", {
device_token: deviceToken,
title,
body,
data,
});
}
async broadcast(appName, country, title, body, data = {}) {
const payload = { title, body, data };
if (appName) payload.app_name = appName;
if (country) payload.country = country;
return this._post("/api/push/broadcast", payload);
}
async _post(path, payload) {
const res = await fetch(this.baseUrl + path, {
method: "POST",
headers: {
"X-API-KEY": this.apiKey,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
const text = await res.text();
try {
return JSON.parse(text);
} catch {
throw new Error("Invalid JSON response: " + text);
}
}
}
3. Exemple complet d’utilisation
// sdk/node/examples/basic_push.js
import SoftAfrikPushClient from "../softafrik-push.js";
const client = new SoftAfrikPushClient("VOTRE_API_KEY");
// 1) Enregistrer un utilisateur → PUID
const user = await client.registerUser("user_12345", {
full_name: "John Doe",
email: "john@example.com",
});
console.log("USER:", user);
const puid = user.puid;
// 2) Enregistrer device
const device = await client.registerDevice(
puid,
"MyFintechApp",
"android",
"AAAAxxxxx",
"device-uuid-123",
"fr",
"TG"
);
console.log("DEVICE:", device);
// 3) Push vers utilisateur
const resp = await client.pushToUser(
puid,
"Transfert reçu",
"Vous avez reçu 20 000 XOF",
{ transaction_id: 9932 }
);
console.log("PUSH:", resp);
Rappel des endpoints
POST /api/users/registerPOST /api/devices/registerPOST /api/devices/unregisterPOST /api/push/to-userPOST /api/push/to-devicePOST /api/push/broadcast
Essai rapide
import SoftAfrikPushClient from "./softafrik-push.js";
const client = new SoftAfrikPushClient("VOTRE_API_KEY");
const resp = await client.pushToUser(
"sa_1001_xxxxxxxx",
"Ping",
"Hello from SoftAfrik Push API"
);
console.log(resp);