How to Manage Calendar Events with the Nylas Node SDK

Using the Nylas Node SDK to manage calendar events with minimal code.

How to Manage Calendar Events with the Nylas Node SDK

Intro

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:

// node-manage-calendar-events/listCalendars.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);

// list calendars
try {
  const calendars = await nylas.calendars.list({ limit: 5 });
  calendars.map(({ name, description, id}) => console.log({ name, description, id }));
} catch (error) {
  console.error("Error:\n", error);
}

The Nylas Node SDK wraps the Nylas API to retrieve a list of 5 calendars. Let’s try calling listCalendars via the terminal:

$ node listCalendars.js
{
  name: 'Emailed events',
  description: 'Emailed events',
  id: '<CALENDAR_ID>'
}
{
  name: '[email protected]',
  description: 'Work calendar events',
  id: '<CALENDAR_ID>'
}
{
  name: 'Holidays',
  description: 'Holidays and Observances',
  id: '<CALENDAR_ID>'
}
{
  name: 'Birthdays',
  description: 'Displays birthdays, anniversaries, and other event dates of people.',
  id: '<CALENDAR_ID>'
}
{
  name: 'PTO calendar',
  description: 'PTO calendar for the team',
  id: '<CALENDAR_ID>'
}

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.

Let’s try running this script in the terminal:

$ node createCalendarEvent.js
{ 
  id: '<EVENT_ID>',
  calendarId: '<CALENDAR_ID>',
  title: 'Build More Awesome with Nylas Node SDK!',
  owner: 'Ram Bansal <[email protected]>',
}

If everything worked, you will see the message in the terminal where the event will be listed with an id, calendarId, title and owner.

Here is what the email invite will look like:

Google Calendar Invite

Here is what the Google email calendar invite will look like:

We’ll use the the event id (<EVENT_ID>) in the coming sections to update the event.

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

Updating a Calendar Event

Let’s try updating the calendar event previously created using the Nylas Node SDK. Let’s create updateCalendarEvent.js:

// node-manage-calendar-events/updateCalendarEvent.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 the event Id to modify
const EVENT_ID = '<EVENT_ID>';

// Update the event event
const updateCalendarEvent = async function() {
  try {
    const event = await nylas.events.find(EVENT_ID)
    event.title = 'Build Awesome Apps with Nylas Node SDK!'
    const response = await event.save();
    console.log({ response });
  } catch (error) {
    console.error("Error:\n", error);
  }
};

updateCalendarEvent();

Make sure to update the following variables:

  • EVENT_ID to be the id of the event that we created

Let’s try updating the event via the terminal:

$ node updateCalendarEvent.js
{
  id: '<EVENT_ID>',
  calendarId: '<CALENDAR_ID>',
  title: 'Build Awesome Apps with Nylas Node SDK!',
  owner: 'Ram Bansal <[email protected]>',
}

If everything worked, you will see the message in the terminal where the event will be listed with an id, calendarId, updated title and owner.

Now we’ve updated one of our first events created using the Nylas Node SDK.

Deleting a Calendar Event

Let’s remove the event we created using the Nylas Node SDK. Let’s create deleteCalendarEvent.js:

// node-manage-calendar-events/deleteCalendarEvent.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 the event Id to delete
const EVENT_ID = '<EVENT_ID>';

// Delete the event
const deleteCalendarEvent = async function() {
  try{
    const response = await nylas.events.delete(EVENT_ID);
    console.log({ response });

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

export default deleteCalendarEvent;

Make sure to update the following variables:

  • EVENT_ID to be the id of the event that we created

Let’s try removing the event via the terminal:

$ node deleteCalendarEvent.js
{ response: { job_status_id: '<JOB_STATUS_ID>' } }

If everything worked, you will see the message in the terminal where the job_status_id will be listed and the event will be removed from the calendar.

Participants will receive an email that the event has been cancelled:

Now we’ve deleted our event using the Nylas Node SDK.

Build Time!

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

You May Also Like

Why it’s mission-critical to build bi-directional email in your CRM with an email API… now
Why it’s mission-critical to build bi-directional email in your CRM with an email API… now
hero banner
Everything you need to know about Microsoft’s basic authentication deprecation
How we secure APIs at Nylas using JSON Web Tokens