How to send a push notification from a Python script
Send iPhone notifications from Python in 2 minutes. One HTTP POST call, no SDK, no complex setup.
Send iPhone push notifications directly from Python with a single HTTP POST call. No SDKs, no complex setup—just a few lines of code.
In this guide, you'll learn how to send notifications from any Python script: monitoring scripts, cron jobs, Flask/Django backends, or data pipelines.
Prerequisites
- Python 3.6+
- The
requestslibrary (pip install requests) - An API key from TheNotificationApp (free tier includes 100 lifetime notifications)
Step 1: Get your API key
Sign up at thenotification.app, create an app, and copy your API key. You'll use this to authenticate all notification requests.
Step 2: Send your first notification
Open Python and make an HTTP POST request to TheNotificationApp's endpoint:
import requests
api_key = "YOUR_API_KEY"
url = "https://thenotification.app/api/sendNotification"
headers = {
"app_key": api_key,
"Content-Type": "application/json"
}
payload = {
"title": "Hello from Python",
"body": "Your script is working!",
"link": "https://example.com"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Run this code, and you'll get an iPhone notification within seconds.
Step 3: Add error handling
In production, always handle failures gracefully:
import requests
import os
api_key = os.getenv("NOTIFICATION_API_KEY")
url = "https://thenotification.app/api/sendNotification"
headers = {
"app_key": api_key,
"Content-Type": "application/json"
}
payload = {
"title": "Script Alert",
"body": "Your cron job just finished",
"link": "https://example.com/logs"
}
try:
response = requests.post(url, json=payload, headers=headers, timeout=5)
response.raise_for_status()
print("Notification sent successfully")
except requests.exceptions.RequestException as e:
print(f"Failed to send notification: {e}")Store your API key in an environment variable instead of hardcoding it. This keeps your credentials safe and makes your code portable.
Step 4: Use it in a real monitoring script
Here's a practical example: a simple health check that notifies you when something goes wrong:
import requests
import os
import subprocess
def send_alert(title, body):
api_key = os.getenv("NOTIFICATION_API_KEY")
url = "https://thenotification.app/api/sendNotification"
headers = {
"app_key": api_key,
"Content-Type": "application/json"
}
payload = {"title": title, "body": body}
try:
requests.post(url, json=payload, headers=headers, timeout=5)
except Exception as e:
print(f"Alert failed: {e}")
# Check if a service is running
result = subprocess.run(["curl", "-f", "http://localhost:8000/health"],
capture_output=True)
if result.returncode != 0:
send_alert("Service Down", "Your web server is not responding")
else:
print("Service is healthy")Optional: Add images and custom links
Notifications can include images and deep links to help users take action:
payload = {
"title": "New deployment",
"body": "Version 2.1.0 is live",
"link": "https://example.com/changelog",
"image": "https://example.com/banner.jpg"
}
requests.post(url, json=payload, headers=headers)Common use cases
- Cron job alerts: Get notified when a scheduled task fails or completes
- Monitoring: Instant alerts when CPU, disk, or memory exceeds a threshold
- CI/CD: Notifications for build failures or deployment milestones
- Data pipelines: Alerts when a job finishes or encounters errors
- Django/Flask: Send notifications on sign-ups, payments, or errors
Rate limits and quota
TheNotificationApp enforces these limits:
- Free: 100 lifetime notifications, up to 3 apps
- Pro: 1,000 notifications per month, up to 10 apps (just $2.99/month)
If you hit your quota, upgrade to Pro for unlimited headroom.
For more details
Check the full API reference for error codes and response formats. If you prefer a different language, we have JavaScript and curl guides too.
Next steps
Start with the free tier—100 notifications is plenty to test your monitoring setup. Once you've seen how useful instant phone alerts are, upgrade to Pro for ongoing use.
Got questions? Check our homepage or full documentation.
Stop babysitting your scripts.
Free to download. Free tier available. Swiss-hosted, private by design.