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

# Verify webhook signatures

> Check that a webhook request came from PostSyncer before you act on it

PostSyncer signs every webhook request with the [Standard Webhooks](https://www.standardwebhooks.com) scheme. Verify the signature before you trust the body, so nobody else can post to your address and pass as PostSyncer.

## Headers

| Header | Value |
| - | - |
| `webhook-id` | The event id. The same id is sent again on every retry, so you can use it to ignore repeats. |
| `webhook-timestamp` | Unix time in seconds when this attempt was sent |
| `webhook-signature` | `v1,` followed by a base64 HMAC-SHA256 signature |

## Your secret

The signing secret starts with `whsec_`. Create returns it in full. Later you can see it again in the app under **Settings → Agents & Integrations → Webhooks** (Reveal), or rotate it there. Everything after `whsec_` is base64; decode it to get the key bytes.

## How the signature is made

1. Take the exact raw request body, before any JSON parsing.
2. Build the string `{webhook-id}.{webhook-timestamp}.{body}`.
3. Compute an HMAC-SHA256 of it with the decoded key.
4. Base64 encode the result and compare it, in constant time, to the value after `v1,` in `webhook-signature`.
5. Refuse requests whose timestamp is more than 5 minutes from your clock.

## Use a library

The Standard Webhooks libraries do all of this for you.

```javascript Node.js theme={null}
import { Webhook } from 'standardwebhooks';

const wh = new Webhook(process.env.POSTSYNCER_WEBHOOK_SECRET); // whsec_...

// rawBody is the unparsed request body string
export function handle(rawBody, headers) {
  const event = wh.verify(rawBody, {
    'webhook-id': headers['webhook-id'],
    'webhook-timestamp': headers['webhook-timestamp'],
    'webhook-signature': headers['webhook-signature'],
  });

  console.log(event.type, event.data);
}
```

```python Python theme={null}
import os
from standardwebhooks.webhooks import Webhook

wh = Webhook(os.environ["POSTSYNCER_WEBHOOK_SECRET"])  # whsec_...

def handle(raw_body: str, headers: dict):
    event = wh.verify(raw_body, headers)
    print(event["type"], event["data"])
```

```php PHP theme={null}
<?php

$secret = getenv('POSTSYNCER_WEBHOOK_SECRET'); // whsec_...
$body = file_get_contents('php://input');

$id = $_SERVER['HTTP_WEBHOOK_ID'] ?? '';
$timestamp = $_SERVER['HTTP_WEBHOOK_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_WEBHOOK_SIGNATURE'] ?? '';

if (abs(time() - (int) $timestamp) > 300) {
    http_response_code(400);
    exit;
}

$key = base64_decode(substr($secret, strlen('whsec_')));
$expected = 'v1,'.base64_encode(hash_hmac('sha256', "{$id}.{$timestamp}.{$body}", $key, true));

// The header can hold several space-separated signatures.
$valid = false;
foreach (explode(' ', $signature) as $candidate) {
    $valid = $valid || hash_equals($expected, $candidate);
}

if (! $valid) {
    http_response_code(400);
    exit;
}

$event = json_decode($body, true);
```

## Answer fast

Reply with a `2xx` status within 10 seconds, and do slow work after you have answered. An answer of `408`, `429` or `5xx`, or no answer, is tried again up to 7 times over about 41 hours. See [Webhooks](https://docs.postsyncer.com/api-reference/webhooks/overview.md) for the full rules.

## Test it

Use [Test webhook](https://docs.postsyncer.com/api-reference/webhooks/test.md) to send a signed `webhook.test` event to your endpoint, then check [List deliveries](https://docs.postsyncer.com/api-reference/webhooks/deliveries.md) for what your server answered.


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