Only show these results:

Events with Node.js

The Nylas Node.js SDK is the quickest way to integrate calendars into your app with JavaScript and the Nylas Calendar API. Build your calendar integration in 15 minutes.

Node Calendar Read

Create Your Free Developer Account

Ready to build your email integration? Create your Nylas developer account to get started.

Node.js has grown to be the most popular framework in the world, primarily due to it’s efficiency for data-intensive real-time applications, and the ability to use JavaScript across the entire web stack. The Nylas Calendar API connects to all major providers including Google, Exchange, iCal, Outlook, and more.

This guide explains how to use the Nylas Node.js SDK and Calendar API to read events, create events, and respond to RSVPs. It covers the following steps:

  1. Setting up your Nylas developer account and getting your API keys.
  2. Installing the Nylas Node.js SDK.
  3. Reading calendar events, creating events and sending notifications to participants, and RSVPing to events.
  4. Exploring the Nylas Calendar API.

Prerequisites

Before you can start using the Nylas Node.js SDK, you'll need to do the following:

  • Sign up for your developer account.
  • Get your developer keys. You need to have your:
    • CLIENT_ID - The Client ID found on the dashboard page for your Nylas application.
    • CLIENT_SECRET - The Client Secret found on the dashboard page for your Nylas application.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas application.
  • Install Node.js on your machine.
  • Install either npm or yarn.

Install the Nylas Node.js SDK

Run either npm install nylas or yarn add nylas in your terminal.

Congratulations!

You’re now ready to write code with the Nylas Node.js SDK.

The Nylas Client Object

At its core, the Nylas Communication Platform is an API client that interfaces with all major contact book providers. Because the Nylas object provides access to every resource in the Nylas Calendar API, the first thing to do is to initialize it with the code below:

const Nylas = require('nylas');   

Next, call the config function and pass the CLIENT_ID and CLIENT_SECRET from earlier in this guide, replacing the placeholders with your values:

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

Be Careful with Secrets

Make sure to follow best practices when including authentication secrets like this in your code. Increase security by storing these values as environment variables.

Lastly, call the with function and pass it the ACCESS_TOKEN from earlier in this guide. This allows Nylas to access data for a specific user account.

const nylas = Nylas.with(ACCESS_TOKEN);   

You’re now ready to start working with calendar events.

Read Calendar Events

Many user accounts have access to multiple calendars which you can access using the nylas.calendars object. Available calendars include the default calendar, any calendars the user has created, and any calendars they've added, such as those for other users or for their teams.

The "Emailed Events" calendar is a unique calendar that includes all events the user receives through an email invite. This calendar will be important later when we RSVP to an event.

In the example shown below, .list() is called to return all calendars associated with the account, and logs each of the calendars’ name, id, and read_only values. The name and id values are useful for identifying and referring to specific calendars. The read_only value is a boolean that lets you know whether the user has write access to the calendar.

nylas.calendars.list().then(calendars => {
for (const calendar of calendars) {
console.log(`Name: ${calendar.name} | ID: ${calendar.id} | Read Only: ${calendar.readOnly}`);
}
});

Review the API documentation for the calendar endpoints to learn more about what you can do with user calendars and Nylas.

The nylas.events object makes it easy to access information about a user’s events. The example below will return the next ten events after the current moment in time on a user’s calendar:

const now = (new Date).getTime()/1000;
nylas.events.list({starts_after: now, limit: 10 , calendar_id: CALENDAR_ID}).then(events => {
for (const event of events) {
console.log(
`Title: ${event.title} | `,
`Status: ${event.status} | `,
`ID: ${event.id}`,
);
}
});

First, the script defines the variable now, which is a Unix Epoch timestamp represented in seconds. This is the time format Nylas uses for event objects. Then, .list() is called with a few filters:

  • starts_after - This selects events that occur after the specified time. This example passes the Unix epoch timestamp we stored as the constant now.
  • limit - This limits the number of events that are returned to ten. Check out our API documentation on pagination to learn more about using limits and offsets to paginate results.
  • calendar_id - This is the id for one of the user’s calendars. Replace CALENDAR_ID with the appropriate value.

Finally, the script iterates through the list of events and logs the title, status, and id for each of them. Take a look at the API documentation for the event endpoints to learn more about the attributes the event object contains.

Below is the full code example for reading calendars and events from a user account:

const Nylas = require('nylas');
Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);

