We are pleased to announce that the OneSignal Python and PHP SDKs are now available. These new API client libraries support developers using OneSignal by facilitating integration with the OneSignal REST API with your backend events, data, and more. This release is part of our ongoing initiative to add server SDKs across various languages and technologies.

Python and PHP are two of the most popular programming languages and we're happy to support them!

The OneSignal Python API and OneSignal PHP API SDKs provide a faster way to get started using OneSignal REST API. These libraries have been generated using OpenApi Generator project with several modifications and fixes on our side.

Getting Started

You can find all the keys you need in your OneSignal dashboard.

  • To get the APP_ID:
    Go to https://app.onesignal.com/apps/ page, find your application, and open it. You can find the APP ID in the URL.
  • To get the APP_KEY_TOKEN:
    Go to https://app.onesignal.com/apps/<YOUR_APP_ID>/settings/keys_and_ids page.
    If you don't have a Rest API Key already generated, please generate a new one by clicking the "Generate New API Key" button.
  • To get the USER_KEY_TOKEN:
    Go to https://app.onesignal.com/profile page and scroll down to the "User Auth Key" section. If you don't have a key already generated, please generate a new one by clicking the "Generate New User Auth Key" button.

PHP Installation & Usage

Requirements

PHP 7.3 and later.

Composer

To install the bindings via Composer, add the following to composer.json:

{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/OneSignal/onesignal-php-api.git"
    }
  ],
  "require": {
    "OneSignal/onesignal-php-api": "*@dev"
  }
}

Then run composer install

Manual Installation

Download the files and include autoload.php:

<?php
require_once('/path/to/OneSignal/vendor/autoload.php');

Usage examples

Imports

use DateTime;
use onesignal\client\api\DefaultApi;
use onesignal\client\Configuration;
use onesignal\client\model\GetNotificationRequestBody;
use onesignal\client\model\Notification;
use onesignal\client\model\StringMap;
use onesignal\client\model\Player;
use onesignal\client\model\UpdatePlayerTagsRequestBody;
use onesignal\client\model\ExportPlayersRequestBody;
use onesignal\client\model\Segment;
use onesignal\client\model\FilterExpressions;
use PHPUnit\Framework\TestCase;
use GuzzleHttp;

Constants

const APP_ID = '<YOUR_APP_ID>';
const APP_KEY_TOKEN = '<YOUR_APP_KEY_TOKEN>';
const USER_KEY_TOKEN = '<YOUR_USER_KEY_TOKEN>';

Configure authorization

$config = Configuration::getDefaultConfiguration()
    ->setAppKeyToken(APP_KEY_TOKEN)
    ->setUserKeyToken(USER_KEY_TOKEN);

$apiInstance = new DefaultApi(
    new GuzzleHttp\Client(),
    $config
);

Notifications

Creating a notification model

function createNotification($enContent): Notification {
    $content = new StringMap();
    $content->setEn($enContent);

    $notification = new Notification();
    $notification->setAppId(APP_ID);
    $notification->setContents($content);
    $notification->setIncludedSegments(['Subscribed Users']);

    return $notification;
}

Sending a notification immediately

$notification = createNotification('PHP Test notification');

$result = $apiInstance->createNotification($notification);
print_r($result);

Scheduling a notification to be sent in 24 hours

$notification = self::createNotification('PHP Test scheduled notification');
$dt = new DateTime();
$dt->modify('+1 day');
$notification->setSendAfter($dt);

$scheduledNotification = $apiInstance->createNotification($notification);
print_r($scheduledNotification);

Canceling a scheduled notification

$cancelResult = $apiInstance->cancelNotification(APP_ID, $scheduledNotification->getId());
print_r($cancelResult->getSuccess());

Retrieving a notification

$scheduledNotification = $apiInstance->getNotification(APP_ID, $scheduledNotification->getId());
print_r($scheduledNotification);

Listing notifications by application ID

$getResult = $apiInstance->getNotifications(APP_ID);
print_r($getResult->getNotifications());

Getting notification history

$requestBody = new GetNotificationRequestBody();
$requestBody
    ->setAppId(APP_ID)
    ->setEvents('sent');

$getResult = $apiInstance->getNotificationHistory($scheduledNotification->getId(), $requestBody);
print_r($getResult->getSuccess());

For more examples please visit our GitHub repository.

Python SDK Installation & Usage

Installation

# pip
pip install onesignal-python-api.git

Creating a Client

Configuration

You can configure the client using the Configuration function.

import onesignal

configuration = onesignal.Configuration(configParams)

Initializing the Client

with onesignal.ApiClient(configuration) as api_client:
	api_instance = default_api.DefaultApi(api_client)

Authentication

You can configure auth parameters by passing them, like this:

from onesignal.api import default_api

configuration = onesignal.Configuration(
	app_key = "<YOUR_APP_KEY>",
    user_key = "<YOUR_USER_KEY>"
)

with onesignal.ApiClient(configuration) as api_client:
	api_instance = default_api.DefaultApi(api_client)

Example: Creating a notification

Sends a notification to your users.

# Example: send a web push notification to chrome browsers
def createNotification():
    notification = Notification()
    notification.set_attribute('app_id', APP_ID)
    notification.set_attribute('external_id', str(uuid.uuid4()))
    contentsStringMap = StringMap()   
    contentsStringMap.set_attribute('en', "Gig'em Ags")
    notification.set_attribute('contents', contentsStringMap)
    notification.set_attribute('is_any_web', True)
    notification.set_attribute('is_chrome', True)
    notification.set_attribute('included_segments', ['Subscribed Users'])

    return notification

notification = createNotification()

notificationResponse = api.create_notification(notification)

Reference

Visit our REST API Reference documentation for a complete list of our supported endpoints.

Visit our OneSignal PHP API or OneSignal Python API documentation pages for a list of supported functions and view implementation examples.

Share Your Feedback

If you’ve had a chance to use our new Python or PHP SDKs and have any feedback or suggestions you’d like to share, please reach out to us on the GitHub repository or our Discord server.

If you encounter any bugs or technical issues while using our new SDKs, we want to know so we can resolve the issue. If you're having trouble, please be sure to file a report on our GitHub repository.

If you’ve found the plugin helpful, we’d also appreciate it if you left us a review on G2.