Docs/Send a Notification

Python

Last updatedยท1 min read

Send push notifications from Python scripts, cron jobs, Django/Flask backends, or data pipelines.

Installation

pip install requests

Basic example

import requests

response = requests.post(
    'https://thenotification.app/api/sendNotification',
    headers={
        'app_key': 'YOUR_API_KEY',
        'Content-Type': 'application/json'
    },
    json={
        'title': 'New Message',
        'body': 'Check out this notification',
        'link': 'https://example.com/page',
        'image': 'https://picsum.photos/400/200'
    }
)

print(response.json())

Reusable function

import requests
import os

def send_notification(title: str, body: str, link: str = None, image: str = None) -> dict:
    """Send a push notification via TheNotificationApp API."""
    payload = {'title': title, 'body': body}

    if link:
        payload['link'] = link
    if image:
        payload['image'] = image

    response = requests.post(
        'https://thenotification.app/api/sendNotification',
        headers={
            'app_key': os.environ.get('NOTIFICATION_API_KEY'),
            'Content-Type': 'application/json'
        },
        json=payload
    )

    return response.json()

# Usage
send_notification(
    title='Deployment complete',
    body='Your app has been deployed successfully!'
)

Store your API key in an environment variable โ€” never hardcode it:

export NOTIFICATION_API_KEY=your_api_key_here

Error handling

def send_notification(title: str, body: str) -> bool:
    try:
        response = requests.post(
            'https://thenotification.app/api/sendNotification',
            headers={
                'app_key': os.environ.get('NOTIFICATION_API_KEY'),
                'Content-Type': 'application/json'
            },
            json={'title': title, 'body': body},
            timeout=10
        )
        result = response.json()

        if response.status_code == 429:
            print('Rate limit reached โ€” upgrade to Pro for more notifications')
            return False

        return result.get('success', False)

    except requests.exceptions.RequestException as e:
        print(f'Failed to send notification: {e}')
        return False