Hero Banner on How to Create Nylas webhooks with NodeJS

How to create Nylas Webhooks with NodeJS

5 min read
Tags:

This blog post contains code for the Nylas API V2. Take a look at your latest docs to see Nylas API V3 Webhooks Docs updated to work with Nylas API V3.

Intro

In this post, we are going to create Nylas webhooks with NodeJS. This will be useful to streamline working with Nylas’ communication platform to be easily notified when specific events occur. You can find example code on the Nylas samples repository, and alternatively, check out our video:

In this post, we’ll create a serverless function to receive webhooks, deploy it to Vercel, and set up a webhook using the Nylas Dashboard.

Prerequisites

  • Sign up here if you need to create a Nylas account for free!
  • Follow the Quickstart guide to create an App (we’ll look at setting up webhooks in a bit):
Nylas quickstart
  • We’ll be using Vercel to deploy a serverless endpoint, so make sure you signup for a Vercel account!

Environment

No local environment setup is required, we’ll be using the Nylas and Vercel dashboards to set everything up.

Creating functions to receive Nylas webhooks

We are building a single endpoint, our callback_url, to deploy on Vercel. To create serverless endpoints, we’ll follow the the Next.JS API docs on creating API Routes. Let’s build out our endpoint to support a GET and POST method:

// {{callback_url}}/api/webhooks/nylas.js

export default function handler(request, response) {
  if (request.method === "GET" && request.query.challenge) {
    console.log(`Received challenge code! - ${request.query.challenge}`);
    console.log(`Now returning challenge code! - ${request.query.challenge}`);
    return response.send(request.query.challenge);
  }

 if (request.method === "POST") {
   console.log('==========Message updated start==========');
   request.body.deltas.map(deltas => console.log(JSON.stringify(deltas)));
   console.log('==========Message updated end==========\n');
   return response.status(200).end();
 }
}

Next.js treats any file under the api folder as an endpoint, so here we are creating an endpoint at {{callback_url}}/api/webhooks/nylas. We’ll get the callback_url when we deploy our function to Vercel in the next section. Let’s dive into the reasons why we are creating two endpoint methods: GET and POST:

  • The GET endpoint is required to enable a Nylas’ webhook. Anytime we create a new webhook, Nylas’ will first call that endpoint with a challenge parameter and expect the challenge parameter to be returned by our deployed endpoint. After Nylas receives the challenge parameter back, the webhook will be enabled. We have logged out the challenge parameter in the code above.
  • The POST endpoint is where all triggered events will be received. So in our case, we will create a message.updated webhook event, that will trigger anytime a message is updated (i.e. favourite or star an email). It is important that we always respond with a status 200 to ensure that Nylas does not retry the webhook event multiple times.

Take a look at our Nylas API to read more about message update events. Also, you can view all the webhooks available.

Now we’ve created a serverless function to enable webhooks and receive webhooks events from Nylas.

Deploying functions on Vercel

Let’s look at deploying the function on Vercel. If you want to follow along, you can fork our repository. Head to Vercel and deploy the serverless function:

You can find the deployed url by going to the overview tab of the deployed function, the callback_url will be the listed under domains:

We will be using the callback_url when creating a Nylas webhook, which will be https://node-webhooks-challenge-serverless-function-demo.vercel.app/api/webhooks/nylas.

Now we’ve deployed the serverless function to Vercel.

Creating Nylas webhooks

Let’s create a Nylas webhook via the Nylas dashboard:

Now we’ve successfully created a webhook on the Nylas dashboard:

Taking a look at the Vercel function logs, we’ll see that the challenge parameter was received and sent back to Nylas:

We need to ensure that we send back the challenge parameter for the webhook to be enabled by Nylas.

Now we’ve created a Nylas webhook that will be triggered when a message is updated.

Receiving Nylas webhooks

Let’s look at triggering the message.updated Nylas webhook by updating a message. I am going to star a message in my inbox:

Let’s take a look at the Vercel function logs:

Looking at the Vercel function logs, we see the webhook event sent by Nylas including relevant information such as event type, message.updated, and message relevant attributes such as id that we can use within our application.

Now we’ve triggered a Nylas webhook and received the event via a POST request on our serverless endpoint.

Build time!

In this post, we explored creating Nylas webhooks with NodeJS using Vercel. You can find example code on the Nylas Samples code repository. Continue building with Nylas and learn more by visiting the documentation.

Related resources

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.

How to integrate a React calendar component to streamline scheduling

Elevate your app’s scheduling with a React calendar component and seamlessly integrate with Nylas for efficient calendar management.