Skip to content
Peal
Esc
↑↓navigate↵open⌘Jpreview
On this page

Verify a delivery

What a delivery looks like, how it is signed with the endpoint's secret, and how to verify it.

Every delivery is a signed POST to your endpoint. Check the signature before you act on a delivery, so your server only trusts requests that come from Peal.

The signing secret

Peal generates a secret for each endpoint: whsec_ followed by 32 random bytes in base64. Peal stores it encrypted.

Rotating the secret gives you a new one, also shown once. For the next 24 hours, each delivery carries two signatures, one per secret, so you can switch your server to the new secret without missing a delivery. After that, the old secret signs nothing.

What Peal sends

Each delivery is a POST to the endpoint’s address, with content-type: application/json and three headers:

Header What it holds
webhook-id The event’s id. It’s the same on every attempt of a delivery, and when the event is sent again, so you can deduplicate on it.
webhook-timestamp When this attempt was made, in seconds since the epoch.
webhook-signature One signature per valid secret, separated by spaces. Each is v1, followed by the base64 of an HMAC-SHA256, as Verify a delivery explains.
POST /peal HTTP/1.1
Host: hooks.example.com
Content-Type: application/json
User-Agent: Peal-Webhooks
webhook-id: 019a6f40-2f6b-7a1c-9d2e-5b4c3a2f1e03
webhook-timestamp: 1794301203
webhook-signature: v1,Mfm1tWLUX4U95AK/DHjduW4e8bHrj/HztAOACn91BF4=

The body is a JSON object with these fields:

Field Type What it holds
id string The event’s id, as in webhook-id
type string What happened, such as post.published. It tells you what data holds.
version integer The version of data: 1
timestamp string When it happened, as an ISO 8601 instant in UTC. It isn’t when the request was sent.
workspaceExternalId string or null For a workspace a partner account owns, the partner’s own id for it, as it was when the event happened. Null when the partner gave none, and for any other workspace.
data object The event’s own fields, listed per event

The order of the fields in the body isn’t part of the contract.

What a payload never holds

Payloads are thin. They carry enough to tell you what happened, and no content:

A payload carries A payload never carries
Ids, states, codes, instants and permalinks What a post says, in any of its parts, nor its images or videos
A boost’s budget, as amounts What a comment or a reply says, nor who wrote a comment
Who a boost is shown to

The endpoint is a third party to the people who wrote the post: an address someone chose, which Peal can’t check beyond its being public. Thin payloads keep a workspace’s content behind API credentials and their permissions, where every read is checked. When your server needs the post, it reads it through the API with a key of its own: GET /api/v1/posts/{postId}.

Versions

Event types and their fields are a public contract. Peal adds event types, and fields to an event, without changing version, so ignore the fields and types you don’t know. Nothing is renamed or removed. A change your code couldn’t read would come as a new version, sent alongside this one.

In the OpenAPI document

The API’s OpenAPI document, at /api/openapi.json, describes every event in its webhooks section (OpenAPI 3.1): one entry per type, with the payload’s schema and the three webhook- headers. The API reference shows each one. The SDKs have no typed events, so read a payload with your language’s JSON parser, as the samples below do.

Verify a delivery

Anyone who learns the address can send it requests, so verify each one before acting on it. The signature covers the event’s id, the attempt’s timestamp and the body, joined with full stops:

<webhook-id>.<webhook-timestamp>.<body>

It’s an HMAC-SHA256 of that text, keyed with the bytes the secret holds in base64 after whsec_, and written in base64 after v1,. During a rotation, the header holds two signatures separated by a space. Accept the request when either one matches.

The Standard Webhooks libraries do all of this, the timestamp check included. The SDKs don’t receive webhooks (SDKs), so use your language’s library:

// npm install standardwebhooks
import { Webhook } from "standardwebhooks";

const webhook = new Webhook(process.env.PEAL_WEBHOOK_SECRET ?? "");

