- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
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.
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.
No local environment setup is required, we’ll be using the Nylas and Vercel dashboards to set everything up.
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
:
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.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.
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.
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.
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.
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.
Ram loves teaching, building and exploring technologies. He is passionate about empowering developers to ship amazing products to market as fast as possible ????. Ram is excited to share knowledge and help others. He’s a Relaxed Tomato ????.