Webhooks: the Provider’s Side

An application that receives webhooks has two halves to test: what it does when an event arrives, and what the sender does when the application does not take one. The first is easy to try by hand. The second needs a sender that signs as the provider signs, waits as the provider waits, and sends again as the provider does.

serve --webhooks FILE has the mock play that provider beside the API it already serves. The application under test asks the mock for a payment; the mock answers, and a moment later sends the webhook the real provider would send, signed as the provider signs it. What the application does not take is sent again on the schedule the file names. Every sending is written down, and mockforge webhooks judge reads what was written: an event counts as taken only when the application answered a sending with a success, and as reacted to only when the request the file names reached the mock in time. An event can also be sent as a forger would send it, and then counts as it should only when the application refused it.

Begin with a starter

mockforge webhooks write stripe writes stripe.webhooks.json, a file to begin with: two events whose bodies are Stripe’s documented examples, to be sent to http://127.0.0.1:4000/webhooks/stripe. Set to to the application’s endpoint. There is a starter for each provider the mock can sign: clerk, discord, github, lemon-squeezy, paddle, resend, sendgrid, shopify, slack, stripe, svix and twilio.

mockforge webhooks write stripe
wrote stripe.webhooks.json
  provider  stripe, to http://127.0.0.1:4000/webhooks/stripe
  events    payment-intent-succeeded, setup-intent-created
next: set `to` to the application's endpoint, then serve it with `mockforge serve --spec provider.json --webhooks stripe.webhooks.json --webhook-log deliveries.jsonl`

--output FILE names another file; a file that is there is not replaced.

The file

A file of webhooks names the provider, where its events are sent, and the events:

{
  "kind": "mockforge-webhooks",
  "schema_version": 1,
  "id": "stripe",
  "title": "A shop's payments",
  "provider": "stripe",
  "to": "http://127.0.0.1:4000/webhooks/stripe",
  "secret": "whsec_demo",
  "retry": { "attempts": 3, "first": "100ms", "multiplier": 2, "max": "400ms" },
  "events": [
    {
      "id": "payment-intent-succeeded",
      "title": "A payment succeeded",
      "body": {
        "id": "evt_1",
        "type": "payment_intent.succeeded",
        "data": { "object": { "id": "pi_1" } }
      },
      "on": {
        "after": "100ms",
        "when": { "method": "POST", "path": "/v1/payment_intents", "answered": 200 }
      },
      "reacts": { "method": "GET", "path": "/v1/events/evt_1", "within": "5s" }
    },
    {
      "id": "payout-paid",
      "body": { "id": "evt_2", "type": "payout.paid" },
      "on": { "after": "0s" }
    }
  ]
}
  • provider and secret name the provider as the table of signatures names it, and what it signs with. All twelve can be sent. Two sign with a key pair, and the application checks with the public key, which webhooks check prints. For Discord, secret is the 32 bytes of an Ed25519 key in hexadecimal, as openssl rand -hex 32 gives. For SendGrid, it is an ECDSA key over secp256k1, as PKCS#8 in base64, as openssl ecparam -name secp256k1 -genkey -noout | openssl pkcs8 -topk8 -nocrypt -outform DER | base64 -w0 gives. The secret is made up for the test, and is never written into a log.
  • to is the application’s endpoint. The mock sends there and nowhere else: it follows no redirect, and nothing that reaches the mock can change the address.
  • retry says how long the provider waits before sending an event a second time (first), how each wait grows (multiplier), the longest wait (max), and the most attempts. again_on says what has it send again: not-taken, any answer that is not a success and no answer at all, or server-error, an answer that is a server’s own error and no answer at all.
  • on says what fires an event. after alone: it is sent that long after the mock starts to serve. With when, a request of the provider’s API fires it, after after the mock answered that request; at_most says how many times a request may fire it, once unless said.
  • reacts names the request the application is expected to make of the provider’s API once it has taken the event, and how soon.
  • signature says how an event is signed: as the provider signs it, unless said otherwise, or as a forger would send it, below.
  • expect says what the application is to do with an event, taken or refused. Without it, an event signed as the provider signs it is to be taken, and one sent as a forger would is to be refused.

