Files

89 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

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
*/
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 = {
"Content-Type": "application/json",
2026-08-19 12:00:05 +05:30
Authorization: `Bearer ${API_KEY}`,
};
async function main() {
console.log("🚀 Starting Notifkit example...\n");
2026-08-19 12:00:05 +05:30
try {
// 1. Sync Templates (Configure how notifications should look)
console.log("1️⃣ Syncing email template...");
2026-08-19 12:00:05 +05:30
const templateRes = await fetch(`${API_BASE}/templates`, {
method: "PUT",
2026-08-19 12:00:05 +05:30
headers,
body: JSON.stringify({
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
});
console.log("Template Response:", await templateRes.json());
2026-08-19 12:00:05 +05:30
console.log();
// 2. Create a User (Register a recipient)
console.log("2️⃣ Registering a user...");
2026-08-19 12:00:05 +05:30
const userRes = await fetch(`${API_BASE}/users`, {
method: "POST",
2026-08-19 12:00:05 +05:30
headers,
body: JSON.stringify({
id: "user_123",
email: "test@example.com",
2026-08-19 12:00:05 +05:30
preferences: {
channels: { email: true },
},
}),
2026-08-19 12:00:05 +05:30
});
console.log("User Response:", await userRes.json());
2026-08-19 12:00:05 +05:30
console.log();
// 3. Send a Notification
console.log("3️⃣ Sending notification...");
2026-08-19 12:00:05 +05:30
const notifyRes = await fetch(`${API_BASE}/notify`, {
method: "POST",
2026-08-19 12:00:05 +05:30
headers,
body: JSON.stringify({
user: "user_123",
template: "welcome_email",
channels: ["email"],
2026-08-19 12:00:05 +05:30
data: {
name: "Alice",
},
}),
2026-08-19 12:00:05 +05:30
});
// 202 Accepted — queued, not delivered. Track it with
// GET /v1/notifications/{taskId}.
console.log("Notify Response:", await notifyRes.json());
console.log("\n✅ Example completed successfully!");
2026-08-19 12:00:05 +05:30
} catch (err) {
console.error("❌ Error executing example:", err.message);
2026-08-19 12:00:05 +05:30
}
}
main();