वेबहुक
हर बदलाव की हस्ताक्षरित, दोबारा भेजी जाने वाली सूचनाएँ, और उन्हें ठीक से पाने का तरीका।
एक एंडपॉइंट जोड़ें और उसके लिए इवेंट चुनें। हर इवेंट छोटी JSON बॉडी वाले POST से भेजा जाता है: क्या हुआ और किस रिकॉर्ड के साथ। विवरण के लिए अपनी की से रिकॉर्ड मँगाएँ।
POST /webhooks HTTP/1.1
Content-Type: application/json
webhook-id: msg_01JD3XC4Q8F2N6R0T4V8X2Z6B0
webhook-timestamp: 1764576000
webhook-signature: v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=हस्ताक्षर जाँचें
हर अनुरोध Standard Webhooks तरीके से आपके एंडपॉइंट के सीक्रेट से हस्ताक्षरित होता है। जिस अनुरोध का हस्ताक्षर मेल न खाए या जिसका टाइमस्टैम्प पाँच मिनट से पुराना हो, उसे अस्वीकार करें।
import crypto from "node:crypto";
export function verify(rawBody, headers, secret, toleranceSec = 300) {
const id = headers["webhook-id"], ts = headers["webhook-timestamp"], sig = headers["webhook-signature"];
if (!id || !ts || !sig || Math.abs(Date.now() / 1000 - Number(ts)) > toleranceSec) return false;
const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
const expected = crypto.createHmac("sha256", key).update(`${id}.${ts}.${rawBody}`).digest("base64");
return sig.split(" ").some((p) => {
const [v, s] = p.split(",");
return v === "v1" && s && s.length === expected.length && crypto.timingSafeEqual(Buffer.from(s), Buffer.from(expected));
});
}<?php
function verify(string $rawBody, array $headers, string $secret, int $toleranceSec = 300): bool
{
$id = $headers['webhook-id'] ?? null;
$ts = $headers['webhook-timestamp'] ?? null;
$sig = $headers['webhook-signature'] ?? null;
if (!$id || !$ts || !$sig || abs(time() - (int) $ts) > $toleranceSec) {
return false;
}
$key = base64_decode(preg_replace('/^whsec_/', '', $secret));
$expected = base64_encode(hash_hmac('sha256', "{$id}.{$ts}.{$rawBody}", $key, true));
foreach (explode(' ', $sig) as $part) {
[$version, $signature] = array_pad(explode(',', $part, 2), 2, '');
if ($version === 'v1' && hash_equals($expected, $signature)) {
return true;
}
}
return false;
}import base64, hashlib, hmac, time
def verify(raw_body: bytes, headers: dict, secret: str, tolerance_sec: int = 300) -> bool:
msg_id = headers.get("webhook-id")
ts = headers.get("webhook-timestamp")
sig = headers.get("webhook-signature")
if not msg_id or not ts or not sig or abs(time.time() - int(ts)) > tolerance_sec:
return False
key = base64.b64decode(secret.removeprefix("whsec_"))
signed = f"{msg_id}.{ts}.".encode() + raw_body
expected = base64.b64encode(hmac.new(key, signed, hashlib.sha256).digest()).decode()
return any(
part.split(",", 1)[0] == "v1" and hmac.compare_digest(part.split(",", 1)[1], expected)
for part in sig.split(" ") if "," in part
)दोबारा भेजना और क्रम
- पाँच सेकंड के अंदर 2xx से जवाब दें; काम बाद में करें।
- विफल डिलीवरी लगभग 32 घंटों में आठ बार दोबारा भेजी जाती है। रीडायरेक्ट का पालन नहीं होता।
- एक ही इवेंट दो बार आ सकता है: जो webhook-id पहले संभाल चुके हैं उसे छोड़ दें।
- इवेंट क्रम से बाहर आ सकते हैं: मौजूदा स्थिति के लिए रिकॉर्ड मँगाएँ।
- तीन दिन विफल रहने वाला एंडपॉइंट बंद हो जाता है; ठीक करने के बाद छूटे इवेंट पैनल से दोबारा भेजें।
सुरक्षा चेकलिस्ट
- मान्य प्रमाणपत्र के साथ https इस्तेमाल करें।
- पार्स करने से पहले कच्ची बॉडी पर हस्ताक्षर जाँचें।
- साइनिंग सीक्रेट अपने सर्वर पर रखें और लीक होने पर बदलें।