// `rawBody` is the request body as received, a string or a Buffer.
// Throws when the signature or the timestamp is wrong.
const event = webhook.verify(rawBody, {
  "webhook-id": request.headers["webhook-id"],
  "webhook-timestamp": request.headers["webhook-timestamp"],
  "webhook-signature": request.headers["webhook-signature"],
});
# pip install standardwebhooks
import os

from standardwebhooks.webhooks import Webhook

webhook = Webhook(os.environ["PEAL_WEBHOOK_SECRET"])

# `raw_body` is the request body as received, as bytes.
# Raises WebhookVerificationError when the signature or the timestamp is wrong.
event = webhook.verify(
    raw_body,
    {
        "webhook-id": request.headers["webhook-id"],
        "webhook-timestamp": request.headers["webhook-timestamp"],
        "webhook-signature": request.headers["webhook-signature"],
    },
)
// go get github.com/standard-webhooks/standard-webhooks/libraries
import (
	"io"
	"net/http"
	"os"

	standardwebhooks "github.com/standard-webhooks/standard-webhooks/libraries/go"
)

func handle(w http.ResponseWriter, r *http.Request) {
	webhook, err := standardwebhooks.NewWebhook(os.Getenv("PEAL_WEBHOOK_SECRET"))
	if err != nil {
		http.Error(w, "misconfigured", http.StatusInternalServerError)
		return
	}

	// The request body as received.
	rawBody, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "unreadable", http.StatusBadRequest)
		return
	}

	if err := webhook.Verify(rawBody, r.Header); err != nil {
		http.Error(w, "invalid signature", http.StatusBadRequest)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}
# gem install standardwebhooks
require "standardwebhooks"

webhook = StandardWebhooks::Webhook.new(ENV.fetch("PEAL_WEBHOOK_SECRET"))

# `raw_body` is the request body as received, as a string.
# Raises StandardWebhooks::WebhookVerificationError when the signature
# or the timestamp is wrong.
event = webhook.verify(raw_body, {
  "webhook-id" => request.env.fetch("HTTP_WEBHOOK_ID"),
  "webhook-timestamp" => request.env.fetch("HTTP_WEBHOOK_TIMESTAMP"),
  "webhook-signature" => request.env.fetch("HTTP_WEBHOOK_SIGNATURE"),
})
// Maven: com.standardwebhooks:standardwebhooks:1.2.0
import com.standardwebhooks.Webhook;
import com.standardwebhooks.exceptions.WebhookVerificationException;

Webhook webhook = new Webhook(System.getenv("PEAL_WEBHOOK_SECRET"));

// `rawBody` is the request body as received, as a string, and `headers`
// the request's headers, a Map<String, List<String>> (names in any case).
// Throws WebhookVerificationException when the signature or the timestamp
// is wrong.
webhook.verify(rawBody, headers);
<?php
// composer require standard-webhooks/standard-webhooks

$webhook = new \StandardWebhooks\Webhook(getenv('PEAL_WEBHOOK_SECRET'));

// The request body as received.
$rawBody = file_get_contents('php://input');

// Throws \StandardWebhooks\Exception\WebhookVerificationException when
// the signature or the timestamp is wrong.
$event = $webhook->verify($rawBody, [
    'webhook-id' => $_SERVER['HTTP_WEBHOOK_ID'],
    'webhook-timestamp' => $_SERVER['HTTP_WEBHOOK_TIMESTAMP'],
    'webhook-signature' => $_SERVER['HTTP_WEBHOOK_SIGNATURE'],
]);
// dotnet add package StandardWebhooks.StandardWebhooks
using System.Net;
using StandardWebhooks;

var webhook = new Webhook(Environment.GetEnvironmentVariable("PEAL_WEBHOOK_SECRET"));

// `rawBody` is the request body as received; `headers` carries the three
// webhook- headers. Throws WebhookVerificationException when the
// signature or the timestamp is wrong.
var headers = new WebHeaderCollection
{
    { "webhook-id", request.Headers["webhook-id"] },
    { "webhook-timestamp", request.Headers["webhook-timestamp"] },
    { "webhook-signature", request.Headers["webhook-signature"] },
};

webhook.Verify(rawBody, headers);
// cargo add standardwebhooks http
use standardwebhooks::Webhook;

