2026-08-19 12:00:05 +05:30
|
|
|
/**
|
|
|
|
|
* Example project demonstrating how to use Notifkit via its HTTP API — no SDK,
|
|
|
|
|
* just fetch. Any language that can POST JSON works the same way.
|
|
|
|
|
*
|
|
|
|
|
* Before running this, start a Notifkit server. The custom-server example is
|
|
|
|
|
* one:
|
|
|
|
|
* $ cd ../custom-server && node index.js
|
|
|
|
|
*
|
|
|
|
|
* To run this example:
|
|
|
|
|
* $ ADMIN_API_KEY=my_super_secret_admin_key npm start
|
|
|
|
|
*/
|
|
|
|
|
|
2026-09-14 23:14:11 +05:30
|
|
|
const API_BASE = process.env.NOTIFKIT_API_URL || "http://localhost:3000/v1";
|
|
|
|
|
const API_KEY = process.env.ADMIN_API_KEY || "my_super_secret_admin_key";
|
2026-08-19 12:00:05 +05:30
|
|
|
|
|
|
|
|
// Every /v1 route is authenticated. Only /health, /live, /ready and /metrics are not.
|
|
|
|
|
const headers = {
|
2026-09-14 23:14:11 +05:30
|
|
|
"Content-Type": "application/json",
|
2026-08-19 12:00:05 +05:30
|
|
|
Authorization: `Bearer ${API_KEY}`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function main() {
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("🚀 Starting Notifkit example...\n");
|
2026-08-19 12:00:05 +05:30
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 1. Sync Templates (Configure how notifications should look)
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("1️⃣ Syncing email template...");
|
2026-08-19 12:00:05 +05:30
|
|
|
const templateRes = await fetch(`${API_BASE}/templates`, {
|
2026-09-14 23:14:11 +05:30
|
|
|
method: "PUT",
|
2026-08-19 12:00:05 +05:30
|
|
|
headers,
|
|
|
|
|
body: JSON.stringify({
|
2026-09-14 23:14:11 +05:30
|
|
|
templates: [
|
|
|
|
|
{
|
|
|
|
|
id: "welcome_email",
|
|
|
|
|
channel: "email",
|
|
|
|
|
// The per-channel fields live under `content`.
|
|
|
|
|
content: {
|
|
|
|
|
subject: "Welcome to Notifkit, {{name}}!",
|
|
|
|
|
html: "<h1>Hello {{name}}</h1><p>Thanks for signing up!</p>",
|
|
|
|
|
text: "Hello {{name}}! Thanks for signing up!",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}),
|
2026-08-19 12:00:05 +05:30
|
|
|
});
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("Template Response:", await templateRes.json());
|
2026-08-19 12:00:05 +05:30
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
|
// 2. Create a User (Register a recipient)
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("2️⃣ Registering a user...");
|
2026-08-19 12:00:05 +05:30
|
|
|
const userRes = await fetch(`${API_BASE}/users`, {
|
2026-09-14 23:14:11 +05:30
|
|
|
method: "POST",
|
2026-08-19 12:00:05 +05:30
|
|
|
headers,
|
|
|
|
|
body: JSON.stringify({
|
2026-09-14 23:14:11 +05:30
|
|
|
id: "user_123",
|
|
|
|
|
email: "test@example.com",
|
2026-08-19 12:00:05 +05:30
|
|
|
preferences: {
|
2026-09-14 23:14:11 +05:30
|
|
|
channels: { email: true },
|
|
|
|
|
},
|
|
|
|
|
}),
|
2026-08-19 12:00:05 +05:30
|
|
|
});
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("User Response:", await userRes.json());
|
2026-08-19 12:00:05 +05:30
|
|
|
console.log();
|
|
|
|
|
|
|
|
|
|
// 3. Send a Notification
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("3️⃣ Sending notification...");
|
2026-08-19 12:00:05 +05:30
|
|
|
const notifyRes = await fetch(`${API_BASE}/notify`, {
|
2026-09-14 23:14:11 +05:30
|
|
|
method: "POST",
|
2026-08-19 12:00:05 +05:30
|
|
|
headers,
|
|
|
|
|
body: JSON.stringify({
|
2026-09-14 23:14:11 +05:30
|
|
|
user: "user_123",
|
|
|
|
|
template: "welcome_email",
|
|
|
|
|
channels: ["email"],
|
2026-08-19 12:00:05 +05:30
|
|
|
data: {
|
2026-09-14 23:14:11 +05:30
|
|
|
name: "Alice",
|
|
|
|
|
},
|
|
|
|
|
}),
|
2026-08-19 12:00:05 +05:30
|
|
|
});
|
|
|
|
|
// 202 Accepted — queued, not delivered. Track it with
|
|
|
|
|
// GET /v1/notifications/{taskId}.
|
2026-09-14 23:14:11 +05:30
|
|
|
console.log("Notify Response:", await notifyRes.json());
|
|
|
|
|
console.log("\n✅ Example completed successfully!");
|
2026-08-19 12:00:05 +05:30
|
|
|
} catch (err) {
|
2026-09-14 23:14:11 +05:30
|
|
|
console.error("❌ Error executing example:", err.message);
|
2026-08-19 12:00:05 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|