node.jsexpressaxioswebhookscalendly

I'm trying to set up a webhook to retrieve real-time data as soon as the user submits a Calendly appointment


I'm trying to set up a Calendly webhook to retrieve real-time data as soon as the user submits a Calendly appointment

This is my nodejs express server code

const port = process.env.PORT || 5000;
const express = require('express');
const axios = require('axios');


const app = express();

const API_KEY = 'MY_API_KEY';
const TOKEN = 'MY_TOKEN';

app.post('/calendly-webhook', (req, res) => {
  // Retrieve real-time data from the request payload
  const data = req.body;
  
  // Log the data to the console
  console.log('Calendly webhook data:', data);
  
  res.send('Webhook received');
});

app.listen(port, () => {
  console.log('Server listening on port 5000');
  
  // Create a new webhook
  axios({
    method: 'post',
    url: 'https://api.calendly.com/webhooks',
    headers: {
      'Authorization': `Bearer ${API_KEY}`,
      'X-Token-Auth': TOKEN
    },
    data: {
      url: 'http://localhost:5000/calendly-webhook',
      events: ['invitee.created']
    }
  })
  .then(response => {
    console.log('Webhook created:', response.data);
  })
  .catch(error => {
    console.error('Webhook creation failed:', error.response.data);
  });
});

I'm getting this error

] Webhook creation failed: {
[0]   title: 'Resource Not Found',
[0]   message: 'The requested path does not exist'

I'm really not sure what I'm doing wrong and i'm pretty much new to this


Solution

  • You're calling the API endpoint for creating a webhook subscription incorrectly, please refer to the documentation: Create Webhook Subscription. The request path has to be different, and you're missing some parameters.

    Example of a correct call:

    axios({
        method: 'post',
        url: 'https://api.calendly.com/webhook_subscriptions',
        headers: {
          'Authorization': `Bearer ${ACCESS_TOKEN}`
        },
        data: {
          url: '<public internet URL>',
          events: ['invitee.created'],
          organization: 'https://api.calendly.com/organizations/AAAAAAAAAAAAAAAA',
          user: 'https://api.calendly.com/users/BBBBBBBBBBBBBBBB',
          scope: 'user'
        }
      })
    

    Note 1: You can't specify a localhost callback URL. You must specify a public internet URL. Use a service like RequestBin to capture requests during development, or a tunneling service like ngrok if you want to send requests to your local machine.

    Note 2: in real code you'll probably want to dynamically fetch the organization and user that you're creating a webhook subscription for. You can call Get Current User, for example, to get that information.

    Note 3: you must use OAuth Access Tokens or Personal Access Tokens to call the current version of Calendly's API. Legacy API keys are deprecated.