UG-D-02 · Receive webhooks and verify their signature
Book: Developers · Applies to: webhooks · Since: v0.16 (phase P16)
What you'll get: Piporex calls a URL of yours within seconds of something happening (a contact is created, a deal is won, an order is paid), signed so you can prove the call came from Piporex.
Before you start
- A Workspace on the Starter or Pro plan, and the Owner or Admin role.
- A public
https://URL that answers within 10 seconds. A Workspace can have up to 20 endpoints. - Read Get started with the Piporex API if you also want to call the API.
Steps
- Go to Settings → Developers → Webhooks and click + Add endpoint: your URL and the events you want (list below).
- Copy the signing secret. It looks like
whsec_…and is shown once. - Click Send test event. Piporex sends one test delivery, even while the endpoint is off, and shows the result in the
delivery log. A test body has
"data": {"test": true}and is never retried. - Verify the signature in your receiver (next section), answer
2xx, and do the real work after answering.
Events
| Event | Sent when | data is |
|---|---|---|
contact.created |
A contact is created | The contact, as the API returns it |
contact.updated |
A contact's details change | The contact |
booking.created |
Someone books an event | The booking (attendee) and event_id |
booking.cancelled |
A booking is cancelled | The booking and event_id |
booking.attended |
An attendee is checked in | The booking and event_id |
deal.stage_changed |
A deal moves to another open stage | The deal |
deal.won |
A deal is won | The deal |
deal.lost |
A deal is lost | The deal |
form.submitted |
A form is submitted | The form submission |
order.paid |
An order is paid | The order |
Changing only a contact's tags does not send contact.updated. A deal created in an open stage sends no event; one
created already won or lost does. The shapes of the contact and the deal are the ones in the
API reference.
What a delivery looks like
Piporex sends a POST with a JSON body and these headers:
| Header | Value |
|---|---|
content-type |
application/json |
user-agent |
Piporex-Webhooks/1.0 (+https://piporex.com) |
x-piporex-event |
The event name, for example contact.created |
x-piporex-delivery |
The id of this delivery |
x-piporex-timestamp |
When the delivery was signed, in Unix seconds |
x-piporex-signature |
v1= followed by 64 hex characters |
{
"id": "11111111-1111-4111-8111-111111111111",
"event": "contact.created",
"occurred_at": "2025-10-09T08:53:20.000Z",
"tenant_id": "44444444-4444-4444-8444-444444444444",
"data": { "id": "22222222-2222-4222-8222-222222222222", "email": "ada@example.com" }
}
The body id names the event. A retry, a replay and every endpoint that receives the same event carry the same id, so
store the ones you have handled and drop repeats. x-piporex-delivery is different on every delivery.
Verify the signature
The signature is v1= plus the hex HMAC-SHA256, keyed with your signing secret (its UTF-8 bytes), of
<timestamp>.<raw body>: the timestamp header, a dot, then the body exactly as received.
- Read the raw body before any JSON parsing; re-serializing changes the bytes.
- Reject the delivery if the timestamp is more than 5 minutes from your clock. This stops replays of a captured call.
- Compute the signature and compare it with
x-piporex-signaturein constant time.
These samples are the ones the Piporex test suite runs against fixed test vectors, so they match what Piporex sends.
Node.js
import { createHmac, timingSafeEqual } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
const TOLERANCE_SECONDS = 300;
// rawBody must be the request body exactly as received (a string or Buffer), not re-serialized JSON.
export function verify(secret, timestamp, signature, rawBody, now = Math.floor(Date.now() / 1000)) {
if (!/^\d+$/.test(String(timestamp))) return 'invalid timestamp';
if (Math.abs(now - Number(timestamp)) > TOLERANCE_SECONDS) {
return 'invalid timestamp outside tolerance';
}
const expected = `v1=${createHmac('sha256', secret).update(`${timestamp}.`).update(rawBody).digest('hex')}`;
const a = Buffer.from(expected);
const b = Buffer.from(String(signature));
if (a.length !== b.length || !timingSafeEqual(a, b)) return 'invalid signature';
return null;
}
// Test harness: reads {secret, timestamp, signature, body, now} from stdin. Not needed in your receiver.
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
const v = JSON.parse(readFileSync(0, 'utf8'));
const problem = verify(v.secret, v.timestamp, v.signature, v.body, v.now);
console.log(problem ?? 'ok');
process.exit(problem ? 1 : 0);
}
PHP
<?php
const TOLERANCE_SECONDS = 300;
// $raw_body must be the request body exactly as received (file_get_contents('php://input')), not re-encoded JSON.
function piporex_verify($secret, $timestamp, $signature, $raw_body, $now = null)
{
$now = $now === null ? time() : $now;
if (!preg_match('/^\d+$/', (string) $timestamp)) {
return 'invalid timestamp';
}
if (abs($now - (int) $timestamp) > TOLERANCE_SECONDS) {
return 'invalid timestamp outside tolerance';
}
$expected = 'v1=' . hash_hmac('sha256', $timestamp . '.' . $raw_body, $secret);
if (!hash_equals($expected, (string) $signature)) {
return 'invalid signature';
}
return null;
}
// Test harness: reads {secret, timestamp, signature, body, now} from stdin. Not needed in your receiver.
if (PHP_SAPI === 'cli' && realpath($_SERVER['argv'][0]) === __FILE__) {
$v = json_decode(file_get_contents('php://stdin'), true);
$problem = piporex_verify($v['secret'], $v['timestamp'], $v['signature'], $v['body'], $v['now']);
echo ($problem === null ? 'ok' : $problem) . "\n";
exit($problem === null ? 0 : 1);
}
Python
import hashlib
import hmac
import json
import sys
import time
TOLERANCE_SECONDS = 300
def verify(secret, timestamp, signature, raw_body, now=None):
"""raw_body must be the request body exactly as received (bytes), not re-serialized JSON."""
now = time.time() if now is None else now
if not str(timestamp).isdigit():
return "invalid timestamp"
if abs(now - int(timestamp)) > TOLERANCE_SECONDS:
return "invalid timestamp outside tolerance"
message = str(timestamp).encode() + b"." + raw_body
expected = "v1=" + hmac.new(secret.encode(), message, hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected.encode(), str(signature).encode()):
return "invalid signature"
return None
# Test harness: reads {secret, timestamp, signature, body, now} from stdin. Not needed in your receiver.
if __name__ == "__main__":
v = json.load(sys.stdin)
problem = verify(v["secret"], v["timestamp"], v["signature"], v["body"].encode(), v["now"])
print(problem or "ok")
sys.exit(1 if problem else 0)
The verify function is the part to copy. The block at the bottom of each sample only lets the test suite run it.
Retries, failures and turning off
- A delivery succeeds on any
2xxwithin 10 seconds. A redirect (3xx) counts as a failure; Piporex does not follow it. - After a failure Piporex retries after 10 seconds, 1 minute, 5 minutes, 30 minutes, 2 hours and 6 hours, then every 6 hours, until 24 hours have passed since the event. A delivery still failing then is marked dead.
- Each endpoint takes at most 5 deliveries at a time, so a slow receiver does not hold up your other endpoints.
- After 50 failed attempts in a row the endpoint turns itself off and Piporex emails the Owner. A success resets the count. Turn the endpoint back on in Settings → Developers → Webhooks once it works.
- If the Workspace goes back to the Free plan, endpoints turn off too. After you upgrade, turn them on again by hand.
- The delivery log keeps 30 days: status, attempts, the last error and the exact body that was signed.
Replay a delivery
In Settings → Developers → Delivery log, Replay sends a finished delivery again with the same body and signature scheme, as a new delivery. It works for a delivery that succeeded, is dead or failed without a retry pending, and only while the endpoint is on.
Which URLs are allowed
Piporex refuses a URL that could reach your private network, and checks again before every delivery:
- The URL must be
https://on port 443, with no user name or password in it. - The host must be a name, not an IP address, and not a local name (
localhost,.local,.internal). - Every address the name resolves to must be public. Private, loopback, link-local and similar addresses are refused.
A refused URL cannot be saved. If a URL stops resolving to public addresses later, deliveries to it fail.
What happens next
Failures are shown per delivery with a short reason (timeout, network, http_status, redirect,
blocked_address, unresolvable, url_not_allowed or endpoint_disabled), never the content your server answered.
If you get stuck
- My signature never matches. Use the raw body, not parsed and re-serialized JSON; use the timestamp from the header as text; make sure you used the secret of this endpoint (each endpoint has its own).
- It says the timestamp is outside tolerance. Your server clock is more than 5 minutes off. Fix its time sync.
- The endpoint turned itself off. It failed 50 attempts in a row. Open the delivery log to see why, fix the receiver, turn the endpoint on and replay what you missed.
- I lost the secret. It cannot be shown again; add a new endpoint and delete the old one.
- I get the same event twice. Retries can deliver an event more than once. Drop repeats by the body
id.
Related
- UG-D-01 · UG-D-03 · API reference
Release notes: v0.16 — article created.