let webhook = Webhook::new(&std::env::var("PEAL_WEBHOOK_SECRET")?)?;

// `raw_body` is the request body as received, as bytes, and `headers` the
// request's http::HeaderMap. Fails when the signature or the timestamp is
// wrong.
webhook.verify(&raw_body, &headers)?;

Without a library

The check is short:

Check the timestamp

Refuse a webhook-timestamp too far from your clock, ahead or behind. The libraries allow five minutes.

Compute the signature

Take the HMAC-SHA256 of <webhook-id>.<webhook-timestamp>.<body>, keyed with the bytes the secret holds in base64 after whsec_.

Compare

Compare it, in constant time, with each v1, signature in webhook-signature, and accept the request when any of them matches.

Each sample below uses only the standard library (Rust needs the hmac, sha2 and base64 crates) and allows five minutes on the timestamp:

import { createHmac, timingSafeEqual } from "node:crypto";

const tolerance = 5 * 60;

// `rawBody` is the request body as received, a string or a Buffer.
export const verify = (
  secret: string,
  headers: Record<string, string | undefined>,
  rawBody: string | Buffer
): boolean => {
  const id = headers["webhook-id"];
  const timestamp = headers["webhook-timestamp"];
  const signatures = headers["webhook-signature"];

  if (!id || !timestamp || !signatures) {
    return false;
  }

  // Too old or too far ahead: a request someone kept to send again.
  const age = Math.abs(Date.now() / 1000 - Number(timestamp));

  if (!(age <= tolerance)) {
    return false;
  }

  const key = Buffer.from(secret.replace(/^whsec_/, ""), "base64");
  const expected = createHmac("sha256", key)
    .update(`${id}.${timestamp}.`)
    .update(rawBody)
    .digest();

  // One signature per valid secret: any of them may match.
  return signatures.split(" ").some((signature) => {
    const [version, mac = ""] = signature.split(",");
    const given = Buffer.from(mac, "base64");

    return (
      version === "v1" &&
      given.length === expected.length &&
      timingSafeEqual(given, expected)
    );
  });
};
import base64
import binascii
import hashlib
import hmac
import time

TOLERANCE = 5 * 60


# `raw_body` is the request body as received, as bytes.
def verify(secret: str, headers: dict[str, str], raw_body: bytes) -> bool:
    msg_id = headers.get("webhook-id")
    timestamp = headers.get("webhook-timestamp")
    signatures = headers.get("webhook-signature")

    if not msg_id or not timestamp or not signatures or not timestamp.isdigit():
        return False

    # Too old or too far ahead: a request someone kept to send again.
    if abs(time.time() - int(timestamp)) > TOLERANCE:
        return False

    key = base64.b64decode(secret.removeprefix("whsec_"))
    expected = hmac.new(
        key, f"{msg_id}.{timestamp}.".encode() + raw_body, hashlib.sha256
    ).digest()

    # One signature per valid secret: any of them may match.
    for signature in signatures.split(" "):
        version, _, mac = signature.partition(",")
        if version != "v1":
            continue
        try:
            given = base64.b64decode(mac, validate=True)
        except binascii.Error:
            continue
        if hmac.compare_digest(given, expected):
            return True

    return False
import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"net/http"
	"strconv"
	"strings"
	"time"
)

const tolerance = 5 * time.Minute

// rawBody is the request body as received.
func Verify(secret string, header http.Header, rawBody []byte) bool {
	id := header.Get("webhook-id")
	timestamp := header.Get("webhook-timestamp")
	signatures := header.Get("webhook-signature")
	if id == "" || timestamp == "" || signatures == "" {
		return false
	}

	// Too old or too far ahead: a request someone kept to send again.
	seconds, err := strconv.ParseInt(timestamp, 10, 64)
	if err != nil {
		return false
	}
	age := time.Since(time.Unix(seconds, 0))
	if age > tolerance || age < -tolerance {
		return false
	}

	key, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(secret, "whsec_"))
	if err != nil {
		return false
	}
	mac := hmac.New(sha256.New, key)
	mac.Write([]byte(id + "." + timestamp + "."))
	mac.Write(rawBody)
	expected := mac.Sum(nil)

	// One signature per valid secret: any of them may match.
	for _, signature := range strings.Split(signatures, " ") {
		version, encoded, found := strings.Cut(signature, ",")
		if !found || version != "v1" {
			continue
		}
		given, err := base64.StdEncoding.DecodeString(encoded)
		if err == nil && hmac.Equal(given, expected) {
			return true
		}
	}
	return false
}
require "openssl"

