stripe webhook raw body

    1

    Correctly preserve and verify Stripe webhook signatures in Express and TypeScript applications.

    $20

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    stripe webhook raw body

    Example session with this skill installed

    I'm getting signature verification errors in my Express app when Stripe sends webhooks. How do I fix the raw body issue?

    • Read your context and instructions
    • Compiled the stripe webhook raw

    I have implemented the raw body preservation pattern. The verification was failing because express.json() parses the body into an object, discarding the exact byte sequence Stripe uses for the HMAC signature. I have updated your server configuration and the webhook route to use a verification callback and a hard guard.

    Stripe Webhook Implementation

    // 1. Update app.ts / server setup
    import express, { Request, Response, NextFunction } from 'express';
    
    app.use(
      express.json({
        verify: (req: any, _res: Response, buf: Buffer) => {
          // Store the raw bytes on the request object
          req.rawBody = buf;
        },
      }),
    );
    
    // 2. Update types/express.d.ts
    declare global {
      namespace Express {
        interface Request {
          rawBody?: Buffer;
        }
      }
    }
    
    // 3. Update routes/webhooks.ts
    router.post("/webhook", async (req: Request, res: Response) => {
      const sig = req.headers["stripe-signature"];
      const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
    
      if (!sig || !webhookSecret) {
        return res.status(400).send("Missing signature or secret");
      }
    
      const rawBody = req.rawBody;
      if (!rawBody) {
        // Fail hard: do not attempt JSON.stringify(req.body)
        return res.status(400).send("Raw body not preserved");
      }
    
      try {
        const event = stripe.webhooks.constructEvent(rawBody, sig, webhookSecret);
        // Process verified event
        res.json({ received: true });
      } catch (err) {
        const message = err instanceof Error ? err.message : "Unknown error";
        res.status(400).send(`Webhook Error: ${message}`);
      }
    });
    

    Next steps

    • Ensure the webhook route is defined after the express.json middleware update.
    • Verify your STRIPE_WEBHOOK_SECRET environment variable matches the one in the Stripe Dashboard.
    • Use the Stripe CLI to trigger a test event and confirm the rawBody is correctly captured.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Webhook signature verification fails when Express middleware parses request bodies, as the original byte sequence is lost. This skill provides the precise implementation pattern to preserve raw request bytes and verify Stripe events. It eliminates the common mistake of re-serializing JSON, which causes cryptographic mismatches due to key ordering and whitespace changes.

    What it does

    • Preserves raw bytes via the express.json verify callback to ensure signature accuracy.
    • Enforces hard guards in route handlers to prevent insecure fallbacks to parsed JSON.
    • Extends Express types to include rawBody buffers, maintaining TypeScript safety.
    • Configures development modes to handle local testing when secrets are unavailable.
    • Audits middleware order to ensure body-parsing doesn't corrupt incoming webhook data.

    How it works

    1. Middleware configuration: Modify the global or route-specific express.json parser to capture the buffer.
    2. Type definition: Extend the Express Request interface to recognize the rawBody property.
    3. Handler implementation: Add the constructEvent call with the captured buffer and signature header.
    4. Error handling: Implement guards to return 400 errors if the raw body is missing or the signature is invalid.

    Frameworks & tools

    Node.js, Express, Stripe SDK, and TypeScript.

    Why this beats prompting it yourself

    Standard LLM prompts often suggest JSON.stringify(req.body) as a fallback, which is a logic error that causes production failures. This skill provides the specific middleware hook pattern that is physically required for cryptographic verification to succeed.

    Use cases

    • Adding a new /webhook endpoint to an existing Express API.
    • Debugging "No relevant data found in the Stripe-Signature header" errors.
    • Migrating a Stripe integration from JavaScript to TypeScript with strict typing.
    • Setting up local development environments for testing Stripe events.

    Known limitations

    Requires the use of Express and the standard Stripe Node.js SDK. Signature verification will fail if other global middleware modifies the request stream before this pattern is applied.

    Requires Express framework. Verification fails if global middleware consumes the request stream before the verify callback runs.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Trust & safety

    Security scan

    Security scan passed 22 days ago

    Permissions

    Network Access
    Listed22 days ago

    What's inside

    Frequently Asked Questions

    This skill provides a battle-tested implementation pattern that captures the original request buffer before Express parses it, preventing the common 'invalid signature' errors caused by JSON re-serialization.

    The skill is designed for Node.js environments using Express and TypeScript, ensuring your webhook handlers are both cryptographically secure and type-safe.

    You will receive the precise middleware configuration, TypeScript interface extensions, and the secure route handler pattern required to verify Stripe signatures correctly.

    Yes, it includes a specific pattern for handling development modes so you can test your integration locally even when webhook secrets aren't available.

    The skill addresses a critical 'middleware order' issue, providing instructions on where to place the code relative to your other body-parsers to avoid data corruption.

    New in APIs & Backend