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 executingdispatch.failed— A command failedcarbon.attested— A new carbon record was createddr.event_started— A demand response event begansettlement.generated— A new settlement was created
Polling vs Webhooks
| Approach | How It Works | Latency | Cost |
|---|---|---|---|
| Polling | You ask every 30s: "Anything new?" | 0-30s | Many API calls |
| Webhook | Server tells you instantly | <1s | One call per event |
Handling Webhooks
Your webhook receiver should:
- Respond 200 quickly — Acknowledge receipt within 5 seconds
- Process async — Do heavy work in a background queue
- Be idempotent — Handle duplicate deliveries gracefully
- 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}), 200When 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.