Webhook Call back not received in the application

Hi I am trying to get the returned object from the webhook to get verified and listen to the events.
I am getting pending status and I see on ngrok the response with the challenge but I never get it back into my server.

Not sure what I am not seeing here!

// to handle response verification.    
app.use(express.json({
      verify: verifyTwitchSignature
    }));


app.post('/createWebhook/:broadcasterId', (req, res) => {
  const createWebHookParams = {
    host: "api.twitch.tv",
    path: "helix/eventsub/subscriptions",
    method: 'POST',
    headers: {
      "Content-Type": "application/json",
      "Client-ID": clientId,
      "Authorization": "Bearer " + authToken
    }
  }

  const createWebHookBody = {
    "type": "channel.follow",
    "version": "1",
    "condition": {
      "broadcaster_user_id": req.params.broadcasterId
    },
    "transport": {
      "method": "webhook",
      "callback": "https:ngrokURL/notification",
      "secret": webhookSecret // 
    }
  }

  let responseData = ""
  const webhookReq = https.request(createWebHookParams, (result) => {
    result.setEncoding('utf8')
    result.on('data', (d) => {
        responseData = responseData + d
      })
      .on('end', (result) => {
        const responseBody = JSON.parse(responseData) 
        console.log(responseBody)
        res.send(responseBody)
      })
  })
  webhookReq.on('error', (e) => {
    console.log("Error")
  })

  webhookReq.write(JSON.stringify(createWebHookBody))
  webhookReq.end()

});



app.post('/notification', (req, res) => {
  console.log("I raaaaaannnnnnn" ,req)
  const messageType = req.header("Twitch-Eventsub-Message-Type");
  if (messageType === 'webhook_callback_verification') {
    console.log("Verifying Webhook..");
    return res.status(200).send(req.body.challenge);
  }
  if (messageType === "revocation") {
    const {
      subscription
    } = req.body;
    console.log(`${subscription.type} subscription ${subscription.id} has been revoked`, subscription)
    return res.status(200).end();
  };

  console.log("I am the body requestttt", req.body)
  // const {
  //   type
  // } = req.body.subscription;
  const {
    event
  } = req.body;

  if (messageType === "notification") {
    try {
      console.log(event);
    } catch (error) {
      console.log(`An error accured sending the online notificatin for ${event.broadcaster_user_name}: `, error)

    }
  }
  res.status(200).end();
})

app.listen(PORT, () => {
  console.log(`listening at http://localhost:${PORT}`)
})

If you can see it in ngrok but not in your server.

That would suggest that ngrok ↔ server is not working as expected

Your code looks like when you get a POST to your sever, then you are creating a subscription.

Which is wrong in the work flow here.

This example might help

https://github.com/BarryCarlyon/twitch_misc/blob/main/eventsub/handlers/nodejs/receive.js

Using express, this example only handles POST/webhooks in.

You use another system to create a subscription request, and this example replies to that request and handles incoming data

Thank you Barry!
I see in ngrok correctly, but not in the server.
I will make sure that I have ngrok setup correctly and then work-through your POST code

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.