In this post, we are going to learn how to use the Nylas Node SDK to manage calendar events. This will be useful to streamline working with calendars with minimal coding. We’ll first set up our environment, and then we’ll start accessing calendars using JavaScript. You can also find example code on the Nylas Samples code repository.
Prerequisites
Sign up here if you need to create a Nylas account for free! Follow the Quickstart to receive your account credentials. Ensure to save the access token to use below.
Environment
If you have used the Nylas Node SDK before, continue to the next section. If you need to configure your environment, take a look at the Environment setup 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.
Reading calendars
Let’s first code how to list out calendars. Let’s create listCalendars.js:
If everything worked, you will see the message in the terminal where five calendars will be listed with a name, description, and id. We’ll be using one of the calendars listed in the coming sections, so take note of the calendar id.
Now we’ve listed five calendars using the Nylas Node SDK.
Reading calendar events
Let’s read events from a calendar using the Nylas Node SDK. Let’s create listCalendarEvents.js:
// node-manage-calendar-events/listCalendarEvents.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 a time stamp to fetch events after this time
const START_AFTER = '<UNIX_TIME_STAMP>';
// Provide the calendar id you want to fetch events from
const CALENDAR_ID = '<CALENDAR_ID>';
// List calendar events
const listCalendarEvents = async function() {
try {
const calendarEvents = await nylas.events.list({
calendar_id: CALENDAR_ID,
starts_after: STARTS_AFTER,
limit: 5,
});
calendarEvents.map(({ title, id, description, when}) =>
console.log({
id,
title,
description,
when,
}));
} catch (error) {
console.error("Error:\n", error);
}
}
export default listCalendarEvents;
Make sure to update the CALENDAR_ID and STARTS_AFTER values as follows:
CALENDAR_ID to be the id of a calendar that was listed in the terminal
STARTS_AFTER to be sometime this morning so we can view events for today (Tue May 31 2022 09:00:00 GMT-0400 is 1654002000)
There are many configurations you can pass to retrieve events from a Calendar, have a look at the Nylas API for more options.
Let’s try running this script in the terminal:
$ node listCalendarEvents.js
{
id: '<EVENT_ID>',
title: '⌨️ Code',
description: 'Write some code',
when: When {
object: 'timespan',
startTime: 1654032600,
endTime: 1654034400
}
}
{
id: '<EVENT_ID>',
title: '☕☕ More Coffee',
description: 'Have more coffee',
when: When {
object: 'timespan',
startTime: 1654034400,
endTime: 1654036200
}
}
{
id: '<EVENT_ID>',
title: '⌨️⌨️ More Code',
description: 'Write more code',
when: When {
object: 'timespan',
startTime: 1654036200,
endTime: 1654038000
}
}
{
id: '<EVENT_ID>',
title: '???? Ship w/ Nylas',
description: 'Ship App build with Nylas!',
when: When {
object: 'timespan',
startTime: 1654038000,
endTime: 1654039800
}
}
{
id: '<EVENT_ID>',
title: '???? ???? Ship more!',
description: 'Ship more App features!',
when: When {
object: 'timespan',
startTime: 1654039800,
endTime: 1654041600
}
}
If everything worked, you will see the message in the terminal where five events will be listed with an id, title, description and when object containing a startTime and endTime.
Now we’ve listed five calendar events using the Nylas Node SDK.
Creating a calendar event
Let’s try creating a calendar event using the Nylas Node SDK. Let’s create createCalendarEvent.js:
// node-manage-calendar-events/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 START_TIME = 1654023600;
const END_TIME = 1654027200;
// Provide the calendar id you want to add an event to
const CALENDAR_ID = '<CALENDAR_ID>';
// Provide details of one participant
const PARTICIPANT_EMAIL = '<PARTICIPANT_EMAIL>'
const PARTICIPANT_NAME = '<PARTICIPANT_NAME>'
// Create an event
const createCalendarEvent = async function() {
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,
}
],
});
const { id, calendarId, title } = await event.save();
console.log({ id, calendarId, title });
} catch (error) {
console.error("Error:\n", error);
}
};
createCalendarEvent();
Make sure to update the following variables:
CALENDAR_ID to be a calendar listed in the terminal from earlier
START_TIME to be sometime later today, say 3pm (Tue May 31 2022 15:00:00 GMT-0400 is 1654023600)
END_TIME to be an hour after the start time, say 4pm (Tue May 31 2022 16:00:00 GMT-0400 is 1654027200)
PARTICIPANT_NAME and PARTICIPANT_EMAIL to be a fellow colleague, yourself, or you can use [email protected]
There are many configurations you can pass to create events, have a look at the Nylas API for more options.