How to test webhooks your API receives
Webhook endpoints are odd APIs. You don't call them; a payment provider, GitHub or your own backend does. They're hard to trigger on demand, they're signed, they get retried, and the events can arrive twice or out of order.
The usual way to test them is "do a real payment in the sandbox and watch the logs". That's slow and it can't test the edge cases. Here's how to test the endpoint directly, from your API client.
Step 1: Capture a real payload
Get one real event from the sender: most providers show recent deliveries with their bodies in their dashboard, or log one from your endpoint in a test environment. Remove anything personal and save it as the body of a request:
POST {{baseUrl}}/webhooks/payments
Content-Type: application/json
{"id": "evt_{{eventId}}", "type": "payment.succeeded", "created": {{timestamp}}, "data": {"paymentId": "pay_123", "amount": 4200}}{{eventId}} and {{timestamp}} are set by the script below, so each send looks like a fresh event.
Step 2: Sign it like the sender does
Most senders sign webhooks with HMAC-SHA256 over the raw body, often with a timestamp. The details differ (header names, what's signed, hex or base64), so check your provider's docs. This script covers the common t=…,v1=… style:
// Pre-request script
const CryptoJS = require('crypto-js');
const timestamp = Math.floor(Date.now() / 1000);
pm.environment.set('timestamp', String(timestamp));
pm.environment.set('eventId', pm.variables.replaceIn('{{$randomUUID}}'));
// Resolve the variables first, so we sign exactly the bytes that are sent
const body = pm.variables.replaceIn(pm.request.body.raw || '');
const secret = pm.environment.get('webhookSecret');
const signature = CryptoJS.HmacSHA256(`${timestamp}.${body}`, secret).toString(CryptoJS.enc.Hex);
pm.request.headers.set('Webhook-Signature', `t=${timestamp},v1=${signature}`);Keep webhookSecret as a secret variable in the environment, the same secret your test server is configured with.
Step 3: The signature tests
Make a request for each case. The valid one first:
pm.test('valid webhook is accepted', () => {
pm.expect(pm.response.code).to.be.within(200, 299);
});Then duplicate it and break one thing per copy:
| Copy | Change | Expected |
|---|---|---|
| Missing signature | Remove the headers.set line | 400 or 401 |
| Wrong secret | Sign with 'wrong' | 400 or 401 |
| Tampered body | Sign body.replace('4200', '1') instead of body, so the signature doesn't match what's sent | 400 or 401 |
| Old timestamp | Use timestamp - 600 (10 minutes ago) | 400 or 401 |
The old-timestamp case matters: without it, anyone who captures one valid webhook can replay it forever.
Step 4: Duplicates and retries
Senders retry when your endpoint is slow or returns an error, so the same event can arrive several times. Your handler should process it once.
Duplicate the valid request without the pre-request lines that set a new eventId, so it sends the same event again:
pm.test('duplicate event is acknowledged', () => {
pm.expect(pm.response.code).to.be.within(200, 299);
});Then check the side effect: one payment marked as paid, one email queued, one row in your events table, through whatever read endpoint your API has.
Step 5: Out of order
payment.refunded can arrive before payment.succeeded. Send them in that order and check that the final state is still right, or that the early event is rejected with a status the sender will retry.
Run it in CI
Put the requests in a Webhooks folder in the order above and run it against staging with the test secret from your CI secrets:
requestarium run-collection "Payments API" --folder Webhooks --env Staging \
--env-var webhookSecret=WEBHOOK_TEST_SECRETNow a refactor that breaks signature checking fails the build, instead of silently accepting forged events in production.