A time is a number and a unit: 500ms, 30s, 15m or 2h.

The API the application calls

The mock serves the provider’s API from a spec, as it serves any API. The event above is fired by the first of these two operations, and the reaction asked for is the second:

{
  "openapi": "3.0.3",
  "info": { "title": "A payment provider", "version": "1" },
  "paths": {
    "/v1/payment_intents": {
      "post": {
        "responses": {
          "200": {
            "description": "A payment intent",
            "content": {
              "application/json": {
                "example": { "id": "pi_1", "status": "requires_confirmation" }
              }
            }
          }
        }
      }
    },
    "/v1/events/{id}": {
      "get": {
        "responses": {
          "200": {
            "description": "An event",
            "content": {
              "application/json": {
                "example": { "id": "evt_1", "type": "payment_intent.succeeded" }
              }
            }
          }
        }
      }
    }
  }
}

Run it

Save the two documents as stripe.webhooks.json and provider.json, start the application with http://127.0.0.1:8080 as its provider, and serve both:

mockforge serve --spec provider.json --webhooks stripe.webhooks.json --webhook-log deliveries.jsonl --exchange-log run.jsonl --http-port 8080

Add --mcp-mock, and the inbox is a set of tools of the mock’s MCP server, below.

The payout is sent at the start. When the application asks the provider for a payment, the mock answers it and, 100 ms later, sends payment_intent.succeeded to the application with a header of the form Stripe-Signature: t=<time>,v1=<hex>, computed as Stripe computes it: the HMAC of the time and the body, with the secret. An application that answers with anything but a success, or with nothing, is sent the event again: after 100 ms, then 200 ms.

Send as a forger would

An application is to refuse a webhook that is not signed properly. To try that, an event of the file says with signature how it is to be sent:

  • none: with none of the provider’s headers, as a forger without the secret sends it.
  • another-secret: signed with a secret, or a key, that is not the file’s. The signature has the provider’s shape and is not the provider’s.
  • stale: signed as the provider signs it, but for an hour before, past every tolerance the table names. Only a provider that signs the time can send it: a file that asks it of another is refused with the reason.

Add these three to the events of the file above:

[
  {
    "id": "forged-unsigned",
    "body": { "id": "evt_3", "type": "payout.failed" },
    "on": { "after": "0s" },
    "signature": "none"
  },
  {
    "id": "forged-another-secret",
    "body": { "id": "evt_4", "type": "payout.failed" },
    "on": { "after": "0s" },
    "signature": "another-secret"
  },
  {
    "id": "forged-stale",
    "body": { "id": "evt_5", "type": "payout.failed" },
    "on": { "after": "0s" },
    "signature": "stale"
  }
]

The file expects such an event to be refused; expect says otherwise where the file wants it taken. An application that checks signatures refuses each of them, so the provider sends each again until the file’s attempts are spent. The judge finds the run kept:

webhooks `stripe`: A shop's payments
deliveries deliveries.jsonl, with run.jsonl
events
  taken       payment-intent-succeeded   attempt 1 answered 200; exchange 2 asked for it 2 ms after
  taken       payout-paid                attempt 1 answered 200
  refused     forged-unsigned            3 attempt(s), answered 400, 400, 400
  refused     forged-another-secret      3 attempt(s), answered 400, 400, 400
  refused     forged-stale               3 attempt(s), answered 400, 400, 400
5 of 5 event(s) ended as the file expects: 2 taken, 3 refused; reacted: 1 of 1 named

An application that took one fails the run: the judge says of it taken, and that the file expects it refused, and the exit code is 1. webhooks check names such an event forged, among the events the provider can send.

An event’s body can also stand twice in a file, under two ids, and an event can be given an after that puts it before one the provider would send later. To the mock each is only another event of the file, and what the application is to do with it is the file’s to say with expect.

Check the file before a run

mockforge webhooks check reads the file as serve does, signs each event, and holds the signature to the table’s own check. With --spec, it also looks up every operation on.when and reacts names, a path with its parameters matching any request it stands for.

