Skip to content
Related Articles

Related Articles

Improve Article
OneSignal API Integration using NodeJS
  • Last Updated : 26 Mar, 2021

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

  • Step 1: Make sure NodeJs is installed on your computer or in your project. In case you need to install it, you can do it from here.

  • Step 2: Open the terminal and initialize npm in your project.

    npm init

    After installing, the terminal asks for details of the project. Update them according to your need.



    Note: Press enter as you are done, it will show the following prompt.

  • Step 3: Install onesignal-node module using the following command.

    npm install onesignal-node --save
  • Step 4: Start using onesignal-node using the following code to require the module.

    const OneSignal = require('onesignal-node');
  • Step 5: Integrate your app to OneSignal using client actions.

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:

script.js




// 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:

script.js




// 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:

My Personal Notes arrow_drop_up
Recommended Articles
Page :