Package: onesignal-sdk
onesignal_sdkA Python client library forOneSignalREST API. Supportsasync/await.Please readv1.x documentationfor older versions.Table of ContentsInstallationExample UsageAsync Example UsageHandling ExceptionsAPI methods.send_notification.cancel_notification.view_notification.view_notifications.notification_history.view_device.view_devices.add_device.edit_device.edit_tags.new_session.new_purchase.csv_export.create_segment.delete_segment.view_outcomes.view_apps.view_app.create_app.update_appLicenseInstallationpip install onesignal-sdkExample UsageYou can think this library as a wrapper around OneSignal REST API. It is fairly simple to use:Create an instance ofClientwith your credentials.user_auth_keyis not required but necessary for some API calls.Build your request body and call related method on the client.Client will make the request with required authentication headers and parse the response for you.fromonesignal_sdk.clientimportClientclient=Client(app_id=APP_ID,rest_api_key=REST_API_KEY,user_auth_key=USER_AUTH_KEY)notification_body={'contents':{'tr':'Yeni bildirim','en':'New notification'},'included_segments':['Active Users'],'filters':[{'field':'tag','key':'level','relation':'>','value':10}],}response=client.send_notification(notification_body)print(response.body)Async Example UsageAsyncClientandClientshares exactly the same interface, method signatures. All the examples forClientin this
documentation is also valid forAsyncClient.fromonesignal_sdk.clientimportAsyncClientasyncdefmain():client=AsyncClient(app_id=APP_ID,rest_api_key=REST_API_KEY)notification_body={'contents':...}response=awaitclient.send_notification(notification_body)print(response.body)Handling ResponseWe are usinghttpxlibrary for making http requests underneath. Responses from OneSignal
REST API are parsed as JSON and returned to you as an instance ofOneSignalResponse, which is just a simple class
consisting of following attributes:.body: JSON parsed body of the response, as a Python dictionary..status_code: HTTP status code of the response..http_response: Originalhttpx.Responseobject, in case you want to access more attributes.Sample code:client=AsyncClient(...)response=awaitclient.view_apps()print(response.body)# JSON parsed responseprint(response.status_code)# Status code of responseprint(response.http_response)# Original http response object.Handling ExceptionsAn instance ofOneSignalHTTPErroris raised whenever http responses have a status code other than 2xx.
For instance, if status code of an http response is 404,OneSignalHTTPErroris raised with additional details. See
the sample snippet below, error handling is the same ofAsyncClientfromonesignal_sdk.clientimportClientfromonesignal_sdk.errorimportOneSignalHTTPError# Create a One Signal client using API KEYS.client=Client(app_id=APP_ID,rest_api_key=REST_API_KEY,user_auth_key=USER_AUTH_KEY)notification_body={'contents':{'tr':'Yeni bildirim','en':'New notification'},'included_segments':['Active Users'],'filters':[{'field':'tag','key':'level','relation':'>','value':10}],}response=client.send_notification(notification_body)print(response.body)try:notification_body={'contents':{'en':'New notification'},'included_segments':['Active Users'],}# Make a request to OneSignal and parse responseresponse=client.send_notification(notification_body)print(response.body)# JSON parsed responseprint(response.status_code)# Status code of responseprint(response.http_response)# Original http response object.exceptOneSignalHTTPErrorase:# An exception is raised if response.status_code != 2xxprint(e)print(e.status_code)print(e.http_response.json())# You can see the details of error by parsing original responseAPI methodssend_notificationReference:https://documentation.onesignal.com/reference/create-notificationnotification_body={'contents':{'en':'New notification'},'included_segments':['Active Users'],}response=client.send_notification(notification_body)cancel_notificationReference:https://documentation.onesignal.com/reference/cancel-notificationresponse=client.cancel_notification('notification-id')view_notificationReference:https://documentation.onesignal.com/reference/view-notificationresponse=client.view_notification('notification-id')view_notificationsReference:https://documentation.onesignal.com/reference/view-notificationsrequest_query={'limit':5,'offset':2}response=client.view_notification(request_query)notification_historyReference:https://documentation.onesignal.com/reference/notification-historybody={'events':'clicked','email':'test@email.com'}response=client.notification_history('notification-id',body)view_deviceReference:https://documentation.onesignal.com/reference/view-deviceresponse=client.view_device('device-id')view_devicesReference:https://documentation.onesignal.com/reference/view-devicesrequest_query={'limit':1}response=client.view_devices(request_query)//ornoqueryresponse=client.view_devices()add_deviceReference:https://documentation.onesignal.com/reference/add-a-devicebody={'device_type':1,'identifier':'7a8bbbb00000'}response=client.add_device(body)edit_deviceReference:https://documentation.onesignal.com/reference/edit-devicebody={'device_type':1,'identifier':'7a8bbbb00000'}response=client.edit_device('2ada581e-1380-4967-bcd2-2bb4457d6171',body)edit_tagsReference:https://documentation.onesignal.com/reference/edit-tags-with-external-user-idbody={'tags':{'foo':'','bar':'new_value',}}response=client.edit_tags('f0f0f0f0',body)new_sessionReference:https://documentation.onesignal.com/reference/new-sessionbody={'language':'de','timezone':-28800}response=client.new_session('foo-device-id',body)new_purchaseReference:https://documentation.onesignal.com/reference/new-purchasebody={'purchases':[{'sku':'SKU123','iso':'EUR'}]}response=client.new_purchase('foo-device-id',body)csv_exportReference:https://documentation.onesignal.com/reference/csv-exportbody={'extra_fields':['country','location'],'last_active_since':'1469392779',}response=client.csv_export(body)create_segmentReference:https://documentation.onesignal.com/reference/create-segmentsbody={'name':'new-segment','filters':[{'field':'session_count','relation':'>','value':1}],}response=client.create_segment(body)delete_segmentReference:https://documentation.onesignal.com/reference/delete-segmentsresponse=client.delete_segment('segment-id-1')view_outcomesReference:https://documentation.onesignal.com/reference/view-outcomesextra_http_params={'outcome_platforms':0}outcome_names=['os__click.count']response=client.view_outcomes(outcome_names,extra_http_params)view_appsReference:https://documentation.onesignal.com/reference/view-apps-appsRequiresuser_auth_key!response=client.view_apps()view_appReference:https://documentation.onesignal.com/reference/view-an-appRequiresuser_auth_key!response=client.view_app('034744e7-4eb-1c6a647e47b')create_appReference:https://documentation.onesignal.com/reference/create-an-appRequiresuser_auth_key!app_body={'name':'new-android-app','apns_env':'production',}response=client.create_app(app_body)update_appReference:https://documentation.onesignal.com/reference/update-an-appRequiresuser_auth_key!app_body={'name':'new-app',}response=client.update_app('f33c318b-6c99',app_body)LicenseThis project is under the MIT license.