Nylas Docs

The Nylas Developer Hub

Welcome to the Nylas developer hub. You'll find comprehensive guides and documentation to help you start working with Nylas as quickly as possible, as well as support if you get stuck. Let's jump right in!

Developer Guide

Create Calendar Events with Python

Learn how to create calendar events with the Nylas Python SDK. Build your calendar integration in 15 minutes.

Python is one of the most popular programming languages in the world due to its extensive collection of libraries, easy to understand syntax, and practical abstraction capabilities. In fact, we love Python so much that we’ve used it to build the Nylas Communications Platform which enables developers to quickly integrate email, calendar, and contacts into their app. The Nylas Calendar API connects to all major providers including Google, Exchange, iCal, Outlook and more, and our Python SDK makes it simple to create events in user accounts.

This guide explains how to use the Nylas Python SDK and Calendar API to create events. It covers the following steps:

  1. Setup your Nylas developer account and get your API keys
  2. Install the Nylas Python SDK
  3. Create an event and send notifications to participants.
  4. Explore the Nylas Calendar API

Create Your Free Developer Account

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

Setup Your Developer Account

After signing up for your Nylas developer account, follow our guide to get your API keys and authorize your first email account. When you complete this guide, you should have three tokens that you need to run the code examples found below:

  • 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

Install the Nylas Python SDK

Prerequisites

  • Ensure you have pip installed on your development environment.
  • Create a virtual environment to install Nylas.

Refer to the official Python documentation on pip and virtualenv to learn more about how to set this up.

Installing the SDK

With your virtual environment activated, run pip install nylas.

Congrats! You’re now ready to write code with the Nylas Python SDK.

Create an Event and Send Invitations

At its core, the Nylas Communication Platform is an API client that interfaces with all of the major calendar providers. First, import the APIClient class from the nylas package, and create a new instance of this class, passing the variables you gathered when you got your developer API keys. In the following example replace CLIENT_ID, CLIENT_SECRET, and ACCESS_TOKEN.

from nylas import APIClient

nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN,
)

Take care with your secrets

It’s not best practice to include secrets like this in your code. A more secure way to provide these values is to store them as environment variables and access them with the os.environ module.

Please note, it’s a bad practice to include secrets like this in your code. If you want to be more secure, you should store these values as environment variables and access them with the os.environ module.

Now, it’s time to create a new event and start adding content to it:


event = nylas.events.create()
event.title = "New Years Party!"
event.location = "My House!"
event.description = "We'll ring in the new year in style!"

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.

Next, let’s set a time for this event, and add a participant:

event.participants = [{"name": "My Friend", 'email': 'my.friend@example.com'}]
event.when = {"start_time": 1577829600, "end_time": 1577840400}

Participants are added as an array of participant subobjects and require an email field, while name is optional. 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. Take a look at the reference documentation for event subobjects to learn more. Pro tip: datetime is a very effective python module for converting UTC timestamps to a human readable format.

Finally, we’ll save the event to the calendar and notify the participants. For this, 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, in the following example, replace CALENDAR_ID with the appropriate value.


event.calendar_id = CALENDAR_ID
event.save(notify_participants='true')

One important note is that notify_participants is set to false by default. If you want to send email notifications to participants, this must be set to true.


from nylas import APIClient

nylas = APIClient(
    CLIENT_ID,
    CLIENT_SECRET,
    ACCESS_TOKEN,
)

event = nylas.events.create()
event.title = "New Years Party!"
event.location = "My House!"
# Event times are set via UTC timestamps
# This example starts on December 31, 2019 at 10 PM GMT.
event.when = {"start_time": 1577829600, "end_time": 1577840400}

# Participants can be added via a list of participant subobjects
event.participants = [{"name": "My Friend", 'email': 'my.friend@example.com'}]

# CALENDAR_ID must be the value of a calendar the user account has write access to.
event.calendar_id = CALENDAR_ID
# Event emails are not sent by default
# Enable notify_participants to send an email notification to participants
event.save(notify_participants='true')
Ready to Build Your Calendar Integration?

Explore the Nylas Calendar API

If you’ve made it this far, congrats, you’ve created your very first calendar event 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.

Create Calendar Events with Python


Learn how to create calendar events with the Nylas Python SDK. Build your calendar integration in 15 minutes.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.