IBM Cloud Event Notifications Node.js SDK
Node.js client library to interact with various Event Notifications APIs.
Table of Contents
Overview
The IBM Cloud Event Notifications Node.js SDK allows developers to programmatically interact with the Event Notifications service in IBM cloud.
Service Name | Import Path |
---|
Event-Notifications | @ibm-cloud/event-notifications-node-admin-sdk/event-notifications/v1 |
Prerequisites
- You need an IBM Cloud account.
- Node.js >=12: This SDK is tested with Node.js versions 12 and up. It may work on previous versions but this is not officially supported.
Installation
npm install @ibm-cloud/event-notifications-node-admin-sdk
Using the SDK
For general SDK usage information, please see
this link
Initialize SDK
Initialize the sdk to connect with your Event Notifications service instance.
import { EventNotificationsV1 } from '@ibm-cloud/event-notifications-node-admin-sdk/event-notifications/v1';
import { IamAuthenticator } from '@ibm-cloud/event-notifications-node-admin-sdk/auth';
const authenticator = new IamAuthenticator({
apikey: <apikey>,
});
const initParameters = {
authenticator,
serviceUrl: "https://" + region + ".event-notifications.cloud.ibm.com/event-notifications"
}
const eventNotificationsService = EventNotificationsV1.newInstance(initParameters);
- region : Region of the Event Notifications Instance
Using the SDK
SDK Methods to consume
Source
Create Source
const params = {
instanceId: <instance-id>,
name: '<source-name>',
description: '<source-description>',
enabled: false,
};
let res;
try {
res = await eventNotificationsService.createSources(params);
console.log(JSON.stringify(res.result, null, 2));
sourceId = res.result.id;
} catch (err) {
console.warn(err);
}
List Sources
const params = {
instanceId: <instance-id>,
};
eventNotificationsService
.listSources(params)
.then((res) => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch((err) => {
console.warn(err);
});
Get Source
const params = {
instanceId: <instance-id>,
id: <source-id>,
};
eventNotificationsService
.getSource(params)
.then((res) => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch((err) => {
console.warn(err);
});
Update Source
const params = {
instanceId: <instance-id>,
id: <sourceId>,
name: '<source-updated-name>',
description: '<source-updated-description>',
enabled: true,
};
let res;
try {
res = await eventNotificationsService.updateSource(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
Delete Source
const params = {
instanceId: <instance-id>,
id: <sourceId>,
};
try {
await eventNotificationsService.deleteSource(params);
} catch (err) {
console.warn(err);
}
Topics
Create Topic
const rulesModel = {
enabled: false,
event_type_filter: "$.notification_event_info.event_type == 'cert_manager'",
notification_filter: "$.notification.findings[0].severity == 'MODERATE'",
};
const topicUpdateSourcesItemModel = {
id: <source-id>,
rules: [rulesModel],
};
const params = {
instanceId: <instance-id>,
name: <topic-name>,
description: <topic-description>,
sources: [topicUpdateSourcesItemModel],
};
eventNotificationsService
.createTopic(params)
.then((res) => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch((err) => {
console.warn(err);
});
List Topics
const params = {
instanceId: <instance-id>,
};
eventNotificationsService
.listTopics(params)
.then((res) => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch((err) => {
console.warn(err);
});
Get Topic
const params = {
instanceId: <instance-id>,
id: <topic-id>,
};
eventNotificationsService
.getTopic(params)
.then((res) => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch((err) => {
console.warn(err);
});
Update Topic
const rulesModel = {
enabled: true,
event_type_filter: "$.notification_event_info.event_type == 'cert_manager'",
notification_filter: "$.notification.findings[0].severity == 'MODERATE'",
};
const topicUpdateSourcesItemModel = {
id: <source-id>,
rules: [rulesModel],
};
const params = {
instanceId: <instance-id>,
id: <topic-id>,
name: <topic-update-name>,
sources: [topicUpdateSourcesItemModel],
};
eventNotificationsService
.replaceTopic(params)
.then((res) => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch((err) => {
console.warn(err);
});
Delete Topic
const params = {
instanceId : <instance-id>,
id : <topic-id>,
}
eventNotificationsService
.deleteTopic(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Destinations
Create Destination
const destinationConfigParamsModel = {
url: <destination-config-url>,
verb: <destination-config-verb>,
custom_headers: { <header-key>: <header-value> },
sensitive_headers: [<header-key>],
};
const destinationConfigModel = {
params: destinationConfigParamsModel,
};
const params = {
instanceId: <instance-id>,
name: <destination-name>,
type: <destination-type>,
description: <destination-description>,
config: destinationConfigModel,
};
eventNotificationsService.createDestination(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
List Destinations
const params = {
instanceId : <instance-id>,
}
eventNotificationsService.listDestinations(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Get Destination
const params = {
instanceId : <instance-id>,
id : <destination-id>,
}
eventNotificationsService
.getDestination(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Update Destination
const destinationConfigParamsModel = {
url: <destination-config-update-url>,
verb: <destination-config-update-verb>,
custom_headers: { <header-key>: <header-value> },
sensitive_headers: [<header-key>],
};
const destinationConfigModel = {
params: destinationConfigParamsModel,
};
const params = {
instanceId: <instance-id>,
id: <destination-id>,
name: <destination-update-name>,
description: <destination-update-description>,
config: destinationConfigModel,
};
eventNotificationsService.updateDestination(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Delete Destination
const params = {
instanceId : <instance-id>,
id : <destination-id>,
}
eventNotificationsService
.deleteDestination(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Destination Devices
List Destination device
const params = {
instanceId: <instance-id>,
id: <destination-id>,
};
let res;
try {
res = await eventNotificationsService.listDestinationDevices(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
Get Destination device report
const params = {
instanceId: <instance-id>,
id: <destination-id>,
};
let res;
try {
res = await eventNotificationsService.getDestinationDevicesReport(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
Create Destination tag subscription
const params = {
instanceId: <instance-id>,
id: <destination-id>,
deviceId: <device-id>,
tagName: <tag-name>,
};
let res;
try {
res = await eventNotificationsService.createTagsSubscription(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
List Destination tag subscription
const params = {
instanceId: <instance-id>,
id: <destination-id>,
};
let res;
try {
res = await eventNotificationsService.listTagsSubscription(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
List Destination device tag subscriptions
const params = {
instanceId: <instance-id>,
id: <destination-id>,
deviceId: <device-id>,
};
let res;
try {
res = await eventNotificationsService.listTagsSubscriptionsDevice(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
Delete Destination device tag subscription
const params = {
instanceId: <instance-id>,
id: <destination-id>,
deviceId: <device-id>,
tagName: <tag-name>
};
try {
await eventNotificationsService.deleteTagsSubscription(params);
} catch (err) {
console.warn(err);
}
Subscriptions
Create Subscription
While Creating Subscription use any of one option from webhook or email
const subscriptionCreateAttributesModel = {
signing_enabled: false,
};
const params = {
instanceId: <instance-id>,
name: <subscription-name>,
destinationId: <destination-id>,
topicId: <topic-id>,
attributes: subscriptionCreateAttributesModel,
description: <subscription-description>,
};
eventNotificationsService
.createSubscription(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
List Subscriptions
const params = {
instanceId : <instance-id>,
}
eventNotificationsService
.listSubscriptions(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Get Subscription
const params = {
instanceId : <instance-id>,
id : <subscription-id>,
}
eventNotificationsService.
getSubscription(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Update Subscription
const subscriptionUpdateAttributesModel = {
signing_enabled: true,
};
const params = {
instanceId: <instance-id>,
id: <subscription-id>,
name: <subscription-update-name>,
description: <subscription-update-description>,
attributes: subscriptionUpdateAttributesModel,
};
eventNotificationsService
.updateSubscription(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Delete Subscription
const params = {
instanceId : <instance-id>,
id : <subscription-id>,
}
eventNotificationsService
.deleteSubscription(params)
.then(res => {
console.log(JSON.stringify(res.result, null, 2));
})
.catch(err => {
console.warn(err);
});
Send Notifications
const notificationDevicesModel = {
user_ids: ['<user-ids>'],
fcm_devices: ['<fcm-device-ids>'],
apns_devices: ['<apns-device-ids>'],
tags: ['<tag-names>'],
platforms: ['<device-platforms>'],
};
const notificationApnsBodyModel = {
aps: {
alert: '<notification-message>',
badge: 5,
},
};
const notificationFcmBodyModel = {
notification: {
title: '<notification-title>',
badge: '<notification-message>',
},
};
const notificationApnsHeaders = {
"apns-collapse-id": "<apns-apns-collapse-id-value>"
}
let notificationID = "<notification-id>"
let notificationSubject = "<notification-subject>"
let notificationSeverity = "<notification-severity>"
let typeValue = "<notification-type>"
let notificationsSouce = "<notification-source>"
const params = {
instanceId: instanceId,
ceIbmenseverity: notificationSeverity,
ceId: notificationID,
ceSource: notificationsSouce,
ceIbmensourceid: sourceId,
ceType: typeValue,
ceTime: '<notification-time>',
ceIbmenpushto: JSON.stringify(notificationDevicesModel),
ceIbmenfcmbody: JSON.stringify(notificationFcmBodyModel),
ceIbmenapnsbody: JSON.stringify(notificationApnsBodyModel),
ceIbmenapnsheaders: JSON.stringify(apnsHeaders),
ceSpecversion: '1.0',
};
let res;
try {
res = await eventNotificationsService.sendNotifications(params);
console.log(JSON.stringify(res.result, null, 2));
} catch (err) {
console.warn(err);
}
Send Notifications Variables
- ceIbmenpushto - Set up the the push notifications tragets.
- user_ids (Array of String) - Send notification to the specified userIds.
- fcm_devices (Array of String) - Send notification to the list of specified Android devices.
- fcm_devices (Array of String) - Send notification to the list of specified iOS devices.
- _devices (Array of String) - Send notification to the list of specified Chrome devices.
- firefox_devices (Array of String) - Send notification to the list of specified Firefox devices.
- tags (Array of String) - Send notification to the devices that have subscribed to any of these tags.
- platforms (Array of String) - Send notification to the devices of the specified platforms.
- Pass 'G' for google (Android) devices.
- Pass 'A' for iOS devices.
- Pass 'WEB_FIREFOX' for Firefox browser.
- Pass 'WEB_CHROME' for Chrome browser.
- Event Notifications SendNotificationsOptions - Event Notifications Send Notifications method.
- InstanceID (String) - Event Notifications instance AppGUID.
- CeIbmenseverity (String) - Severity for the notifications.
- CeID (String) - ID for the notifications.
- CeSource (String) - Source of the notifications.
- CeIbmensourceid (String) - Event Notifications instance Source ID.
- CeType (String) - Type for the notifications.
- CeTime (String) - Time of the notifications.
- CeIbmenpushto (string) - Targets for the FCM notifications.
- CeIbmenfcmbody (string) - Set payload string specific to Android platform [Refer this FCM official link].
- CeIbmenapnsbody (string) - Set payload string specific to iOS platform [Refer this APNs official doc link].
- CeIbmenapnsheaders (string) - Set headers required for the APNs message [Refer this APNs official link(Table 1 Header fields for a POST request)]
- CeIbmenchromebody (string) - Message body for the Chrome notifications. Refer this official documentation for more.
- CeIbmenfirefoxbody (string) - Message body for the Firefox notifications. Refer this official documentation for more.
- CeIbmenchromeheaders (string) - Headers for the Chrome notifications. Refer this official documentation for more.
- CeIbmenfirefoxheaders (string) - Headers for the Firefox notifications. Refer this official documentation for more.
- CeSpecversion (String) - Spec version of the Event Notifications. Default value is
1.0
.
Set Environment
Find event_notifications_v1.env.hide in the repo and rename it to event_notifications_v1.env
. After that add the values for,
EVENT_NOTIFICATIONS_URL
- Add the Event Notifications service instance Url.EVENT_NOTIFICATIONS_APIKEY
- Add the Event Notifications service instance apikey.EVENT_NOTIFICATIONS_GUID
- Add the Event Notifications service instance GUID.
Optional
EVENT_NOTIFICATIONS_AUTH_URL
- Add the IAM url if you are using IBM test cloud.EVENT_NOTIFICATIONS_FCM_KEY
- Add firebase server key for Android FCM destination.EVENT_NOTIFICATIONS_FCM_ID
- Add firebase sender Id for Android FCM destination.
Questions
If you are having difficulties using this SDK or have a question about the IBM Cloud services,
please ask a question at
Stack Overflow.
Issues
If you encounter an issue with the SDK, you are welcome to submit
a bug report.
Before that, please search for similar issues. It's possible someone has
already encountered this issue.
Open source @ IBM
Find more open source projects on the IBM Github Page
Contributing
See CONTRIBUTING.
License
This project is released under the Apache 2.0 license.
The license's full text can be found in
LICENSE.