Yohav Rn
Yohav Rn

Reputation: 209

Webhook Calendly with Node.js

I'm trying to use Calendly webhooks, and despise the doc, I believe that I did not get the concept of how it is supposed to work.

So here is all the steps that I did after generating a personal token on Calendly.

  1. Using Postman,GET, https://api.calendly.com/users/me with my token to get the necessary infos:
{
    "resource": {
        "avatar_url": null,
        "created_at": "2024-04-21T10:27:11.053887Z",
        "current_organization": "https://api.calendly.com/organizations/7ebe4c78-5363-5363-b40a-7ebe4c78ab",
        "email": "[email protected]",
        "name": "Info MyApp",
        "resource_type": "User",
        "scheduling_url": "https://calendly.com/myapp-info",
        "slug": "myapp-info",
        "timezone": "Asia/Jerusalem",
        "updated_at": "2024-08-31T19:30:12.282586Z",
        "uri": "https://api.calendly.com/users/56e798cfe00c-4773-4747-815d-56e798cfe00c"
    }
}
  1. After, using Postman again,POST, https://api.calendly.com/webhook_subscriptions with this in the body:
{
 "url": "https://staging.api.my-app.io/calendly-webhook",
 "events": [
   "invitee.created",
   "invitee.canceled",
   "invitee_no_show.created",
   "invitee_no_show.deleted"
 ],
 "organization": "https://api.calendly.com/organizations/7ebe4c78-5363-5363-b40a-7ebe4c78ab",
 "scope": "organization",
 "signing_key": "5mEzn9C-I28UtwOjZJtFoob0sAAFZ95GbZkqj4y3i0I" <= this is a random signing_key made up by me not something that was given to me
}
  1. I receive all the infos, confirmation that webhook is created and now in my Node.js app I create this:
const verifyWebhookSignature = (req) => {
  const signature = req.headers['calendly-webhook-signature'];
  const webhookSecret = process.env.CALENDLY_SIGNING_KEY;

  const payload = JSON.stringify(req.body);
  const hmac = crypto.createHmac('sha256', webhookSecret);
  const digest = hmac.update(payload).digest('hex');

  return signature === digest;
};

// CALENDLY WEBHOOK
AdminRouter.post('/calendly-webhook', async (req, res) => {
  try {
    // Verify the webhook signature
    console.log('Webhook received:');
    if (!verifyWebhookSignature(req)) {
      return res.status(401).send('Invalid signature');
    }

    const event = req.body;

    switch (event.event) {
      case 'invitee.created':
        await handleInviteeCreated(event.payload);
        break;
      case 'invitee.canceled':
        await handleInviteeCanceled(event.payload);
        break;
      case 'invitee_no_show.created':
        await handleInviteeNoShow(event.payload);
        break;
      case 'invitee_no_show.deleted':
        await handleInviteeNoShowDeleted(event.payload);
        break;
      default:
        console.log(`Unhandled event type: ${event.event}`);
    }

    res.status(200).send('Webhook received successfully');
  } catch (error) {
    console.error('Error processing Calendly webhook:', error);
    res.status(500).send('Error processing webhook');
  }
});

I pass the details about the functions for each case..

  1. I create a Calendly with my URL (https://calendly.com/myapp-info) and nothing happens, I also tried on postman, POST, https://staging.api.my-app.io/calendly-webhook with this infos:
Header:
Calendly-Webhook-Signature: t=1693597064,v1=5mEzn9C-I28UtwOjZJtFoob0sAAFZ95GbZkqj4y3i0I

Body:
{
  "event": "invitee.created",
    "payload": {
      "event": {
      "start_time": "2024-09-01T10:00:00Z",
      "end_time": "2024-09-01T10:30:00Z"
      },
    "invitee": {
      "email": "[email protected]",
      "name": "Test User"
    }
  }
}

The only answer I get is Invalid signature. I don't know where the problem is, any help is welcome. Thank you.

Upvotes: 0

Views: 111

Answers (0)

Related Questions