Only show these results:

Events with Node.js

Beginner

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-nylas-account
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, and our Node.js SDK makes it simple work with calendars and events.

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. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Node.js SDK
  3. Read calendar events, create an event and send notifications to participants, and RSVP to events
  4. Explore the Nylas Calendar API

Prerequisites

Before you can start using the Nylas Node.js SDK, make sure you have done 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 App.
    • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas App.
  • Get Node.js installed on your machine.
  • Make sure you have npm or yarn.

Install the Nylas Node.js SDK

Run npm install nylas or yarn add nylas.

Congrats! 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 of the major contact book providers. The Nylas object provides access to every resource in the Nylas Calendar API, so the first thing to do is to initialize it:

const Nylas = require('nylas');   

Then, 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,
});

Take care with your secrets
It’s not best practice to include secrets like this in your code. For increased security, [store these values as environment variables][6].

Next, 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);   

Now you’re ready to start working with calendar events.

Read Calendar Events

Many user accounts have access to multiple calendars; you can access these via the nylas.calendars object. Available calendars include the default calendar, any calendars the user has created, and any calendars they have 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 via email invite; this calendar will be important later when we RSVP to an event.

In the following example, .list() is called to return all calendars associated with the account, and log each of the calendars’ name, id, and read_only values. The name and id values are useful for identifying and referring to specific calendars, and read_only 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 reference for the calendar endpoints to learn more about what Nylas enables you to do with user calendars.

The nylas.events object makes it easy to access information about a user’s events. The next example will return the next 10 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 - selects events that occur after the specified time. This example passes Unix epoch timestamp we stored as the constant now.
  • limit - limits the number of events that are returned to 10. Check out the API reference for pagination to learn more about using limits and offsets to paginate results.
  • calendar_id - 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 reference for the event endpoints to learn more about the attributes the event object contains.

Here 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
var 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

Now, it’s time to create your first event.


const event = nylas.events.build({
title: 'New Years Party!',
location: 'My House!',
when: { start_time: 1546290000, end_time: 1546300800 },
participants: [{ email: 'my.friend@example.com', name: 'My Friend' }],
calendarId: CALENDAR_ID
});

This example creates a new event object and assigns it a title and location. Take a look at the API reference for events to learn more about the parameters you can assign to an events object.

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, or datespan. Visit the reference documentation for event subobjects to learn more.

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

For CALENDAR_ID, you will need the ID for the calendar that the user has write access to. Take a look at the 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.

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

Here is the entire example.


const Nylas = require('nylas');

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

const nylas = Nylas.with(ACCESS_TOKEN);

const event = nylas.events.build({
title: 'New Years Party!',
// Event times are set via UTC timestamps
// This example creates an event on December 31, 2018
when: { start_time: 1546290000, end_time: 1546300800 },
// Participants are stored as an array of participant subobjects
participants: [{ email: 'my.friend@example.com', 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.

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 - the id for the “Emailed Events” calendar that contains all event invites the user has received via email. Replace EMAILED_CALENDAR_ID with the appropriate value.
  • title - the title of the event to RSVP to.

Head to the API reference 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 'no' or 'maybe' to .rsvp(). Check out the API reference for the RSVP endpoint to learn more.

Here is the entire code example to RSVP to calendar events.

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 is plenty more that you can do with it: take a look at the following resources to learn more about the Nylas Communications Platform capabilities.