TOLERANCE = 5 * 60

# `raw_body` is the request body as received, as a string.
def verify(secret, id, timestamp, signatures, raw_body)
  return false if [id, timestamp, signatures].any? { |value| value.nil? || value.empty? }
  return false unless timestamp.match?(/\A\d+\z/)

  # Too old or too far ahead: a request someone kept to send again.
  return false if (Time.now.to_i - timestamp.to_i).abs > TOLERANCE

  key = secret.delete_prefix("whsec_").unpack1("m0")
  expected = OpenSSL::HMAC.digest("SHA256", key, "#{id}.#{timestamp}.#{raw_body}")

  # One signature per valid secret: any of them may match.
  signatures.split(" ").any? do |signature|
    version, mac = signature.split(",", 2)
    next false unless version == "v1" && mac

    given = mac.unpack1("m")
    given.bytesize == expected.bytesize &&
      OpenSSL.fixed_length_secure_compare(given, expected)
  end
end
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public final class WebhookSignature {
    private static final long TOLERANCE_SECONDS = 5 * 60;

    // `rawBody` is the request body as received, as bytes.
    public static boolean verify(
            String secret, String id, String timestamp, String signatures, byte[] rawBody)
            throws GeneralSecurityException {
        if (id == null || timestamp == null || signatures == null) {
            return false;
        }

        // Too old or too far ahead: a request someone kept to send again.
        long seconds;
        try {
            seconds = Long.parseLong(timestamp);
        } catch (NumberFormatException e) {
            return false;
        }
        long now = Instant.now().getEpochSecond();
        if (seconds < now - TOLERANCE_SECONDS || seconds > now + TOLERANCE_SECONDS) {
            return false;
        }

        byte[] key = Base64.getDecoder().decode(secret.replaceFirst("^whsec_", ""));
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(key, "HmacSHA256"));
        mac.update((id + "." + timestamp + ".").getBytes(StandardCharsets.UTF_8));
        byte[] expected = mac.doFinal(rawBody);

        // One signature per valid secret: any of them may match.
        for (String signature : signatures.split(" ")) {
            String[] parts = signature.split(",", 2);
            if (parts.length != 2 || !parts[0].equals("v1")) {
                continue;
            }
            byte[] given;
            try {
                given = Base64.getDecoder().decode(parts[1]);
            } catch (IllegalArgumentException e) {
                continue;
            }
            if (MessageDigest.isEqual(given, expected)) {
                return true;
            }
        }
        return false;
    }
}
<?php
const TOLERANCE = 5 * 60;

// $rawBody is the request body as received.
function verify(string $secret, ?string $id, ?string $timestamp, ?string $signatures, string $rawBody): bool
{
    if (!$id || !$timestamp || !$signatures || !ctype_digit($timestamp)) {
        return false;
    }

    // Too old or too far ahead: a request someone kept to send again.
    if (abs(time() - (int) $timestamp) > TOLERANCE) {
        return false;
    }

    $key = base64_decode(preg_replace('/^whsec_/', '', $secret), true);
    if ($key === false) {
        return false;
    }
    $expected = hash_hmac('sha256', $id . '.' . $timestamp . '.' . $rawBody, $key, true);

    // One signature per valid secret: any of them may match.
    foreach (explode(' ', $signatures) as $signature) {
        [$version, $mac] = array_pad(explode(',', $signature, 2), 2, '');
        $given = base64_decode($mac, true);
        if ($version === 'v1' && $given !== false && hash_equals($expected, $given)) {
            return true;
        }
    }

    return false;
}
using System.Linq;
using System.Security.Cryptography;
using System.Text;

