Integration
PaymentHooks CLI
Develop webhook handlers locally: receive every event for your account on your machine as it happens, signed like a real delivery.
Install
Requires Node.js 18 or later.
npm install -g @paymenthooks/cli
ph --version
# or without installing
npx @paymenthooks/cli --helpAPI key
Create a secret key under Developers > API Keys. Only phk_live_secret keys are accepted. Events are test or live according to your account's Platform Mode, not the key.
| Command | Scope |
|---|---|
ph listen | webhooks:read |
ph trigger, ph events resend, ph secret rotate | webhooks:write |
export PAYMENTHOOKS_API_KEY=phk_live_...Forward events: ph listen
ph listen --forward-to localhost:3000/webhooks
# Ready. Forwarding TEST events to http://localhost:3000/webhooks
# Signing secret for your handler: whsec_8f1c...
# Press Ctrl+C to stop.
# 2026-10-25T12:01:07.512Z payment.succeeded ph_evt_185c59f9d2ad673199ef63dd4e7a1b0c -> [200]
# 2026-10-25T12:01:09.004Z payment.succeeded ph_evt_9a1b2c3d4e5f60718293a4b5c6d7e8f9 -> failed (connect ECONNREFUSED 127.0.0.1:3000)- Events are pushed as they are dispatched (no polling), even when no webhook endpoint is registered.
- Each line shows the event type, event id and your handler's HTTP status, or
failed (reason). A failed forward is not retried; listening continues. --events payment.succeeded,checkout.session.completedforwards only those types;--printalso prints each event.- Ctrl+C stops it and exits 0.
The request body is the exact event envelope endpoints receive. Headers:
| Header | Value |
|---|---|
Content-Type | application/json |
X-PaymentHooks-Signature | t=<unix seconds>,v1=<hex HMAC-SHA256>, signed with your CLI secret |
X-PaymentHooks-Event | Event type |
X-PaymentHooks-Delivery | cli_<16 hex>, unique per forward |
X-PaymentHooks-Timestamp | Unix seconds when forwarded |
X-PaymentHooks-Api-Version | 2026-04-22 |
User-Agent | PaymentHooks-CLI |
Production deliveries use ph_whd_ delivery ids and the PaymentHooks-Webhook/1.0 user agent; everything else matches. De-duplicate on the envelope id, never on the delivery id.
Verify forwarded events
Forwarded events are signed with your account's CLI secret, printed when ph listen starts. Set it as the webhook secret in your local environment; the handler code stays the same as in production.
import { verifyWebhookSignature, SIGNATURE_HEADER } from "@paymenthooks/sdk";
const event = await verifyWebhookSignature({
payload: rawBody, // raw request bytes, never re-serialised
signature: req.header(SIGNATURE_HEADER) ?? "",
secret: process.env.PAYMENTHOOKS_WEBHOOK_SECRET!, // CLI secret locally, endpoint secret in production
});- Created the first time you run
ph listen; it stays the same until you rotate it. Stored encrypted, shared by all of your account's keys. - Separate from endpoint secrets, so it cannot sign a delivery your production endpoint would accept. If it leaks, run
ph secret rotate. --signing-secret whsec_...signs with a secret of your choice instead.- In other languages, verify as in Verifying signatures.
Send a test event: ph trigger
ph trigger payment.succeeded
# Triggered payment.succeeded (ph_evt_3f9c7a1e5b2d8f4c6a0e9b3d7f1c5a8e).
# It is sent to running `ph listen` sessions only, never to your registered endpoints.- Builds a sample event with the same builders as real events and sends it to your running
ph listensessions. It is never delivered to registered endpoints, so a sample payment cannot reach production; use the simulator under Developers > Webhooks to test an endpoint. - Samples have
livemode: falseandmetadata.simulated: "true". - The type must be one PaymentHooks sends (see Webhook events); anything else fails with
VALIDATION_ERROR. - Succeeds even when no
ph listenis running. Limited to 20 per minute (RATE_LIMITED), shared with the dashboard simulator.
Redeliver a logged event: ph events resend
ph events resend ph_whd_1a3c5e7f9b2d4f6a8c0e2b4d6f8a1c3e
# Redelivered ph_evt_9c41e0b27f5a3d18e6b2c7a4e1f0d3b5: endpoint answered 200 (success).Sends a delivery from Developers > Webhooks > Logsto its endpoint again with the original event id, so your handler's de-duplication still applies. Exits 1 with REDELIVERY_FAILED when the endpoint does not answer 2xx; a disabled endpoint fails with ENDPOINT_INACTIVE and an unknown id with NOT_FOUND. A log from before envelopes were stored is resent with a new event id, and the command says so.
Replace the signing secret: ph secret rotate
ph secret rotate
# New CLI signing secret: whsec_...
# The old secret stops working for new `ph listen` sessions; restart running ones and update your local .env.Options and environment
| Option | Applies to | Meaning |
|---|---|---|
--forward-to <url> | listen | Required. localhost:3000/webhooks (http assumed) or a full URL. |
--events <a,b> | listen | Only forward these event types (filtered by the CLI). |
--print | listen | Also print each event as JSON. |
--signing-secret <s> | listen | Sign with this secret instead of the CLI secret. |
--api-key <key> | all | Secret key. Default PAYMENTHOOKS_API_KEY. |
--api-base <url> | all | API base. Default PAYMENTHOOKS_API_BASE, else https://paymenthooks.com/api/v1; /api/v1 is appended when missing. |
--help, ph help | Usage. | |
--version | Version. |
Options take --name value or --name=value. Environment: PAYMENTHOOKS_API_KEY (secret key) and PAYMENTHOOKS_API_BASE (for example http://localhost:6600 for a local PaymentHooks).
Exit codes and errors
Exit code 0 means success (including Ctrl+C on ph listen); 1 means failure, printed as Error [CODE]: message.
| Code | Meaning |
|---|---|
MISSING_API_KEY | No key set. |
INVALID_API_KEY | Key does not start with phk_live_. |
INVALID_API_BASE | --api-base is not an http(s) URL. |
UNKNOWN_COMMAND | Unknown command or subcommand. |
MISSING_FLAG_VALUE | An option was given without a value. |
MISSING_FORWARD_TO / INVALID_FORWARD_TO | --forward-to missing or not a URL. |
MISSING_EVENT_TYPE | ph trigger without an event type. |
MISSING_DELIVERY_ID | ph events resend without an id. |
REDELIVERY_FAILED | The endpoint did not answer 2xx to a resend. |
STREAM_CLOSED | The server closed the event stream; run ph listen again. |
STREAM_FAILED | The connection to the event stream failed. |
STREAM_PROTOCOL / NO_STREAM_BODY | Unexpected stream response. |
HTTP_<status> | An API error without an error code. |
API errors keep the server's code: UNAUTHORIZED, INSUFFICIENT_SCOPE, FORBIDDEN, VALIDATION_ERROR, RATE_LIMITED, NOT_FOUND, ENDPOINT_INACTIVE. An unexpected internal failure prints Error: <stack trace>.
Programmatic use
The package is ESM only (import, not require).
import { main, verifyPayload } from "@paymenthooks/cli";
const code = await main(["trigger", "payment.succeeded"], { out: console.log, err: console.error, env: process.env });
const ok = verifyPayload(secret, req.headers["x-paymenthooks-signature"], rawBody);| Export | Purpose |
|---|---|
main(argv, io) | Run the CLI; resolves to the exit code. |
runListen(config, options) | ph listen without the argument parser. |
forwardEvent(url, secret, envelope) | Sign and POST one envelope. |
trigger, resend, rotateSecret | The other commands. |
resolveConfig(apiKey, apiBase) | Validate a key and API base. |
signPayload(secret, body, t?) / verifyPayload(secret, header, body, now?) | Signature helpers; verify accepts v1 and v1_previous within 300 seconds. |
SIGNATURE_HEADER | "X-PaymentHooks-Signature" |
CliError | Error class with code and status. |
parseArgs, parseSseChunk, normaliseForwardUrl | Parsing helpers. |
DEFAULT_API_BASE, VERSION, USAGE | Constants. |
Types: ApiConfig, ListenOptions, ForwardResult.
Stream API
The CLI uses these endpoints; you can call them directly with a secret key.
| Endpoint | Scope | Behaviour |
|---|---|---|
GET /api/v1/webhooks/stream | webhooks:read | Server-sent events. First `ready` { merchantId, livemode, signingSecret }, then one `webhook` { type, envelope } per dispatched event. |
POST /api/v1/webhooks/stream/trigger | webhooks:write | Body { eventType }. 201 { eventId, type }. Stream only, livemode false. 20 per minute. |
POST /api/v1/webhooks/stream/secret/rotate | webhooks:write | 200 { signingSecret }. New streams use the new secret. |
POST /api/v1/webhooks/logs/:id/replay | webhooks:write | Redeliver a logged delivery with its original event id. |