Urban Airship Python Library

7.3.1 · active · verified Fri Apr 17

The `urbanairship` library is a Python client for interacting with the Airship REST API, enabling programmatic access to features like sending push notifications, managing audiences, and retrieving reports. It is currently at version 7.3.1 and receives updates periodically, with major releases introducing significant changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to send a basic push notification to all devices using `BasicAuthClient`, which is the recommended client for key/secret authentication. Ensure your Airship App Key and Master Secret are configured either as environment variables or directly in the code (for development purposes).

import os
from urbanairship import BasicAuthClient, Push, Audience, Notification, Platform

# Replace with your actual credentials or set as environment variables
APP_KEY = os.environ.get('URBAN_AIRSHIP_APP_KEY', 'YOUR_APP_KEY')
MASTER_SECRET = os.environ.get('URBAN_AIRSHIP_MASTER_SECRET', 'YOUR_MASTER_SECRET')

if APP_KEY == 'YOUR_APP_KEY' or MASTER_SECRET == 'YOUR_MASTER_SECRET':
    print("Please set URBAN_AIRSHIP_APP_KEY and URBAN_AIRSHIP_MASTER_SECRET environment variables or replace placeholders.")
    # exit(1) # Uncomment to prevent execution with dummy credentials

# Initialize the client with Basic Authentication
client = BasicAuthClient(APP_KEY, MASTER_SECRET)

try:
    # Create a Push object
    push = Push(client)

    # Target all devices
    push.audience = Audience.all()

    # Define the notification message
    push.notification = Notification(alert="Hello from Urban Airship Python Library!")

    # Specify device types (platforms) to target
    push.device_types = Platform.all()

    # Send the push notification
    response = push.send()

    print(f"Push notification sent successfully! Status: {response.status_code}")
    print(f"Response body: {response.json()}")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →