Skip to content

v6.10.0

Compare
Choose a tag to compare
@mrashed-dev mrashed-dev released this 04 Apr 19:15
· 41 commits to main since this release

This latest release of the Nylas Node SDK includes support for verifying webhook signatures.

Release Notes

Added

  • Add support for verifying webhook signatures (#442)

Usage

To verify a webhook signature

const Nylas = require('nylas');
const express = require('express');
const cors = require('cors');

const app = express();

// Enable CORS
app.use(cors());

// The port the express app will run on
const port = 9000;

// Nylas app credentials
const NYLAS_CLIENT_SECRET = process.env.NYLAS_CLIENT_SECRET;
if (!NYLAS_CLIENT_SECRET) {
  throw new Error('NYLAS_CLIENT_SECRET is required')
}

// Create a callback route for the Nylas Event Webhook
app.post('/', express.json(), async (req, res) => {
  // Verify the Nylas Event Webhook signature to ensure the request is coming from Nylas
  const signature =
    req.headers['x-nylas-signature'] || req.headers['X-Nylas-Signature'];
  if (
    !WebhookNotification.verifyWebhookSignature(
      NYLAS_CLIENT_SECRET,
      signature,
      JSON.stringify(req.body)
    )
  ) {
    res.status(403).send('Invalid signature');
  }

  const { body } = req;

  // Log the webhook event to the console
  console.log('Webhook event received: ', JSON.stringify(body, undefined, 2));

  // Send a 200 response to the Nylas Event Webhook
  res.status(200).send({ success: true });
});