Skip to main content

Signed Callbacks

When signing is switched on for an API key, every callback sent for that key is signed. Checking the signature proves the callback came from Metric and was not changed on the way.

What a signed callback looks like​

The body is the same JSON you receive today. We add three headers.

POST <your callback URL>
Content-Type: application/json
X-TIMESTAMP: 1786665600
X-SIGNATURE: 3f9a1c...
X-SIGNATURE-ALGORITHM: HMAC-SHA256

{ ...the usual callback body... }
HeaderMeaning
X-TIMESTAMPUnix time in seconds when we signed the callback
X-SIGNATUREHMAC-SHA256 of <X-TIMESTAMP>.<raw request body>, keyed with your signing secret, as lowercase hex
X-SIGNATURE-ALGORITHMAlways HMAC-SHA256
note

Timestamps are in seconds, not milliseconds, and the tolerance is 5 minutes either side.

note

A handler that ignores these headers keeps working, so signing can be switched on before you start checking it.

Verify the signature​

Recompute the signature from the raw body and compare it with the one we sent.

  1. Read the raw request body: the bytes as received, before any JSON parsing.
  2. Build the string <X-TIMESTAMP>.<raw body>.
  3. Compute HMAC-SHA256 of that string with your signing secret, as lowercase hex.
  4. Compare it with X-SIGNATURE using a constant-time comparison. Reject the callback on a mismatch.
  5. Reject the callback if X-TIMESTAMP is more than 5 minutes from your server time.
  6. Reject a transactionnumber you have already processed.

Steps 5 and 6 stop a genuine callback that someone captured from being sent to you again.

Important

Use the raw body. A re-encoded body can differ in spacing, key order or number formatting, and a single changed byte fails the check.

$raw = file_get_contents('php://input');
$ts = $_SERVER['HTTP_X_TIMESTAMP'] ?? '';
$sig = $_SERVER['HTTP_X_SIGNATURE'] ?? '';

$expected = hash_hmac('sha256', $ts . '.' . $raw, $signingSecret);

if (!hash_equals($expected, $sig) || abs(time() - (int) $ts) > 300) {
http_response_code(401);
exit;
}

$payload = json_decode($raw, true);