Open In App

OneSignal API Integration using Node.js

We generally use OneSignal API to deliver notifications, email, export data, create and delete segments, etc. We will be using onesignal-node module to integrate OneSignal API in NodeJS. The onesignal-node is a Node.js client library for OneSignal API and will be helpful in enabling us for doing so.

Steps to integrate



We use OneSignal.Client for actions likes to create a notification, add a device, CSV export, create a segment, etc. For this, we need to first get our appId and apikey. For this, follow the following steps:

  1. Open your OneSignal Account and go to settings.
  2. Open the Keys & IDs tab.
  3. You can access your OneSignal app ID and apikey here.

After this, use the Id and key in your OneSignal.Client method as shown below:




// With default options
const client = new OneSignal.Client('appId', 'apiKey');
  
// With custom API endpoint
const client = new OneSignal.Client('appId', 'apiKey'

For actions like view apps, update an app, create an app. etc, we use OneSignal.UserClient. For this, we need to acquire the User Auth key:

User Auth Key which can be found in Account manages operations outside an app. Click on the Account dropdown on the top right of your dashboard and scroll down to the User Auth Key section. It is past the section that displays each of your app’s auth keys.

Use this in the OneSignal.UserClient method as shown below:




// With default options
const userClient = new OneSignal.UserClient('userAuthKey');
  
// With custom API endpoint
const userClient = new OneSignal.UserClient('userAuthKey'

Now, the OneSignal API is integrated with NodeJs, you can use this for creating, canceling, and viewing notifications, creating, viewing, and updating apps, view, add and edit devices, create and delete segments, etc.

Reference:


Article Tags :