Skip to main content...
Python (or Go) for Automation
35 min

Day 31: Project: API query → JSON transform → webhook alert

Phase 4 capstone project

Combine everything this week: query a real API, transform the JSON response, and post an alert to a webhook (Slack/Discord both accept simple JSON webhooks) if a condition is met.

alert.py — a real, runnable shape
import requests
import sys

API_URL = 'https://api.github.com/repos/kubernetes/kubernetes'
WEBHOOK_URL = 'https://hooks.slack.com/services/...'
OPEN_ISSUES_THRESHOLD = 3000

def check_and_alert():
    resp = requests.get(API_URL, timeout=10)
    resp.raise_for_status()
    data = resp.json()

    open_issues = data['open_issues_count']
    if open_issues > OPEN_ISSUES_THRESHOLD:
        message = f':warning: Open issues at {open_issues}, above threshold {OPEN_ISSUES_THRESHOLD}'
        alert_resp = requests.post(WEBHOOK_URL, json={'text': message}, timeout=10)
        alert_resp.raise_for_status()
        print('Alert sent.')
    else:
        print(f'OK — {open_issues} open issues, under threshold.')

if __name__ == '__main__':
    try:
        check_and_alert()
    except requests.RequestException as e:
        print(f'Check failed: {e}', file=sys.stderr)
        sys.exit(1)

Make it yours

Swap the API for something you actually care about monitoring, wire the script into cron (Phase 1, Day 11) to run every 15 minutes, and confirm an alert actually lands in a real Slack/Discord channel. This exact query → transform → alert shape is the skeleton of every monitoring integration you'll build for the rest of this course.

Phase 4 complete — you should now be able to

Why does the script check resp.status_code (via raise_for_status) both for the API call and the webhook POST?

We use cookies

We use cookies to enhance your browsing experience, serve personalized content, and analyze our traffic. By clicking "Accept All", you consent to our use of cookies. Learn more

    Day 31: Project: API query → JSON transform → webhook alert | RBTechIconX