{"library":"onesignal-python-api","title":"OneSignal Python API Client","description":"The `onesignal-python-api` library is the official Python client for the OneSignal Push Notification API. It enables developers to programmatically send push notifications, manage users and segments, and interact with other OneSignal services. The current stable version is 5.3.0, and the library generally maintains a monthly or bi-monthly release cadence, with major versions often introducing significant API changes.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install onesignal-python-api"],"cli":null},"imports":["import onesignal as os_client","from onesignal.exceptions import OneSignalAPIError"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nimport onesignal as os_client\nfrom onesignal.exceptions import OneSignalAPIError\n\n# Initialize the OneSignal client using environment variables for credentials\n# Replace 'YOUR_APP_ID' and 'YOUR_REST_API_KEY' with actual values or set environment variables.\napp_id = os.environ.get(\"ONESIGNAL_APP_ID\", \"YOUR_APP_ID\")\nrest_api_key = os.environ.get(\"ONESIGNAL_REST_API_KEY\", \"YOUR_REST_API_KEY\")\n\n# Basic check for placeholder credentials\nif app_id == \"YOUR_APP_ID\" or rest_api_key == \"YOUR_REST_API_KEY\":\n    print(\"WARNING: Please set ONESIGNAL_APP_ID and ONESIGNAL_REST_API_KEY environment variables \")\n    print(\"         or replace placeholders for successful API calls.\")\n\ntry:\n    client = os_client.Client(\n        app_id=app_id,\n        rest_api_key=rest_api_key\n    )\n\n    # Example 1: Send a basic notification to all subscribed users\n    notification_body = {\n        \"contents\": {\"en\": \"Hello from the OneSignal Python API client!\"},\n        \"included_segments\": [\"All\"],\n        \"name\": \"Python Test Notification\" # Optional, for internal tracking\n    }\n    print(\"\\nAttempting to send a notification...\")\n    notification = client.send_notification(notification_body)\n    print(f\"Notification created successfully. ID: {notification.id}\")\n\n    # Example 2: Get details for the app (requires appropriate API key permissions)\n    print(\"\\nAttempting to retrieve app details...\")\n    app_details = client.get_app(app_id)\n    print(f\"Successfully fetched app '{app_details.name}' (ID: {app_details.id})\")\n\nexcept OneSignalAPIError as e:\n    print(f\"\\nOneSignal API Error: {e.status_code} - {e.json_body}\")\nexcept Exception as e:\n    print(f\"\\nAn unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize the OneSignal client using environment variables for credentials and then send a simple notification to all users. It also includes an example of fetching app details. Remember to set `ONESIGNAL_APP_ID` and `ONESIGNAL_REST_API_KEY` in your environment or replace the placeholders.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}