public static class WebhookSignature
{
    private const long ToleranceSeconds = 5 * 60;

    // `rawBody` is the request body as received, as bytes.
    public static bool Verify(
        string secret, string? id, string? timestamp, string? signatures, byte[] rawBody)
    {
        if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(timestamp)
            || string.IsNullOrEmpty(signatures))
        {
            return false;
        }

        // Too old or too far ahead: a request someone kept to send again.
        if (!long.TryParse(timestamp, out var seconds))
        {
            return false;
        }
        var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        if (seconds < now - ToleranceSeconds || seconds > now + ToleranceSeconds)
        {
            return false;
        }

        var key = Convert.FromBase64String(
            secret.StartsWith("whsec_") ? secret["whsec_".Length..] : secret);
        var content = Encoding.UTF8.GetBytes($"{id}.{timestamp}.").Concat(rawBody).ToArray();
        var expected = HMACSHA256.HashData(key, content);

        // One signature per valid secret: any of them may match.
        foreach (var signature in signatures.Split(' '))
        {
            var parts = signature.Split(',', 2);
            if (parts.Length != 2 || parts[0] != "v1")
            {
                continue;
            }
            var given = new byte[expected.Length];
            if (Convert.TryFromBase64String(parts[1], given, out var written)
                && written == expected.Length
                && CryptographicOperations.FixedTimeEquals(given, expected))
            {
                return true;
            }
        }
        return false;
    }
}
// cargo add hmac sha2 base64
use base64::{engine::general_purpose::STANDARD, Engine};
use hmac::{Hmac, Mac};
use sha2::Sha256;
use std::time::{SystemTime, UNIX_EPOCH};

const TOLERANCE: u64 = 5 * 60;

// `raw_body` is the request body as received, as bytes.
pub fn verify(secret: &str, id: &str, timestamp: &str, signatures: &str, raw_body: &[u8]) -> bool {
    if id.is_empty() || signatures.is_empty() {
        return false;
    }

    // Too old or too far ahead: a request someone kept to send again.
    let Ok(sent) = timestamp.parse::<u64>() else {
        return false;
    };
    let Ok(now) = SystemTime::now().duration_since(UNIX_EPOCH) else {
        return false;
    };
    if now.as_secs().abs_diff(sent) > TOLERANCE {
        return false;
    }

    let Ok(key) = STANDARD.decode(secret.strip_prefix("whsec_").unwrap_or(secret)) else {
        return false;
    };

    // One signature per valid secret: any of them may match.
    signatures.split(' ').any(|signature| {
        let Some(("v1", mac)) = signature.split_once(',') else {
            return false;
        };
        let Ok(given) = STANDARD.decode(mac) else {
            return false;
        };
        let Ok(mut expected) = Hmac::<Sha256>::new_from_slice(&key) else {
            return false;
        };
        expected.update(format!("{id}.{timestamp}.").as_bytes());
        expected.update(raw_body);
        // verify_slice compares in constant time.
        expected.verify_slice(&given).is_ok()
    })
}

Security

Follow these practices on the server that receives deliveries:

Practice Why
Verify every request before acting on it Check the bytes that arrived, and answer an error to a request that doesn’t verify.
Refuse old timestamps A webhook-timestamp more than a few minutes from your clock is a request someone kept to send again. The libraries allow five minutes.
Deduplicate on webhook-id A delivery made twice is then handled once.
Answer fast, work later Answer a 2xx as soon as the request is verified and stored, and do the work afterwards. An answer that takes more than 15 seconds counts as a failure, and the delivery comes again.
Keep the secret secret Keep it where your server keeps its other secrets, never in code or in logs, and rotate it if it may have leaked.
Keep secrets out of the address Peal shows an endpoint’s address to the members who manage webhooks. The signature is what proves a request comes from Peal.
Don’t filter on Peal’s address Requests come from the Peal server’s addresses. On your own instance those are yours to know, and Peal Cloud doesn’t promise to keep its own fixed.
Confirm before anything that can’t be undone A payload says what happened when it was sent. Read the post or the channel through the API before you act on it for good.

Was this page helpful?