Skip to Content
Webhooks & EventsWebhook Receiver

Build a Webhook Receiver

Receive msend events over HTTPS in real time, verify signatures, and deduplicate deliveries using only the standard library.

Tested with: Python 3.12 and Node.js v23.11, msend API v1.

Signature Verification & Handler

import hashlib, hmac, json, os, time from http.server import BaseHTTPRequestHandler, HTTPServer SECRET = os.environ["MSEND_WEBHOOK_SECRET"] seen = set() def valid(raw_body: bytes, timestamp: str, signature: str) -> bool: try: if abs(time.time() - int(timestamp)) > 300: return False except ValueError: return False expected = "v1=" + hmac.new(SECRET.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256).hexdigest() return hmac.compare_digest(expected, signature) class Handler(BaseHTTPRequestHandler): def do_POST(self): raw = self.rfile.read(int(self.headers.get("Content-Length", 0))) if not valid(raw, self.headers.get("Msend-Timestamp", ""), self.headers.get("Msend-Signature", "")): self.send_response(401) self.end_headers() return event_id = self.headers.get("Msend-Event-Id") if event_id not in seen: seen.add(event_id) event = json.loads(raw) # Queue slow LLM work in the background after replying 200 self.send_response(200) self.end_headers()

For local testing against http://127.0.0.1:4000, start msend with ALLOW_INSECURE_WEBHOOKS=true and ALLOW_PRIVATE_WEBHOOK_TARGETS=true.

Last updated on