NotificationAPI Node.js Server SDK
The NotificationAPI Node.js Server SDK provides a streamlined interface for integrating real-time and multi-channel notifications into server-side applications. This SDK allows developers to send various types of notifications, including in-app, email, SMS, and push notifications, by interacting with the NotificationAPI platform. Currently at version 2.8.0, the package appears to be actively maintained with frequent updates, indicated by its publishing history on npm. It abstracts the complexity of notification delivery, enabling developers to focus on application logic rather than the intricacies of different communication channels or provider APIs. The SDK ships with built-in TypeScript types, ensuring a robust development experience and improved code quality for TypeScript users. Key differentiators lie in the comprehensive notification platform it connects to, offering a centralized solution for notification management, templating, and delivery across multiple channels.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported
cause Attempting to import the NotificationAPI SDK using CommonJS `require()` syntax in an environment configured for ES Modules, or if the SDK is ESM-only.fixUse ES Module `import` syntax: `import { NotificationAPI } from 'notificationapi-node-server-sdk';` ensure your `package.json` has `"type": "module"` if using ESM for your project, or configure your TypeScript/bundler appropriately. -
TypeError: Cannot read properties of undefined (reading 'send')
cause The NotificationAPI client was not correctly initialized, or `clientId`/`clientSecret` were missing, leading to an undefined or improperly constructed object.fixVerify that `new NotificationAPI({ clientId, clientSecret })` is called with valid credentials and that the `notificationapi` object is available in scope before attempting to call its methods. -
API Error: Invalid credentials
cause The provided `clientId` or `clientSecret` are incorrect, expired, or lack the necessary permissions to perform the requested operation.fixDouble-check your `NOTIFICATIONAPI_CLIENT_ID` and `NOTIFICATIONAPI_CLIENT_SECRET` environment variables. Ensure they match the keys from your NotificationAPI dashboard and have the required permissions.
Warnings
- breaking Future major versions of this SDK may drop support for older Node.js LTS versions (e.g., Node.js 16 or 18 as Node.js 20+ becomes prevalent). Always check the release notes for minimum Node.js requirements before upgrading to a new major SDK version.
- gotcha API Keys (ClientId and ClientSecret) should never be hardcoded directly into your application's source code, especially in client-side bundles. Exposure of these keys can lead to unauthorized access to your NotificationAPI account.
- breaking Changes in the NotificationAPI platform's API or data models may necessitate updates to the SDK, potentially introducing breaking changes in method signatures or response structures. Always review the migration guide when upgrading major SDK versions.
- gotcha Incorrectly formatted notification payloads (e.g., missing required fields, invalid types) will result in API errors. The SDK may perform basic validation, but robust server-side validation is still essential.
Install
-
npm install notificationapi-node-server-sdk -
yarn add notificationapi-node-server-sdk -
pnpm add notificationapi-node-server-sdk
Imports
- NotificationAPI
const NotificationAPI = require('notificationapi-node-server-sdk');import { NotificationAPI } from 'notificationapi-node-server-sdk'; - NotificationAPI
import { NotificationApi } from 'notificationapi-node-server-sdk';import NotificationAPI from 'notificationapi-node-server-sdk';
- NotificationAPIClientConfig
import type { NotificationAPIClientConfig } from 'notificationapi-node-server-sdk';
Quickstart
import { NotificationAPI } from 'notificationapi-node-server-sdk';
const CLIENT_ID = process.env.NOTIFICATIONAPI_CLIENT_ID ?? '';
const CLIENT_SECRET = process.env.NOTIFICATIONAPI_CLIENT_SECRET ?? '';
if (!CLIENT_ID || !CLIENT_SECRET) {
console.error('Environment variables NOTIFICATIONAPI_CLIENT_ID and NOTIFICATIONAPI_CLIENT_SECRET must be set.');
process.exit(1);
}
const notificationapi = new NotificationAPI({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
async function sendExampleNotification() {
try {
const response = await notificationapi.send({
notificationId: 'user_welcome',
user: {
id: 'test-user-123',
email: 'test@example.com',
// Additional user properties like 'name', 'phone_number' can be added
},
mergeTags: {
firstName: 'John',
lastName: 'Doe',
welcomeMessage: 'Welcome to our service!',
},
});
console.log('Notification sent successfully:', response);
} catch (error) {
console.error('Failed to send notification:', error);
}
}
sendExampleNotification();