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.
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: