FG
๐Ÿ”Œ APIs & SDKsStripe

Webhook signature validation not working

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score65%
65%

Problem

Hi, I'm using the latest version of the stripe node.js library v4.19.0 and am trying to verify the signature in some test webhooks. It is failing with: error: Error SyntaxError: Unexpected token o in JSON at position 1 at Object.parse (native) at Object.Webhook.constructEvent (C:\Users\shovl\BitBucket\apm-billing\node_modules\stripe\lib\Webhooks.js:10:28) I followed the example here: https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/express.js Does the node.js library officially support webhook signatures yet? I noticed that the documentation does not list Node.js when referring to the "official" libraries. Thanks very much, Raymond

Error Output

error: Error SyntaxError: Unexpected token o in JSON at position 1

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Webhook Signature Validation Error in Stripe Node.js Library

Medium Risk

The error 'Unexpected token o in JSON at position 1' typically occurs when attempting to parse a non-JSON string as JSON. In this case, the issue arises from either an incorrect payload being sent to the webhook or the payload being parsed incorrectly before signature validation. Ensure that the payload is a raw string and not an already parsed object.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Ensure Raw Body Middleware is Configured

    Make sure that the middleware used to handle incoming requests does not parse the body before it reaches the webhook handler. Use the 'body-parser' middleware with the 'raw' option for the specific route handling the webhook.

    javascript
    app.use('/webhook', bodyParser.raw({ type: 'application/json' }));
  2. 2

    Verify Webhook Signature Validation Logic

    Check that you are correctly using the 'constructEvent' method from the Stripe library. Pass the raw body and the signature header to this method to validate the webhook.

    javascript
    const event = stripe.webhooks.constructEvent(req.body, req.headers['stripe-signature'], endpointSecret);
  3. 3

    Check for Proper Error Handling

    Implement error handling around the webhook signature verification to catch and log any errors that occur during the process. This will help you understand if the issue persists.

    javascript
    try { const event = stripe.webhooks.constructEvent(req.body, req.headers['stripe-signature'], endpointSecret); } catch (err) { console.error('Webhook signature verification failed:', err.message); return res.status(400).send(`Webhook Error: ${err.message}`); }
  4. 4

    Test with Valid Payload

    Use the Stripe CLI or a similar tool to send test webhook events to your endpoint. Ensure that the payload is correctly formatted and that you are using the correct endpoint secret.

    bash
    stripe trigger payment_intent.succeeded

Validation

Confirm that the webhook signature validation no longer throws an error by checking the logs for successful processing of the webhook. You can also set up a test webhook in the Stripe dashboard and verify that it triggers the expected behavior in your application.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

stripepaymentsapi