Typescript client for OneSignal Server Rest API
npm install onesignal-api-client-coreNpm
``sh`
npm install onesignal-api-client-core
Yarn
`sh`
yarn add onesignal-api-client-core
#### Create notification through the Segment
API Reference
Segments are the most common way developers send notifications via OneSignal. Sending to segments is easy: you simply specify which segments you want to send to, and, optionally, which ones you don't.
`typescript
import { OneSignalAppClient, NotificationBySegmentBuilder } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const input = new NotificationBySegmentBuilder()
.setIncludedSegments(['Active Users', 'Inactive Users'])
.notification() // .email()
.setContents({ en: 'My Message' })
.build();
const result = await client.createNotification(input);
`
#### Create notification through the Filter
API Reference
Filters are a powerful way to target users, allowing you to use both data that OneSignal has about a user and any Tags your app may send OneSignal. Filters can be combined together to form advanced, highly precise user targeting. OneSignal customers use all sorts of filters to send notifications, including language, location, user activity, and more.
`typescript
import { OneSignalAppClient, NotificationByFilterBuilder } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const filters = [
{ 'field': 'tag', 'key': 'level', 'relation': '>', 'value': '10' },
{ 'field': 'amount_spent', 'relation': '>', 'value': '0' },
];
const input = new NotificationByFilterBuilder()
.setFilters(filters)
.notification() // .email()
.setContents({ en: 'My Message' })
.build();
const result = await client.createNotification(input);
`
#### Create notification through the Device
API Reference
You may also target specific devices with the create notification method. Targeting devices is typically used in two ways:
- For notifications that target individual users, such as if they've received a message from someone.
- For apps that wish to manage their own segments, such as tracking a user's followers and sending notifications to them when that user posts.
`typescript
import { OneSignalAppClient, NotificationByDeviceBuilder } from 'onesignal-api-client-core';
const client = new OneSignalAppClient('appId', 'restApiKey');
const input = new NotificationByDeviceBuilder()
.setIncludeExternalUserIds(['externalUserId1', 'externalUserId2'])
.notification() // .email()
.setContents({ en: 'My Message' })
.build();
const result = await client.createNotification(input);
`
typescript
import { OneSignalAppClient, ICancelNotificationInput } from 'onesignal-api-client-core';const client = new OneSignalAppClient('appId', 'restApiKey');
const input = { id: notification.id } as ICancelNotificationInput;
const result = await client.cancelNotification(input);
`$3
API Reference
`typescript
import { OneSignalAppClient, ICreateDeviceInput, IViewDeviceInput, IUpdateDeviceInput, DeviceType } from 'onesignal-api-client-core';const client = new OneSignalAppClient('appId', 'restApiKey');
const createInput = { device_type: DeviceType.ChromeWebPush } as ICreateDeviceInput;
const createResult = await client.createDevice(input);
const viewInput = { id: device.id } as IViewDeviceInput;
const viewResult = await client.viewDevice(viewInput);
const viewsResult = await client.viewDevices();
const updateInput = { id: device.id, device_type: DeviceType.Android } as IUpdateDeviceInput;
const updateResult = await client.updateDevice(input);
`$3
API Reference
`typescript
import { OneSignalUserClient, ICreateAppInput, IViewAppInput, IUpdateAppInput } from 'onesignal-api-client-core';const client = new OneSignalUserClient('userAuthKey');
const createInput = { name } as ICreateAppInput;
const createResult = await client.createApp(input);
const viewInput = { id: app.id } as IViewAppInput;
const viewResult = await client.viewApp(viewInput);
const viewsResult = await client.viewApps();
const updateInput = { id: app.id, name: 'Updated name' } as IUpdateAppInput;
const updateResult = await client.updateApp(input);
`$3
API Reference
`typescript
import { OneSignalAppClient, ITrackOpenInput } from 'onesignal-api-client-core';const client = new OneSignalAppClient('appId', 'restApiKey');
const trackOpenInput = { notificationId: notification.id, opened: true } as ITrackOpenInput;
const trackOpenResult = await client.trackOpen(input);
`$3
API Reference
`typescript
import { OneSignalAppClient, INewSessionInput } from 'onesignal-api-client-core';const client = new OneSignalAppClient('appId', 'restApiKey');
const newSessionInput = { deviceId: device.id } as INewSessionInput;
const newSessionResult = await client.newSession(input);
``If any bugs are found in the API wrapper, please open an issue on GitHub, or a Pull Request if you want to fix it yourself!
Please be as explicit as possible and provide a minimum reproducing repository if at all possible, as it helps track down what went wrong.
All documentation for this wrapper comes from the Server Rest API,
if there are any typos, please let me know or open a PR to fix it.