How to activate a webhook. How to handle a challenge string.

Max Winslow
Max Winslow
  • Updated

Task

Activate a webhook by parsing the challenge parameter and returning it in the response body.

 

https://developer.nylas.com/docs/developer-tools/webhooks/#challenge-parameter 

 

Requirements

A working server that accepts GET and POST requests.

 

Instructions

Node.JS example using Express:

// Require express and body-parser
const express = require("express");
const bodyParser = require("body-parser");

// Initialize express and define a port
const app = express();
const PORT = 3000;

// Tell express to use body-parser's JSON parsing
app.use(bodyParser.json());

app.all("/", (request, response) => {
// console.log(request);
if (request.method === "GET" && request.query.challenge) {
console.log(`Received challenge code! - ${request.query}`);
response.send(request.query.challenge);
console.log(`Sending challenge code! - ${request.query.challenge}`);
}

if (request.method === "POST") {
console.log('==========BODY DELTAS START==========');
console.log(request.body);
if (request.body.deltas[0].metadata) {
for (key in request.body.deltas[0].metadata) {
console.log(key + ": " + request.body.deltas[0].metadata[key])
}
}
console.log('==========BODY DELTAS START==========\n');
}

response.status(200).end() // Responding is important
})


// Start express on the defined port
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));

Outcome

Webhook is in an 'active' state and can begin receiving trigger events.

 

Resources

Webhooks Docs

 

 

 

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.