- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
The code in the blog post has been updated to work with Nylas API V3.
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:
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.
This is optional if you want to follow along and try out the code snippets in JavaScript and Python:
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:
The following sections will focus on the output of working through this UI, a recurrence rule that is a calendar standard syntax.
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 occursBYDAY
is what day the event occursBYMONTH
is which month the event occursCOUNT
is the total occurrences of the eventINTERVAL
is the time between recurring eventsTo highlight the above list is non-exhaustive. Here is an overview of different RRule keywords and possible inputs:
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.
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:
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
.
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 keyGRANT_ID
: The connected user’s grant IDCALENDAR_ID
: The specific calendar to create the event forRunning 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:
In this section, we looked at creating a recurring calendar event using RRule syntax and Nylas.
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:
Ram loves teaching, building and exploring technologies. He is passionate about empowering developers to ship amazing products to market as fast as possible ????. Ram is excited to share knowledge and help others. He’s a Relaxed Tomato ????.