Skip to main content

Overview

The Webhook class provides a secure, type-safe way to handle webhook events from Paystack. It automatically verifies webhook signatures, parses event payloads, and routes events to registered handlers.

Key Features

  • Automatic signature verification using HMAC SHA-512
  • Type-safe event handlers with full TypeScript support
  • Event-driven architecture with chainable .on() method
  • Platform-agnostic works with Express, Next.js, Hono, and more
  • Zod validation for runtime type safety

Methods

on

Registers a handler function for a specific webhook event. This method is chainable, allowing you to register multiple handlers at once.

Parameters

string
required
The event type to listen for. See Supported Events below
function
required
The function to execute when the event is received. The function receives the event data as its parameter and can be async

Returns

Returns the Webhook instance for chaining.

process

Processes an incoming webhook request by verifying the signature, parsing the payload, and dispatching it to the appropriate handler.

Parameters

string
required
The raw, unparsed request body as a string. Important: Do not parse the JSON before passing it to this method, as the signature verification requires the raw body
string
required
The value of the x-paystack-signature header from the webhook request

Returns

Returns a Promise that resolves to the parsed webhook payload if successful.

Throws

  • Error("Missing 'x-paystack-signature' header") - If the signature header is missing
  • Error("Invalid webhook signature") - If the signature verification fails
  • Error("Failed to parse webhook payload") - If the payload doesn’t match the expected schema

Supported Events

The SDK supports all Paystack webhook events with full TypeScript types:

Charge Events

Triggered when a charge is successful.Handler data includes:
  • reference - Transaction reference
  • amount - Amount in kobo
  • customer - Customer details (email, name, etc.)
  • authorization - Card/payment authorization details
  • metadata - Custom metadata
Triggered when a dispute is created on a charge.Handler data includes:
  • id - Dispute ID
  • transaction - Full transaction details
  • customer - Customer details
  • status - Dispute status
Triggered to remind you of a pending dispute.
Triggered when a dispute is resolved.

Transfer Events

Triggered when a transfer is successful.Handler data includes:
  • reference - Transfer reference
  • amount - Transfer amount
  • recipient - Recipient details
  • status - Transfer status
Triggered when a transfer fails.
Triggered when a transfer is reversed.

Subscription Events

Triggered when a subscription is created.Handler data includes:
  • subscription_code - Unique subscription code
  • plan - Plan details
  • customer - Customer details
  • authorization - Payment authorization
Triggered when a subscription is disabled.
Triggered when a subscription will not renew.
Triggered to alert about expiring cards on subscriptions. Returns an array of subscriptions with expiring cards.

Invoice Events

Triggered when an invoice is created.
Triggered when an invoice is updated.
Triggered when an invoice payment fails.

Refund Events

Triggered when a refund is pending.
Triggered when a refund is being processed.
Triggered when a refund has been processed.
Triggered when a refund fails.

Other Events

Triggered when a dedicated virtual account is successfully assigned to a customer.
Triggered when dedicated virtual account assignment fails.
Triggered when customer identification is successful.
Triggered when customer identification fails.
Triggered when a payment request is pending.
Triggered when a payment request is successful.

Security

Signature Verification

The SDK automatically verifies webhook signatures using HMAC SHA-512. This ensures that webhooks are genuinely from Paystack and haven’t been tampered with.

Best Practices

The signature is computed against the raw request body. Parse the body to JSON only after verification fails.
Never hardcode your Paystack secret key.
Paystack may send the same webhook multiple times. Use the event’s reference/ID to prevent duplicate processing.
Process webhooks quickly or queue them for background processing. Paystack expects a 200 response within a reasonable time.

Error Handling

Handle webhook processing errors gracefully:

Testing Webhooks

Using Paystack Test Mode

  1. Use your test secret key to initialize the SDK
  2. Make a test transaction on your staging/test environment
  3. Paystack will send webhooks to your configured endpoint

Local Testing with Ngrok

Manual Testing

You can manually trigger webhooks using the Paystack API or dashboard for testing.

Paystack Webhook Docs

Official Paystack webhook documentation

Webhook Events

Complete list of webhook events and their payloads

Security

Learn more about webhook security

GitHub Examples

See complete webhook integration examples