<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shubham Sharma</title>
    <description>The latest articles on DEV Community by Shubham Sharma (@shubbu03).</description>
    <link>https://dev.to/shubbu03</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4280906%2F06ebd00c-1e83-4b24-a382-ad92b4394c05.jpg</url>
      <title>DEV Community: Shubham Sharma</title>
      <link>https://dev.to/shubbu03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubbu03"/>
    <language>en</language>
    <item>
      <title>Why Stripe webhook signature verification fails (and how to find which input is wrong)</title>
      <dc:creator>Shubham Sharma</dc:creator>
      <pubDate>Fri, 04 Dec 2026 07:12:06 +0000</pubDate>
      <link>https://dev.to/shubbu03/why-stripe-webhook-signature-verification-fails-and-how-to-find-which-input-is-wrong-1dag</link>
      <guid>https://dev.to/shubbu03/why-stripe-webhook-signature-verification-fails-and-how-to-find-which-input-is-wrong-1dag</guid>
      <description>&lt;p&gt;Stripe's libraries throw the same error for every signature mismatch:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;No signatures found matching the expected signature for payload&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That tells you the check failed, not why. The check has exactly three inputs: the &lt;strong&gt;raw request body&lt;/strong&gt;, the &lt;strong&gt;&lt;code&gt;Stripe-Signature&lt;/code&gt; header&lt;/strong&gt;, and your endpoint's &lt;strong&gt;signing secret&lt;/strong&gt;. One of them is wrong. Here are the causes in the order I'd check them.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Stripe signs a webhook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Stripe-Signature&lt;/code&gt; header has comma-separated parts:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;t=1767225600,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;t&lt;/code&gt;: when this attempt was signed, in Unix seconds. Every retry is signed again with a new one.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;v1&lt;/code&gt;: the HMAC-SHA256 signature, hex-encoded. There's one per active secret, so you'll see two right after you roll a secret.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;v0&lt;/code&gt;: a placeholder on test events. Ignore it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To verify:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Split the header on commas, and each part on its first &lt;code&gt;=&lt;/code&gt;. Keep &lt;code&gt;t&lt;/code&gt; and every &lt;code&gt;v1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Build the message &lt;code&gt;t + "." + rawBody&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Compute HMAC-SHA256 of that message, keyed with the &lt;strong&gt;whole&lt;/strong&gt; &lt;code&gt;whsec_…&lt;/code&gt; secret as text, and hex-encode it.&lt;/li&gt;
&lt;li&gt;Compare it in constant time with each &lt;code&gt;v1&lt;/code&gt;. Any match is valid.&lt;/li&gt;
&lt;li&gt;Reject timestamps more than five minutes from your clock.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Node that's:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createHmac, timingSafeEqual } from "node:crypto";

// rawBody must be the exact bytes received (a Buffer), not re-serialized JSON.
export function verifyStripeWebhook(rawBody, headers, secret) {
  let timestamp;
  const signatures = [];
  for (const item of (headers["stripe-signature"] ?? "").split(",")) {
    const separator = item.indexOf("=");
    const key = item.slice(0, separator);
    const value = item.slice(separator + 1);
    if (key === "t") timestamp = value;
    else if (key === "v1") signatures.push(Buffer.from(value, "hex"));
  }
  if (!timestamp || !/^\d+$/.test(timestamp) || signatures.length === 0) return false;
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) &amp;gt; 300) return false;

  // The whole whsec_ string is the key: don't strip it or Base64-decode it.
  const expected = createHmac("sha256", secret)
    .update(timestamp + ".")
    .update(rawBody)
    .digest();

  return signatures.some(
    (signature) =&amp;gt; signature.length === expected.length &amp;amp;&amp;amp; timingSafeEqual(signature, expected),
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You don't need to write this yourself: &lt;code&gt;stripe.webhooks.constructEvent&lt;/code&gt; (Node), &lt;code&gt;stripe.Webhook.construct_event&lt;/code&gt; (Python) and &lt;code&gt;webhook.ConstructEvent&lt;/code&gt; (Go) do the same thing. Knowing the steps is what lets you debug them.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The body was parsed before it was verified
&lt;/h2&gt;

&lt;p&gt;This is the most common cause. The signature covers the exact bytes Stripe sent. If a JSON parser runs first, you get an object back, and serializing it again changes the bytes: whitespace, key order, and how Unicode is escaped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express:&lt;/strong&gt; register the webhook route with &lt;code&gt;express.raw()&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; the global JSON parser.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/webhooks/stripe", express.raw({ type: "application/json" }), (req, res) =&amp;gt; {
  const event = stripe.webhooks.constructEvent(req.body, req.headers["stripe-signature"], secret);
  // ...
});

