npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastify-webhook-verify

v1.0.0

Published

Multi-provider webhook signature verification for Fastify with replay protection and TypeScript support

Downloads

2

Readme

fastify-webhook-verify

Multi-provider webhook signature verification for Fastify with replay protection and TypeScript support.

npm version CI License: MIT

Features

  • Multi-provider support: Stripe, GitHub, Twilio, Slack, Shopify out of the box
  • Custom providers: Easy to add your own webhook providers
  • Replay protection: Built-in protection against replay attacks with configurable tolerance
  • TypeScript-first: Full type safety with TypeScript declarations
  • Fastify-native: Uses Fastify's preHandler pattern for clean route integration
  • Automatic raw body handling: Preserves raw body for signature verification

Installation

npm install fastify-webhook-verify

Quick Start

import Fastify from 'fastify';
import webhookVerify from 'fastify-webhook-verify';

const fastify = Fastify({ logger: true });

await fastify.register(webhookVerify, {
  providers: {
    stripe: process.env.STRIPE_WEBHOOK_SECRET,
    github: process.env.GITHUB_WEBHOOK_SECRET,
  },
});

// Stripe webhook endpoint
fastify.post('/webhooks/stripe', {
  preHandler: fastify.webhookVerify({ provider: 'stripe' }),
}, async (request) => {
  const { eventType, timestamp } = request.webhook!;
  console.log(`Received Stripe event: ${eventType}`);

  // Handle the webhook event
  return { received: true };
});

// GitHub webhook endpoint
fastify.post('/webhooks/github', {
  preHandler: fastify.webhookVerify({ provider: 'github' }),
}, async (request) => {
  console.log('Received GitHub webhook');
  return { received: true };
});

await fastify.listen({ port: 3000 });

Supported Providers

| Provider | Algorithm | Signature Header | Timestamp | |----------|-----------|------------------|-----------| | Stripe | HMAC-SHA256 | Stripe-Signature | In header | | GitHub | HMAC-SHA256 | X-Hub-Signature-256 | - | | Twilio | HMAC-SHA1 | X-Twilio-Signature | - | | Slack | HMAC-SHA256 | X-Slack-Signature | X-Slack-Request-Timestamp | | Shopify | HMAC-SHA256 | X-Shopify-Hmac-SHA256 | - |

Configuration

Plugin Options

interface FastifyWebhookVerifyOptions {
  // Provider secrets
  providers?: {
    stripe?: string;
    github?: string;
    twilio?: string;
    slack?: string;
    shopify?: string;
  };

  // Replay protection settings (default: enabled with 5 min tolerance)
  replayProtection?: {
    enabled: boolean;
    tolerance?: number; // seconds, default: 300
    storage?: ReplayStorage; // custom storage (e.g., Redis)
  };

  // Custom error handler
  errorHandler?: (error: Error, request: FastifyRequest, reply: FastifyReply) => void;

  // Hook called after successful verification
  onVerify?: (result: WebhookVerificationResult, request: FastifyRequest) => void;

  // Enable logging of verification attempts
  logAttempts?: boolean;
}

Route Options

interface WebhookRouteOptions {
  provider: 'stripe' | 'github' | 'twilio' | 'slack' | 'shopify' | 'custom';
  secret?: string; // Override global provider secret
  customConfig?: CustomProviderConfig; // For custom providers
  replayProtection?: Partial<ReplayProtectionConfig>; // Override per-route
}

Examples

Multiple Environments

// Different secrets for live vs test
fastify.post('/webhooks/stripe/live', {
  preHandler: fastify.webhookVerify({
    provider: 'stripe',
    secret: process.env.STRIPE_LIVE_SECRET!,
  }),
}, handler);

fastify.post('/webhooks/stripe/test', {
  preHandler: fastify.webhookVerify({
    provider: 'stripe',
    secret: process.env.STRIPE_TEST_SECRET!,
  }),
}, handler);

Custom Provider

fastify.post('/webhooks/internal', {
  preHandler: fastify.webhookVerify({
    provider: 'custom',
    secret: process.env.INTERNAL_SECRET!,
    customConfig: {
      name: 'internal-service',
      signatureHeader: 'X-Internal-Signature',
      timestampHeader: 'X-Internal-Timestamp',
      algorithm: 'sha256',
      signatureEncoding: 'hex',
      buildPayload: (body, ts) => `${ts}.${body.toString()}`,
    },
  }),
}, handler);

Custom Error Handler

await fastify.register(webhookVerify, {
  providers: { stripe: process.env.STRIPE_WEBHOOK_SECRET },
  errorHandler: (error, request, reply) => {
    request.log.error({ err: error }, 'Webhook verification failed');

    // RFC 9457 Problem Details response
    reply.status(error.statusCode).send({
      type: `https://api.example.com/errors/${error.code.toLowerCase()}`,
      title: error.message,
      status: error.statusCode,
    });
  },
});

Redis Storage for Replay Protection

import { createClient } from 'redis';

const redis = createClient({ url: process.env.REDIS_URL });
await redis.connect();

const redisStorage = {
  async has(nonce: string): Promise<boolean> {
    const exists = await redis.exists(`webhook:nonce:${nonce}`);
    return exists === 1;
  },
  async set(nonce: string, expiresAt: number): Promise<void> {
    const ttl = Math.ceil((expiresAt - Date.now()) / 1000);
    await redis.setEx(`webhook:nonce:${nonce}`, ttl, '1');
  },
};

await fastify.register(webhookVerify, {
  providers: { stripe: process.env.STRIPE_WEBHOOK_SECRET },
  replayProtection: {
    enabled: true,
    tolerance: 300,
    storage: redisStorage,
  },
});

Disable Replay Protection Per-Route

fastify.post('/webhooks/stripe/idempotent', {
  preHandler: fastify.webhookVerify({
    provider: 'stripe',
    replayProtection: { enabled: false },
  }),
}, handler);

Audit Logging Hook

await fastify.register(webhookVerify, {
  providers: { stripe: process.env.STRIPE_WEBHOOK_SECRET },
  onVerify: async (result, request) => {
    await auditLog.record({
      timestamp: new Date(),
      provider: result.provider,
      eventType: result.eventType,
      success: result.valid,
      ip: request.ip,
    });
  },
});

Accessing Webhook Data

After verification, webhook data is available on request.webhook:

interface WebhookData {
  verified: boolean;
  provider: string;
  timestamp?: Date;
  rawBody: Buffer;
  eventType?: string;
}

Error Types

The plugin exports typed error classes:

import {
  WebhookError,
  MissingSignatureError,
  InvalidSignatureError,
  TimestampExpiredError,
  ReplayAttackError,
  MissingRawBodyError,
  UnknownProviderError,
  MissingSecretError,
} from 'fastify-webhook-verify';

Requirements

  • Node.js >= 20.0.0
  • Fastify >= 5.0.0

License

MIT