Course map
Stage 1: Foundations
Stage 2: Data Integration
Stage 3: Building & Shipping
Stage 4: Customer Engagement
Stage 5: Specialization

Part 05 / Topic 6 of 7

Webhooks

Pull has a mirror: the customer's systems can push events to you. A webhook is an HTTP request their system sends to your endpoint when something happens — shipment.delivered, say. Anyone can POST to a URL, so before trusting a webhook you verify it was signed with the shared secret:

# examples/03_verify_a_webhook_signature_with_hmac.py
import hashlib, hmac, json

SECRET = b"shared-webhook-secret"
body = json.dumps({"event": "shipment.delivered", "id": "S-2001"}).encode()
signature = hmac.new(SECRET, body, hashlib.sha256).hexdigest()       # what the sender attaches

def verify(body: bytes, received_sig: str) -> bool:
    expected = hmac.new(SECRET, body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, received_sig)              # constant-time comparison

print("genuine:", verify(body, signature))
tampered = body.replace(b"S-2001", b"S-9999")
print("tampered body:", verify(tampered, signature))
# genuine: True
# tampered body: False

hmac.compare_digest matters: a naive == returns faster the earlier the strings differ, which lets an attacker learn the signature byte by byte from response timings. The constant-time comparison closes that door.

🧪 Quick quiz

  1. Why compare webhook signatures with hmac.compare_digest?
Show the answer

It takes the same time whether or not the strings match, preventing timing attacks. A genuine body verified as True; a tampered body as False. (verified against the example output above)

Done here? The practice set for Part 5 — 4 exercises with solutions — is on the part page.

Search the guide