// Most user accounts have multiple calendars where events are stored
nylas.calendars.list().then(calendars => {
for (const calendar of calendars) {
// Print the name of each calendar, it's ID, and whether or not the calendar is read_only
console.log(`Name: ${calendar.name} | ID: ${calendar.id} | Read Only: ${calendar.readOnly}`);
}
});
// The Nylas API keeps track of time with Unix Epoch seconds timestamps
const now = (new Date().getTime())/1000;
// Return the the next 10 events after the current time
// CALENDAR_ID should be replaced with the ID for a specific calendar
nylas.events.list({starts_after: now, limit: 10 , calendar_id: CALENDAR_ID}).then(events => {
for (const event of events) {
// Print the event title, confirmation status, and ID.
console.log(
`Title: ${event.title} | `,
`Status: ${event.status} | `,
`ID: ${event.id}`,
);
}
});

Create an Event and Send Invitations

It’s now time to create your first event. An example is shown below:

const { default: Event } = require('nylas/lib/models/event');

const event = new Event(nylas, {
title: 'New Years Party!',
location: 'My House!',
when: { startTime: 1546290000, endTime: 1546300800 },
participants: [{ email: '[email protected]', name: 'My Friend' }],
calendarId: CALENDAR_ID
});

This example creates a new event object and assigns it a title and location. For more about events object parameters, view our API documentation on events.

Event times are set with UTC timestamps and can be one of four sub-objects that correspond to the time and duration of an event:

  • time
  • timespan
  • date
  • datespan

Check out our API documentation for event subobjects to learn more.

Participants are added as an array of participant subobjects and require an email field. The name field is optional.

For CALENDAR_ID, you'll need the ID for the calendar that the user has write access to. Take a look at our quickstart example for reading calendars and events to learn how to find a calendar ID. Then, replace CALENDAR_ID with the appropriate value.

Finally, we’ll save the event to the calendar and notify the participants with the code below:

event.save({ notify_participants: true }).then(event => {
console.log(event);
});

The entire example is shown below:

const Nylas = require('nylas');
const { default: Event } = require('nylas/lib/models/event');

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

const nylas = Nylas.with(ACCESS_TOKEN);

const event = new Event(nylas, {
title: 'New Years Party!',
// Event times are set via UTC timestamps
// This example creates an event on December 31, 2018
when: { startTime: 1546290000, endTime: 1546300800 },
// Participants are stored as an array of participant subobjects
participants: [{ email: '[email protected]', name: 'My Friend' }],
location: 'My House!',
// calendarID must be the ID for a calendar the user has write access to.
calendarId: CALENDAR_ID
});

// Event notification emails are not sent by default
// Enable notify_participants to send an email notification to participants
event.save({ notify_participants: true }).then(event => {
console.log(event);
});

RSVP to Calendar Events

Now it’s time to take a look at how to RSVP to events. An example script is shown below:

nylas.events.first({calendar_id: EMAILED_CALENDAR_ID, title: "Birthday Party!"}).then(event => {
event.rsvp('yes', 'I can\'t wait to see the birthday girl!')
})

This script uses .first() to return the first event and passes two filters to control the result:

  • calendar_id - This is the id for the Emailed Events calendar that contains all event invites the user has received through an email invite. Replace EMAILED_CALENDAR_ID with the appropriate value.
  • title - This is the title of the event to RSVP to.

Check out our API documentation to learn about how filters work.

If you know the id of the specific event that you want to RSVP to, you can also use nylas.events.find({event_id}) to select a specific event, replacing {event_id} with the appropriate value.

Finally, event.rsvp() is called to send a 'yes' response, along with an RSVP message. You can also pass a 'no' or 'maybe' response to .rsvp(). Check out the API documentation for theRSVP endpoint to learn more.

The entire code example to RSVP to calendar events is below:

const Nylas = require('nylas');
Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);

// CALENDAR_ID should be the id for the 'Emailed Events' calendar, which are calendar invitations
nylas.events.first({calendar_id: EMAILED_CALENDAR_ID, title: "Birthday Party!"}).then(event => {
event.rsvp('yes', 'I can\'t wait to see the birthday girl!')
})

Explore the Nylas Calendar API

If you’ve made it this far, congrats! You’ve integrated your first calendar with the Nylas Calendar API. There's plenty more that you can do with it. Take a look at the following resources to learn more about the Nylas Communications Platform capabilities: