hero banner to send notification for calendar events

Send notifications for calendar events using the Nylas Node SDK

7 min read

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

Intro

In this post, we will take a look at how to create a notification for calendar events using the Nylas Node SDK. Building event notifications will let you engage users outside of a calendar event, whether before or after an event. For example, let’s imagine you want feedback from your users or find ways to engage with them outside of the event. In this blog post, we will explore how to send different notifications. Alternatively, check out our video on creating calendar event notifications.

Prerequisite

Sign up here if you need to create a Nylas account for free! Follow Quickstart to link an account to your account credentials. Ensure to save the access token to use below.

Environment

If you need to configure your environment to use Node, take a look at the Environment setup section from our code samples repository. Alternatively, you can check out this post on How to Send Emails with Nylas Node SDK to configure your environment to use Node.

Creating a calendar event to send notifications

Let’s start by creating a calendar event so we can add notifications. Before we create an event, we need a specific Calendar (i.e. CALENDAR_ID). Let’s use the Nylas Command Line Interface (CLI) to access available calendars. Check out Working with the Nylas CLI to learn more.

Let’s grab the first calendar via the Nylas CLI:

$ nylas api calendars get --limit=1

[
    {
        "account_id": "<ACCOUNT_ID>",
        "description": "Emailed events",
        "id": "<CALENDAR_ID>",
        "is_primary": null,
        "location": null,
        "name": "Emailed events",
        "object": "calendar",
        "read_only": true,
        "timezone": null
    }
]

Next, let’s create a calendar event in Node:

// node-send-calendar-event-notifications/createCalendarEvent.js

// Import your dependencies
import 'dotenv/config';
import Nylas from "nylas"
import Event from "nylas/lib/models/event";

// Configure your Nylas client
Nylas.config({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
});

const nylas = Nylas.with(process.env.ACCESS_TOKEN);

// Provide a starts and end time for the event as unix timestamps
const THIRTY_MINUTES = 30*60;

const START_TIME = Date.now()/1000 + THIRTY_MINUTES;
const END_TIME = START_TIME + THIRTY_MINUTES;
// Provide the calendar id you want to add an event to
const CALENDAR_ID = '<CALENDAR_ID>';
// Provide participant details
const PARTICIPANT_EMAIL = '<PARTICIPANT_EMAIL>'
const PARTICIPANT_NAME = '<PARTICIPANT_NAME>'

// Create a calendar event
try {
  const event = new Event(nylas, {
    calendarId: CALENDAR_ID,
    title: 'Build Apps with Nylas Node SDK',
    when: {
        startTime: START_TIME,
        endTime: END_TIME,
    },
    participants: [
      {
        name: PARTICIPANT_NAME,
        email: PARTICIPANT_EMAIL,
      }
    ],
    notifications: [],
  });
  
  const { id, calendarId, title, notifications } = await event.save();
  console.log({ id, calendarId, title, notifications });
} catch (error) {
  console.error("Error:\n", error);
}

Ensure you update the variables:

  • CLIENT_ID
  • CLIENT_SECRET
  • ACCESS_TOKEN
  • CALENDAR_ID
  • PARTICIPANT_EMAIL
  • PARTICIPANT_NAME

The Nylas Node SDK wraps the Nylas API to create a calendar event. Let’s try calling createCalendarEvent via the terminal:

$ node createCalendarEvent.js
{
  id: '<EVENT_ID>',
  calendarId: '<CALENDAR_ID>',
  title: 'Build Apps with Nylas Node SDK',
  notifications: []
}

Take note of the EVENT_ID, which we will be using in the upcoming sections to create notifications.

Now we’ve created a calendar event using the Nylas Node SDK.

Sending email notifications for calendar events

Let’s create an email notification that is send out 60 minutes before the event starts:

// node-send-calendar-event-notifications/addEmailNotification.js

// Import your dependencies
import 'dotenv/config';
import Nylas from "nylas";

// Configure your Nylas client
Nylas.config({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
});

const nylas = Nylas.with(process.env.ACCESS_TOKEN);

// Provide an event id to add notifications to
const EVENT_ID = '<EVENT_ID>';

// Add email notification to event
try {
  const event = await nylas.events.find(EVENT_ID);
  const newNotification  = {
    "type": "email",
    "minutes_before_event": "60",
    "subject": "Build Apps with Nylas Node SDK starts in one hour!",
    "body": "Build Apps with Nylas Node SDK starts soon!",
  };

  event.notifications = event.notifications ?
    [...event.notifications, newNotification ] 
    : [newNotification];

  await event.save();

  console.log({ notifications: event.notifications });
} catch (error) {
  console.error("Error:\n", error);
}

The Nylas Node SDK wraps the Nylas API to create a notification. Let’s try calling addEmailNotification via the terminal:

$ node addEmailNotification.js
{
  notifications: [
    EventNotification {
      type: 'email',
      minutesBeforeEvent: 60,
      body: 'Build Apps with Nylas Node SDK starts soon!',
      subject: 'Build Apps with Nylas Node SDK starts in one hour!'
    }
  ]
}

Let’s take a look at what the notification looks like once received:

email notification

Now we’ve create a email notification using the Nylas Node SDK.

Sending webhook notifications for calendar events

The benefit of creating a webhook notification is that we can programmatically initiate functionality related to an event such as communicating with a third party tool. An example of this would be creating a Slack notification.

In this section, we’ll test out webhooks using a Pipedream. Pipedream allows you to build out API workflows to test out. You will need a WEBHOOK_URL, which you can create using Pipedream.

Let’s create a webhook notification that is sent 60 minutes before the event starts:

// node-send-calendar-event-notifications/addWebhookNotification.js

// Import your dependencies
import 'dotenv/config';
import Nylas from "nylas";

// Configure your Nylas client
Nylas.config({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
});

const nylas = Nylas.with(process.env.ACCESS_TOKEN);

// Provide an event id to add notifications to
const EVENT_ID = '<EVENT_ID>';
const WEBHOOK_URL = '<WEBHOOK_URL>';
const WEBHOOK_PAYLOAD = { text: "Build Apps with Nylas Node SDK starts in one hour!" };


// Add email notification to event
const addWebhookNotification = async function() {
  try {
    const event = await nylas.events.find(EVENT_ID);

    const newNotification = {
      "type": "webhook",
      "minutes_before_event": "60",
      "payload": JSON.stringify(WEBHOOK_PAYLOAD),
      "url": WEBHOOK_URL,
    }
    
    event.notifications = event.notifications ? [...event.notifications, newNotification ] : [newNotification];

    await event.save();

    console.log({ event: event.notifications });
  } catch (error) {
    console.error("Error:\n", error);
  }
}

export default addWebhookNotification;

The Nylas Node SDK wraps the Nylas API to create a notification. Let’s try calling addWebhookNotification via the terminal:

$ node addWebhookNotification.js
{
  event: [
    EventNotification {
      type: 'email',
      minutesBeforeEvent: 60,
      body: 'Build Apps with Nylas Node SDK starts soon!',
      subject: 'Build Apps with Nylas Node SDK starts in one hour!'
    },
    EventNotification {
      type: 'webhook',
      minutesBeforeEvent: 60,
      payload: '{"text":"Build Apps with Nylas Node SDK starts in one hour!"}',
      url: '<WEBHOOK_URL>'
    }
  ]
}

Let’s take a look at what the notification looks like once received in Pipedream’s console:

Here you can see the body of the payload contains text from the webhook notification we created (”Build Apps with Nylas Node SDK starts in one hour!”).

Now we’ve created a webhook notification using the Nylas Node SDK.

Build Time!

Using Nylas you can create different notifications such as email, SMS and webhook. We’ve explored a few of them in this blog post.

You can find example code on the Nylas Samples code repository. Continue building with Nylas and learn more by visiting the Node SDK documentation

Don’t miss the action, watch our LiveStream Coding with Nylas:

Related resources

How to block time slots in Outlook and Google calendar with Nylas Calendar API

Key Takeaways Managing calendar availability is essential for professionals, teams, and businesses to stay organized…

How to Solve Webhook Integration Challenges with PubSub Notification Channel

Key Takeaways This article addresses the challenges of webhook integration and introduces the PubSub Notification…

How to Send Emails Using an API

Key Takeaways This post will provide a complete walkthrough for integrating an email API focused…