app.use(express.json()); // after the webhook route
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Next.js App Router:&lt;/strong&gt; read the body as text, not JSON.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function POST(request: Request) {
  const body = await request.text();
  const event = stripe.webhooks.constructEvent(body, request.headers.get("stripe-signature")!, secret);
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Next.js Pages Router:&lt;/strong&gt; turn off &lt;code&gt;bodyParser&lt;/code&gt; for the route and read the raw buffer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flask:&lt;/strong&gt; pass &lt;code&gt;request.get_data()&lt;/code&gt;, not &lt;code&gt;request.json&lt;/code&gt;. &lt;strong&gt;Django:&lt;/strong&gt; use &lt;code&gt;request.body&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go:&lt;/strong&gt; &lt;code&gt;io.ReadAll&lt;/code&gt; the body (behind &lt;code&gt;http.MaxBytesReader&lt;/code&gt;) and verify before &lt;code&gt;json.Unmarshal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS API Gateway:&lt;/strong&gt; make sure the raw body reaches your Lambda function unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The secret is from a different endpoint
&lt;/h2&gt;

&lt;p&gt;Every Stripe endpoint has its own &lt;code&gt;whsec_&lt;/code&gt; secret. Test-mode and live-mode endpoints never share one. And &lt;code&gt;stripe listen&lt;/code&gt; prints a secret of its own, separate from any endpoint in the Dashboard.&lt;/p&gt;

&lt;p&gt;So a secret that works locally fails in production, and the other way around. A quick check: look at &lt;code&gt;"livemode"&lt;/code&gt; in the event body. &lt;code&gt;false&lt;/code&gt; means a test-mode endpoint signed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The secret was changed on its way to the HMAC
&lt;/h2&gt;

&lt;p&gt;Stripe uses the whole &lt;code&gt;whsec_…&lt;/code&gt; string, prefix included, as the key. Any of these change it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stripping &lt;code&gt;whsec_&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Base64-decoding the rest. That's how &lt;a href="https://www.standardwebhooks.com/" rel="noopener noreferrer"&gt;Standard Webhooks&lt;/a&gt; secrets work, so it's easy to carry over from another provider, but Stripe doesn't do it.&lt;/li&gt;
&lt;li&gt;Loading it from an &lt;code&gt;.env&lt;/code&gt; file with quotes or a trailing newline&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Only part of the header was checked
&lt;/h2&gt;

&lt;p&gt;The header can hold more than one &lt;code&gt;v1&lt;/code&gt;: after you roll a secret, Stripe signs with both the old and the new one while the old one is still active. Code that takes only the first &lt;code&gt;v1&lt;/code&gt;, or compares against the whole header string, breaks exactly when you rotate.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The timestamp is outside the tolerance
&lt;/h2&gt;

&lt;p&gt;The libraries reject anything signed more than five minutes ago. Every retry gets a fresh timestamp, so this usually means you replayed a request by hand (for example, from a saved curl command) or your server's clock has drifted. Keep it synced with NTP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding which input is wrong
&lt;/h2&gt;

&lt;p&gt;When you can't tell which of the three it is, compare against a known-good computation. I built a &lt;a href="https://cascadehook.com/tools/stripe-webhook-signature-verifier" rel="noopener noreferrer"&gt;free Stripe signature checker&lt;/a&gt; for this: paste the header, the secret and the raw body, and it says which input doesn't match. It also catches the specific mistakes above, like a stripped &lt;code&gt;whsec_&lt;/code&gt; prefix or a re-encoded body. It runs in your browser, so nothing you paste is sent anywhere.&lt;/p&gt;

&lt;p&gt;To capture a real delivery's exact bytes in the first place, point the endpoint at a &lt;a href="https://cascadehook.com/tools/webhook-tester" rel="noopener noreferrer"&gt;webhook tester&lt;/a&gt;. It gives you a temporary URL, shows each request's headers and raw body, and checks the Stripe signature as requests arrive.&lt;/p&gt;

&lt;p&gt;If you also need to &lt;em&gt;send&lt;/em&gt; webhooks from your own product, the &lt;a href="https://cascadehook.com/tools/webhook-signature-comparison" rel="noopener noreferrer"&gt;signature scheme comparison&lt;/a&gt; shows how Stripe, GitHub, Shopify, Slack and Standard Webhooks each sign theirs.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://cascadehook.com" rel="noopener noreferrer"&gt;Cascadehook&lt;/a&gt;, a webhook delivery service. The tools linked above are free and don't need an account.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>stripe</category>
      <category>webhooks</category>
      <category>node</category>
      <category>security</category>
    </item>
  </channel>
</rss>
