.NET Client SDK

The OneSignal .NET API Library allows for your backend .NET application to integrate with OneSignal for backend events, data, and more.

Installation

Install using Visual Studio

  1. In Solution Explore, right-click the References or a project and select Manage Nuget Packages...
  2. On the Browse tab Search for OneSignalApi. Select the OneSignalApi package from the list, then click Install.

Install using the Package Manager Console

  1. Open the project/solution in Visual Studio, and open the console using the Tools > NuGet Package Manager > Package Manager Console command.
  2. Run the install command
Install-Package OneSignalApi

Install using the dotnet CLI

  1. Open a command line and switch to the directory that contains your project file.
  2. Use the following command to install the OneSignalApi package to that project.
dotnet add package OneSignalApi

Usage

OneSignal has two authentication contexts, a separate client instance should be used for each context (see https://documentation.onesignal.com/reference to determine which endpoints require which authentication type)

  • AppAuth
  • UserAuth

AppAuth Example

var appConfig = new Configuration();
appConfig.BasePath = "https://onesignal.com/api/v1";
appConfig.AccessToken = "REST_API_KEY";
            
var appInstance = new DefaultApi(appConfig);

UserAuth Example

var appConfig = new Configuration();
appConfig.BasePath = "https://onesignal.com/api/v1";
appConfig.AccessToken = "USER_AUTH_KEY";
            
var appInstance = new DefaultApi(appConfig);

Advanced Example: Creating a brand new app

If creating a new app via the client, the response will return the app's API key via the basic_auth_key response parameter. You can then use this to modify your configuration object and create a new client that will ahve both user-level and app-level authentication set up.

// Configure with user_key Bearer token for authorization to access endpoints
// that require the OneSignal User Auth Key.
var userConfig = new Configuration();
userConfig.BasePath = "https://onesignal.com/api/v1";
userConfig.AccessToken = "YOUR_USER_AUTH_KEY";
var userInstance = new DefaultApi(userConfig);

// Create a new OneSignal application
var newApp = new App(name: "Sample App");
var newAppResult = await userInstance.CreateAppAsync(newApp);

// Configure a new API instance with app_key Bearer token for authorization to
// access endpoints that require the OneSignal App Auth Key.
var appConfig = new Configuration();
appConfig.BasePath = "https://onesignal.com/api/v1";
appConfig.AccessToken = newAppResult.BasicAuthKey;
var appInstance = new DefaultApi(appConfig);

// As an example, create a new player within the application just created
var player = new Player(appId: newAppResult.Id)
    {
        Identifier = "<A_DEVICE_IDENTIFIER>",
        DeviceType = 1
    };
await appInstance.CreatePlayerAsync(player);

API Reference

All URIs are relative to https://onesignal.com/api/v1. Each method has a synchronous version and an asynchronous version. The asynchronous versions will have Async appended to the method name.

ClassMethodHTTP requestDescription
DefaultApiCancelNotificationDELETE /notifications/{notification_id}Stop a scheduled or currently outgoing notification
DefaultApiCreateAppPOST /appsCreate an app
DefaultApiCreateNotificationPOST /notificationsCreate notification
DefaultApiCreatePlayerPOST /playersAdd a device
DefaultApiCreateSegmentsPOST /apps/{app_id}/segmentsCreate Segments
DefaultApiDeletePlayerDELETE /players/{player_id}Delete a user record
DefaultApiDeleteSegmentsDELETE /apps/{app_id}/segments/{segment_id}Delete Segments
DefaultApiExportPlayersPOST /players/csv_export?app_id={app_id}CSV export
DefaultApiGetAppGET /apps/{app_id}View an app
DefaultApiGetAppsGET /appsView apps
DefaultApiGetNotificationGET /notifications/{notification_id}View notification
DefaultApiGetNotificationHistoryPOST /notifications/{notification_id}/historyNotification History
DefaultApiGetNotificationsGET /notificationsView notifications
DefaultApiGetOutcomesGET /apps/{app_id}/outcomesView Outcomes
DefaultApiGetPlayerGET /players/{player_id}View device
DefaultApiGetPlayersGET /playersView devices
DefaultApiUpdateAppPUT /apps/{app_id}Update an app
DefaultApiUpdatePlayerPUT /players/{player_id}Edit device
DefaultApiUpdatePlayerTagsPUT /apps/{app_id}/users/{external_user_id}Edit tags with external user id

Did this page help you?