> ## Documentation Index
> Fetch the complete documentation index at: https://docs.postploy.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Postploy's MCP server for posting is https://api.postploy.com/mcp and signs in with OAuth. The REST API base URL is https://api.postploy.com/public/v1 and takes an API key as a Bearer token. The API calls channels accounts.

# Verify requests

> Check each request's signature so you know it came from Postploy.

Anyone can send a request to your endpoint. The `Postploy-Signature` header proves a request came from Postploy and wasn't changed on the way.

```http theme={null}
Postploy-Signature: t=1894698004,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
```

* `t` is when the request was signed, in Unix seconds.
* `v1` is an HMAC-SHA256 of `t`, a dot and the raw request body, made with your endpoint's secret and written in hex.

## Check a signature

**1. Read the raw body**

Use the body exactly as it arrived, before any JSON parsing. Parsing and turning it back into text can change it, and then the signature won't match.

**2. Make your own signature**

Join `t`, a `.` and the raw body. Make an HMAC-SHA256 of that with your secret, including its `whsec_` start, and write it in hex.

**3. Compare**

The request is real if your signature matches any `v1` in the header. Compare in constant time.

**4. Check the time**

Reject requests where `t` is more than 5 minutes away from now. This stops someone sending an old request again.

```js Node.js (Express) theme={null}
import express from "express";
import { createHmac, timingSafeEqual } from "node:crypto";

const secret = process.env.POSTPLOY_WEBHOOK_SECRET;

export function verify(rawBody, header, secret) {
  const pairs = (header ?? "").split(",").map((part) => part.split("="));
  const t = pairs.find(([key]) => key === "t")?.[1];
  if (!t || Math.abs(Date.now() / 1000 - Number(t)) > 300) return false;
  const expected = createHmac("sha256", secret).update(`${t}.${rawBody}`).digest("hex");
  return pairs.some(
    ([key, value]) =>
      key === "v1" &&
      value?.length === expected.length &&
      timingSafeEqual(Buffer.from(value), Buffer.from(expected)),
  );
}

const app = express();

app.post("/postploy/webhooks", express.raw({ type: "application/json" }), (req, res) => {
  const rawBody = req.body.toString("utf8");
  if (!verify(rawBody, req.get("Postploy-Signature"), secret)) {
    return res.sendStatus(400);
  }
  const event = JSON.parse(rawBody);
  console.log(event.type, event.data);
  res.sendStatus(200);
});

app.listen(3000);
```

```python Python (Flask) theme={null}
import hashlib
import hmac
import os
import time

from flask import Flask, abort, request

SECRET = os.environ["POSTPLOY_WEBHOOK_SECRET"]


def verify(raw_body: bytes, header: str, secret: str) -> bool:
    pairs = [part.split("=", 1) for part in header.split(",") if "=" in part]
    t = next((value for key, value in pairs if key == "t"), "")
    if not t.isdigit() or abs(time.time() - int(t)) > 300:
        return False
    signed = t.encode() + b"." + raw_body
    expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return any(key == "v1" and hmac.compare_digest(value, expected) for key, value in pairs)


app = Flask(__name__)


@app.post("/postploy/webhooks")
def postploy_webhook():
    if not verify(request.get_data(), request.headers.get("Postploy-Signature", ""), SECRET):
        abort(400)
    event = request.get_json()
    print(event["type"], event["data"])
    return "", 200
```

> [!TIP]
>
> In Next.js, read the raw body with `await request.text()` in your route handler, then parse it yourself after the check.

## Test your code

Run your signature code on these values, leaving out the time check. It should make the signature shown.

| | |
| - | - |
| Secret | `whsec_test` |
| `t` | `1700000000` |
| Body | `{"id":"2e3f4a5b-6c7d-4e8f-9a0b-1c2d3e4f5a6b","type":"ping"}` |
| Signature | `64db98be76217891e20e67247527d9c3a59d1264c769ea03d65a1f191283cc90` |

Then send a real test from **Settings › Webhooks**, or with [Send a test event](https://docs.postploy.com/api-reference/webhooks/send-a-test-event.md).

## Change the secret

If a secret leaks, [roll it](https://docs.postploy.com/api-reference/webhooks/roll-the-secret.md) to get a new one. For the next 24 hours, each request carries two `v1` signatures, one made with the new secret and one with the old. Code that accepts any matching `v1` keeps working while you swap the new secret in.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.