how to create recurring calendar events using rrule

How to create recurring events using RRule

6 min read

The code in the blog post has been updated to work with Nylas API V3.

Intro

In this post, we will look at how to program recurring calendar events using the Recurrence Rule, also known as RRule. RRule is a syntax used by calendar providers for communicating recurring events. Consider checking our blog post on how recurring events benefits your users.

We’ll first spend time exploring what recurrence rules are and take a look at examples. Next, we’ll spend time creating recurrence rules and looking at how to program recurring calendar events using RRule. Alternatively, you can follow our live stream on creating recurring calendar events:

Prerequisites

Consider signing up 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

This is optional if you want to follow along and try out the code snippets in JavaScript and Python:

  • If you need to configure your environment to use Node, 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.
  • If you already have the Nylas Python SDK installed and your environment is configured, then you’re ready to follow along with this post. Otherwise, I recommend that you read the post How to Send Emails with the Nylas Python SDK where the setup and configuration are clearly explained.

What is RRule?

RRule is a calendar standard syntax for creating the same events over some time. As an example, consider Google Calendar UI for creating recurring events:

google calendar custom recurrence

The following sections will focus on the output of working through this UI, a recurrence rule that is a calendar standard syntax.

What are examples of using RRule?

Let’s consider examples of recurring rules. The RRule syntax consists of many unordered keywords; let’s explore some of them to start:

  • FREQ is the only required input and refers to how often the event occurs
  • BYDAY is what day the event occurs
  • BYMONTH is which month the event occurs
  • COUNT is the total occurrences of the event
  • INTERVAL is the time between recurring events

To highlight the above list is non-exhaustive. Here is an overview of different RRule keywords and possible inputs:

recurring event, rrule keywords

You can also get an in-depth overview of recurrence rules by checking out the iCalendar Scheduling Object Specification.

Let’s decipher RRule examples using the above keywords:

  • RRULE:FREQ=WEEKLY;UNTIL=20221230T090000Z;INTERVAL=1;WKST=MO;BYDAY=MO,FR: The event occurs every Monday and Friday until the start of 2023.
  • DTSTART;TZID=America/New_York:20221201T213000 RRULE:FREQ=MONTHLY;COUNT=10;WKST=MO: The event occurs every month starting on December, 2022, on the first day of the month for the next 10 months.
  • RRULE:FREQ=MONTHLY;WKST=MO;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1: This event will be the last working day of every month.

In this section we looked at the different keywords that make up the RRule standard and examples of different RRule.

Creating RRule Syntax

Let’s take a look at how we can generate RRule Syntax. There are many RRule generators online (about +15k search results for rrule generator). I recommend looking at ones available as libraries to consume when creating recurring calendar events.

The library we used in a recent live stream on creating recurring calendar events was rrule.js. What’s awesome about rrule.js is that they have a sandbox environment available online for creating and trying different RRules:

using a rrule library like rrule.js

Let’s look at examples of using rrule.js:

// weeklyRule.js
const { RRule } = require('rrule');

// Create a rule:
const weeklyRule = new RRule({
  freq: RRule.WEEKLY,
  byweekday: RRule.TU,
  dtstart: new Date(2023, 0, 2, 9, 30, 0, 0),
  count: 10,
});

const weekly = weeklyRule.all();
console.log(weeklyRule.toString());

Running weeklyRule.js in the terminal returns:

$ node build/index.js
DTSTART:20230102T143000Z
RRULE:FREQ=WEEKLY;BYDAY=TU;COUNT=10

Let’s look at creating a recurring event that occurs monthly using rrule.js:

// monthyRule.js
const { RRule } = require('rrule');
// Create a monthly rule:
const monthlyRule = new RRule({
  freq: RRule.MONTHLY,
  dtstart: new Date(2023, 0, 4, 17, 30, 0, 0),
  until: new Date(2023, 12, 0),
  count: 10,
});
  
const monthly = monthlyRule.all();
console.log(monthlyRule.toString())

Running monthyRule.js in the terminal returns:

$ node build/index.js
DTSTART:20230104T223000Z
RRULE:FREQ=MONTHLY;UNTIL=20231231T050000Z;COUNT=10

In this section, we looked at creating RRules using rrule.js.

Create Recurring Events

Let’s look at an example of creating recurring calendar events in Python using Nylas from our code samples repository.

import os
from dotenv import load_dotenv
import datetime
from nylas import Client
# Loading all the environment variables from `.env` file
load_dotenv()

nylas = Client(
  os.environ.get('NYLAS_API_KEY'),
  os.environ.get('NYLAS_API_URI')
)

# You need to specify a GRANT_ID to create an event for a specific user
grant_id = os.environ.get("GRANT_ID")
# You need to specify a CALENDAR_ID to create an event for
calendar_id = os.environ.get("CALENDAR_ID")
# Get today’s date
today = datetime.date.today()
# Today’s date at 11:00:00 am
START_TIME = int(datetime.datetime(today.year, today.month, today.day, 15, 00, 0).strftime('%s'))
# Today’s date at 12:00:00 pm
END_TIME = int(datetime.datetime(today.year, today.month, today.day, 16, 0, 0).strftime('%s'))

events = nylas.events.create(
  grant_id,
  request_body={
    "title": "Coffee date to Discuss Recurring Events!!",
    "location": "NylasHQ!",
    "when": {
      "start_time": START_TIME, 
      "end_time": END_TIME
    },
    # RRule is a recurring event that occurs once every month
    "recurrence": {'rrule': ['RRULE:FREQ=MONTHLY;COUNT=10;INTERVAL=1;WKST=MO'], 'timezone': 'America/Toronto'},
    "participants": [{"name": "Nylas", "email": '[email protected]'}],
    "notify_participants": True
    },
  query_params={
    calendar_id
  }
)

Before running the code, ensure to set the .env variables which should include the following availably by creating a free Nylas account following the Quickstart to link an account to your account credentials.

  • NYLAS_API_KEY: The Nylas application secret key
  • NYLAS_API_URI: The Nylas API endpoint
  • GRANT_ID: The connected user’s grant ID
  • CALENDAR_ID: The specific calendar to create the event for

Running the code will recreate a recurring event:

python reoccuring_event.py

Checking your calendar will show the recurring calendar event on the 8th of every month:

example recurring event in google calendar

In this section, we looked at creating a recurring calendar event using RRule syntax and Nylas.

Build Time

Now you can create recurring events using RRule. You can find example code on the Nylas Samples code repository. You can continue building with Nylas and learn more by visiting the Nylas documentation.

You can sign up Nylas for free and start building!

Don’t miss the action, watch our LiveStream Build with Nylas:

Related resources

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.

How to integrate a React calendar component to streamline scheduling

Elevate your app’s scheduling with a React calendar component and seamlessly integrate with Nylas for efficient calendar management.