🐍 The Python SDK — app6.6
Developer CourseModule 6: The Python SDKapp6.6
Reading8 min

Webhooks: Get Notified When Things Happen

🐍 Module 6: The Python SDK

Webhooks: Get Notified When Things Happen

Instead of polling the API every 30 seconds, you can have the API call YOU when something happens.

What Webhooks Are

A webhook is a URL on your server that EnergyAtIt calls when an event occurs:

  • dispatch.completed — A command finished executing
  • dispatch.failed — A command failed
  • carbon.attested — A new carbon record was created
  • dr.event_started — A demand response event began
  • settlement.generated — A new settlement was created

Polling vs Webhooks

ApproachHow It WorksLatencyCost
PollingYou ask every 30s: "Anything new?"0-30sMany API calls
WebhookServer tells you instantly<1sOne call per event

Handling Webhooks

Your webhook receiver should:

  1. Respond 200 quickly — Acknowledge receipt within 5 seconds
  2. Process async — Do heavy work in a background queue
  3. Be idempotent — Handle duplicate deliveries gracefully
  4. Verify signatures — Check the webhook signature header

Production Pattern

# Flask webhook receiver example
from flask import Flask, request, jsonify
import hmac, hashlib

app = Flask(__name__)
WEBHOOK_SECRET = os.environ["WEBHOOK_SECRET"]

@app.route("/webhook", methods=["POST"])
def handle_webhook():
    # Verify signature
    signature = request.headers.get("X-Signature")
    expected = hmac.new(WEBHOOK_SECRET.encode(), request.data, hashlib.sha256).hexdigest()
    if signature != expected:
        return jsonify({"error": "invalid signature"}), 403

    event = request.json
    print(f"Event: {event['type']} — {event['data']}")
    return jsonify({"received": True}), 200

When to Use Webhooks vs Polling

  • Webhooks: Production apps that need real-time updates
  • Polling: Simple scripts, debugging, or when you cannot host a server

Log in to track your progress and earn certifications.