mockforge webhooks check --webhooks stripe.webhooks.json --spec provider.json
webhooks `stripe`: A shop's payments
provider  stripe, to http://127.0.0.1:4000/webhooks/stripe
events
  signed     payment-intent-succeeded   `Stripe-Signature` holds; every operation it names is declared
  signed     payout-paid                `Stripe-Signature` holds; every operation it names is declared
2 of 2 event(s) can be sent as `stripe` signs them, with every operation named declared by the spec

The exit code is 0 when every event can be sent, 1 when one cannot, and 2 when a file cannot be read or is not one this version reads.

The inbox

The mock keeps every sending. Its management API serves them, at the paths below, and asks for the management credential, as every request to the control plane does:

  • GET /__mockforge/api/webhooks/events: the events of the file.
  • POST /__mockforge/api/webhooks/events/{id}/send: send an event now.
  • GET /__mockforge/api/webhooks/deliveries: every sending, the earliest first: the event, the attempt, the headers as sent, the body, the status the application answered with and the start of its answer.
  • GET /__mockforge/api/webhooks/deliveries/{seq}: one sending.
  • POST /__mockforge/api/webhooks/deliveries/{seq}/replay: send a sending again as it was recorded, the same bytes under the same signature, so the application sees a delivery it has taken already.

The admin UI shows the same inbox on its page Webhook Inbox, where the mock is started with the admin UI: the events of the file, each with a button to send it now, and the sendings, the latest first, with what became of each and a button to replay one. There the inbox is behind the admin UI’s own access check and not the management credential: a login, or the credential where the mock has one. On a loopback address with neither set, the pages of the admin UI are readable without a login, this one with them; to send or to replay always asks for one.

The inbox as MCP tools

With --mcp-mock beside --webhooks, the mock’s MCP server gains four tools over the inbox, for an agent that is to read what was sent, or to have the provider send:

  • webhooks_list_deliveries: one line per sending, earliest first: its number, the event, the attempt, what became of it, what fired it and when. event narrows the list to one event.
  • webhooks_get_delivery: one sending by its number, as a document: the headers as sent, the start of the body, and what the application answered.
  • webhooks_send_event: send an event of the file now, signed as the provider signs it, and again until the application takes it.
  • webhooks_replay_delivery: send a sending again as it was recorded.

A catalog may give any tool the same answer with "result": { "inbox": "list" }, get, send or replay.

The mock’s MCP server asks for no credential, as it never does: whoever can reach /mcp can list the sendings, send an event and replay one, as it can call any operation of the mock’s API. Serve the mock where only the test can reach it.

Judge the run

mockforge webhooks judge --webhooks stripe.webhooks.json --deliveries deliveries.jsonl --exchanges run.jsonl
webhooks `stripe`: A shop's payments
deliveries deliveries.jsonl, with run.jsonl
events
  taken       payment-intent-succeeded   attempt 1 answered 200; exchange 2 asked for it 2 ms after
  taken       payout-paid                attempt 1 answered 200
2 of 2 event(s) ended as the file expects: 2 taken; reacted: 1 of 1 named
  • An event is taken, refused, unreachable or never sent, by what the delivery log holds: taken when the application answered a sending with a success; refused when every sending was answered and none with a success; unreachable when nothing answered the last sending; never sent when the log holds no sending of it.
  • A reaction is judged by the exchange log. The request reacts names must reach the mock within the time the file names, counted from the sending that was taken. Without --exchanges, nothing is said of a reaction.
  • A replay is left out of the verdict, and counted.
  • What became of an event is held to what the file expects of it, by expect and by how the event is signed. The last line counts the events that ended as the file expects.
  • The exit code is 0 when every event ended as the file expects and every named request came in time, 1 otherwise, and 2 when a file cannot be read.

--format json gives the judgement as one document.

What is not here yet

  • The starter’s bodies are the providers’ documented examples, trimmed where they run long. GitHub publishes no literal body for push or ping; those two are composed from its field tables.
  • A forged sending is one of three kinds: no signature, another secret, or an hour old. Nothing else is sent on purpose.