A webhook is one server calling another when something happens. HeyMySocial has three, and all of them are signed the same way.
| Webhook | Direction | Signed with | For |
|---|---|---|---|
| Tracking webhook | Your server to HeyMySocial | The signing secret of a tracking site (hms_whsec_…) |
Leads and later events from a form, a calculator or a backend you already have |
| CRM inbound webhook | Your CRM to HeyMySocial | The signing secret of a CRM connection (hmscrm_…) |
Contacts, leads, opportunities and sales as they change in the CRM |
| CRM outbound webhook | HeyMySocial to your CRM | The same secret of that CRM connection | A lead's status, when it changes here |
None of them uses an API key. The address finds the tracking site or the connection; the signature proves who is calling. If you would rather call with an API key, the REST API takes the same events and the same CRM records.
The signature
Every webhook request carries one header:
X-HMS-Signature: t=<unix seconds>,v1=<hex>
tis the time the request was signed, in whole seconds since 1970.v1is an HMAC-SHA256, written as 64 lower-case hex characters. The key is the signing secret. The message is the timestamp, a full stop, and the raw request body:<t>.<raw body>.
Sign the bytes you send, and verify the bytes you received. Parsing the JSON and writing it out again can change spacing or the order of fields, and then the signature no longer matches.
T=$(date +%s)
SIG=$(printf '%s.%s' "$T" "$BODY" | openssl dgst -sha256 -hmac "$HMS_SIGNING_SECRET" | sed 's/^.* //')
echo "X-HMS-Signature: t=$T,v1=$SIG"
The clock
The timestamp is inside the signed text, so a request that someone captured cannot be sent again later with a fresh time.
- The tracking webhook refuses a timestamp more than 5 minutes from the server's clock, in either direction.
- The CRM inbound webhook refuses one more than 5 minutes off.
- When you receive the outbound webhook, do the same: refuse anything older than 5 minutes.
Keep the clock of the sending machine in step (NTP), and sign each attempt with the time of that attempt. A retry signed with the old timestamp will sooner or later be too old.
Replays
A valid signature inside the time window could still be sent twice. Each webhook closes that gap in its own way.
| Webhook | What stops a replay |
|---|---|
| Tracking | The exact same request (the same signature) is refused with 409. A retry that is signed again is accepted, and the events in it are recognized by their event_id and answered as duplicates. |
| CRM inbound | An event_id is taken once for each connection. A repeat is answered 200 with the status duplicate and changes nothing. |
| CRM outbound | Every delivery has a delivery_id, also sent as the header X-HMS-Delivery. A retry carries the same id: keep the ids you have handled and skip one you have seen. |
Verify a signature
Use this where you receive the CRM outbound webhook. It reads the header, checks the clock, computes the signature over the raw body and compares in constant time. A header may carry more than one v1; one match is enough.
<?php
function hms_signature_is_valid(string $rawBody, string $header, string $secret, int $toleranceSeconds = 300): bool
{
$timestamp = null;
$signatures = [];
foreach (explode(',', $header) as $part) {
[$name, $value] = array_pad(explode('=', trim($part), 2), 2, '');
if ($name === 't') {
$timestamp = $value;
} elseif ($name === 'v1') {
$signatures[] = $value;
}
}
if ($timestamp === null || ! ctype_digit($timestamp) || abs(time() - (int) $timestamp) > $toleranceSeconds) {
return false;
}
$expected = hash_hmac('sha256', $timestamp.'.'.$rawBody, $secret);
foreach ($signatures as $signature) {
if (hash_equals($expected, $signature)) {
return true;
}
}
return false;
}
const crypto = require('node:crypto');
function hmsSignatureIsValid(rawBody, header, secret, toleranceSeconds = 300) {
let timestamp = '';
const signatures = [];
for (const part of String(header || '').split(',')) {
const [name, value = ''] = part.trim().split('=', 2);
if (name === 't') timestamp = value;
if (name === 'v1') signatures.push(value);
}
if (!/^\d+$/.test(timestamp) || Math.abs(Date.now() / 1000 - Number(timestamp)) > toleranceSeconds) {
return false;
}
const expected = crypto.createHmac('sha256', secret).update(`${timestamp}.`).update(rawBody).digest();
return signatures.some((signature) => {
const given = Buffer.from(signature, 'hex');
return given.length === expected.length && crypto.timingSafeEqual(given, expected);
});
}
import hashlib
import hmac
import time
def hms_signature_is_valid(raw_body: bytes, header: str, secret: str, tolerance_seconds: int = 300) -> bool:
timestamp = ""
signatures = []
for part in (header or "").split(","):
name, _, value = part.strip().partition("=")
if name == "t":
timestamp = value
elif name == "v1":
signatures.append(value)
if not (timestamp.isascii() and timestamp.isdigit()) or abs(time.time() - int(timestamp)) > tolerance_seconds:
return False
expected = hmac.new(secret.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256).hexdigest()
return any(hmac.compare_digest(expected.encode(), signature.encode()) for signature in signatures)
Give the function the body as it arrived, before any JSON parsing: file_get_contents('php://input') in PHP, the collected Buffer of the request in Node, request.get_data() or the equivalent in Python. Answer 401 when it returns false.
Looking after secrets
- A signing secret is shown once, when the tracking site or the CRM connection is created and each time you rotate it. It is stored encrypted and cannot be read back.
- Rotating replaces it at once. Requests signed with the old secret are refused from that moment, so change the sender straight after.
- Keep it on a server, next to your other secrets. It never belongs in a web page.