PushRadar and Python

The latest version of PushRadar's API is v2, released July 5, 2019.

Welcome to PushRadar's API documentation for Python. This article covers calling the various methods of the PushRadar object to publish notifications to a channel in realtime.


PushRadar's Python library supports Python 3.3+.

You will need to do the following in order to use the library:

  • (a)Install PushRadar's Python library using Pip by running the command pip install PushRadar
(server)"Hello World!" example

The following example demonstrates broadcasting a "Hello World!" message on a channel called test-channel. Replace your-secret-key with your actual PushRadar secret, which may be obtained from the API page of your dashboard.

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
radar.broadcast("test-channel", message="Hello World!")
(client)Receiving notifications

To find out how to receive notifications by subscribing to channels, check out the documentation for our JavaScript client library.

(tip)Adding multiple data items

For convenience, PushRadar's Python library supports a fluent syntax to structure broadcasts, as well as passing in unlimited data items to the broadcast method after the channel name. To add multiple data items, you can do either of the following:

  • Python
# ... radar = PushRadar(...)
    message="Hello World!",
    item_2="It's me!"

# --- or ---

radar.broadcast("test-channel", message="Hello World!", item_2="It's me!")
(feature)Action targeting

The following example demonstrates sending a notification to users who have taken a specific action, in this case, users who have purchased item #11.

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
    "test-channel", message="You have purchased item 11.")

If you include more than one action by calling target_action() again or target_actions(), PushRadar will send the notification to users who have taken any of the given actions.

With the PushRadar JavaScript client library, you can track actions that users have taken using the action method:

  • JavaScript
// ... var radar = new PushRadar(...)
Reverse action targeting

To send a notification to those who have not taken a certain action, you can use the target_not_action and target_not_actions methods. If you specify more than one action identifier, PushRadar will only send the notification to clients who have not taken any of the given actions.

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
    "test-channel", message="You have not purchased item 12.")
(feature)User targeting

User targeting allows you to broadcast notifications to specific users, identified by their user ID. For this example, we will send a new message to the user with ID usr100 in our system by calling target_user(). Similarly, to target multiple users, you can call target_users().

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
    message="Hey there, from Sam!",

Make sure to set user IDs on each website page load using the PushRadar JavaScript client library:

  • JavaScript
// Set user ID on initialization
var radar = new PushRadar("your-api-key", "usr100");

// ... or ... radar.setUserID("usr100");

radar.subscribe.to("new-messages", function(notification) {
    alert("New message from " + notification.sender_name); // "New message from Sam"

Note:   If you are targeting users, you should in most cases also use private channels to make for a more secure solution.

(feature)Browser targeting

You can target notifications to users who are currently using specific web browsers by calling target_browser() or target_browsers(). In the following example, we broadcast a notification to users currently using Firefox, Chrome or Internet Explorer:

Supported browsers are:   "chrome", "firefox", "ie", "edge", "safari" and "opera"

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
radar.target_browsers("firefox", "chrome", "ie").broadcast("test-channel",
    data="This notification is browser-targeted.")
(feature)Geo-targeting (ISO 3166-1 alpha-2 )

You can easily broadcast notifications to clients located in certain countries by calling target_country() or target_countries(), passing in two-letter ISO country codes. In this example, we broadcast a notification to clients who are currently located in the UK or France:

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
radar.target_countries("GB", "FR").broadcast("test-channel",
    data="You are in the UK or France!")

Continent targeting is also supported, with specific target... methods you can call. Below, we add to the previous example and broadcast to all users located in North America, the UK or France:

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
radar.target_north_america().target_countries("GB", "FR").broadcast("test-channel",
    data="You are in North America, the UK or France!")

Each continent has its own dedicated "target" method:

   - target_europe()
   - target_north_america()
   - target_south_america()
   - target_oceania()
   - target_asia()
   - target_africa()
   - target_antarctica()

(feature)IP address targeting

PushRadar supports IP address targeting. When using IP targeting, only clients with the given IP address(es) specified will receive your notification. Below is an example targeting a client with IP address The target_ip and target_ips methods accept both IPv4 and IPv6 addresses.

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")
    data="This notification is IP-targeted.")
(secure channels)Channel authentication

Channel authentication is used for private and presence channels. Your authentication endpoint should return a response containing an authentication token if the user can join the channel.

PushRadar provides a channel_auth method that generates channel authentication tokens. Here's an example:

  • Python
from PushRadar import PushRadar

radar = PushRadar("your-secret-key")

# Get request query parameter "channel" and determine whether the user can join it

# ... if they can, return the auth token
return json.dumps({"authToken": radar.channel_auth("channel-name")})

Then register your authentication endpoint URL using the JavaScript client library.

  • JavaScript
var radar = new PushRadar("your-api-key");