A guide on connecting Nylas to your iCloud account

Need to connect to your iCloud account services like iCalendar and iMail? Here’s the guide for you. Using Python to illustrate the examples.

A guide on connecting Nylas to your iCloud account

We usually rely on Google and Microsoft for our mailbox and calendars, forgetting (if we are iPhone users) that an iCloud account is at the core of our phones.

Let’s learn how to connect our iCloud account to Nylas to access our email, events and contacts.

In this tutorial, we’ll use the Nylas Python SDK to access the Nylas APIs, since Python is one of the most popular programming languages right now.

Is your system ready?

If you already have the Nylas Python SDK installed and your Python environment configured, skip to the next section. Also, you can check the Nylas Quickstart Guides.

If this is your first time working with the Nylas SDK, I recommend reading the post How to Send Emails with the Nylas Python SDK, where I explain the basic setup.

What are we going to talk about?

Which account should I use?

We’re going to assume that this is our very first time interacting with an iCloud account, so naturally, we don’t know where to begin. The one thing we do know is that we have an AppleId email account which we use to connect to iCloud.

We’ll go to our Nylas Dashboard and log in. Click on our application and then on Connect an email account:

Connect email account

Once in there, we’re going to try to connect an account. Leave the scopes checkboxes selected, and enter our Apple ID email address:

Connect an account

The selected provider on the login prompt changes depending on which email address we use as our Apple ID:

Login with provider

Enter your iCloud account password and see what happens. You should see it fail with an error message like this:

Need app password

So, most likely, we’re going to change the provider to iCloud account and make sure we have two-factor authentication enabled and will create an app password (An app password is a token of authorization that we can use in several services).

Enabling two-factor authentication

We can do this using any of the following three methods:

  • On your iPhone, iPad or iPod Touch: Go to settings → your name → Password & Security. Tap on Turn on Two-Factor authentication and follow the instructions.
  • On your Mac: Go to the Apple Menu → System Settings or System Preferences → Select your name or Apple ID → Click on Password & Security → Click on Turn on Two-Factor authentication and follow the instructions.
  • On the web: Go to appleid.apple.com and sign in with your Apple ID → Answer the security questions → Click on continue when prompted to upgrade your account security and follow the instructions.

This will add an extra layer of security to your account, so every time you log in you will need to approve it:

Two-Factor Authentication

Creating an App Password

We can now continue with the next step, which is creating a new app password.

If you are no longer connected to appleid.apple.com, log in again.

In the Sign-in and Security section, select App-Specific Passwords:

Look for App Password

A pop-up window appears and prompts you to generate a new app-specific password

Create App Password

Click the plus sign, enter “Nylas” as a name, and copy the generated password.

Trying to log in again

Since we know that we’re logging into the iCloud account, we can go back to the Nylas dashboard, click Select a different provider and select iCloud from the list. Then use our new App password, and try to log in again, just to fail one more time:

Need App Password

Why? Well, it turns out that our Apple ID email is not exactly the same as our iCloud email. Do we even have an iCloud email?

Creating an iCloud email account

While you’re supposed to be able to use your iPhone, iPad or Mac to create an iCloud email account, doing it on the Mac doesn’t always work, so we’ll do it on the iPhone.

Go to Settings → your name → iCloud → Tap on iCloud Mail then follow the instructions.

If it fails, simply log out and log in again and then try again.

Create iMail account

Our iCloud Mail account should be ready to use.

Log in using iCloud email account and App Password

Finally, we should be able to log in using our new iCloud email account and App Password.

This time, it should work and will look like this on our Nylas dashboard.

iCloud setup

When you do this, Nylas creates an access token. Make sure you copy it and store it in a safe place.

Give it a couple of minutes to sync and get everything into place (Nylas and Apple).

Testing out our iCloud integration

Now that everything is working as expected, let’s test it out by creating a simple Python script to read out our mailbox. This should be almost as empty since we just created it.

On your .env file add the following at the bottom of the file:

ICLOUD = “YOUR_ICLOUD_ACCESS_TOKEN”

Remember that Nylas access token you saved? That’s what you put here. 

Next, we move on to the Python script. Let’s call it Read_iCloud_Inbox.py and copy and paste the following code:

# Import packages
import os
from nylas import APIClient
from dotenv import load_dotenv
load_dotenv()

# Initialize Nylas client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ICLOUD"),
)

# Call the messages endpoint
messages = nylas.messages
# Display the Subject, who sends the email
for message in messages:
	print("Subject: {} | From: {}".format(message.subject, message.from_))
	print("\n")

We can run this code by typing the following in a terminal window:

$ python3 Read_iCloud_Inbox.py
Read iCloud Inbox

Nice, we have one welcome email from iCloud!

Now, let’s take a look at iCalendar, since we most likely want to access our events as well.

For this, we will need two scripts: one to get the ids of our calendars and the second to access the events contained in each calendar.

Let’s start with getting our calendars. Create a script called Read_iCloud_Calendars.py:

# Import packages
import os
from nylas import APIClient
from dotenv import load_dotenv
load_dotenv()

# Initialize Nylas client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ICLOUD"),
)

# Call calendars endpoint
calendars = nylas.calendars.all()
# Display name and description
for calendar in calendars:
	print("Id: {} | Name: {} | Description: {}".format(
          calendar.id, calendar.name, calendar.description))

This will bring back a bunch of calendars, but in this case, we’re only interested in the one named Blag. You will have different calendars for sure:

Read iCloud Calendars

Let’s grab the Id as we’re going to use it on our next script which will be called Read_iCloud_Events.py:

# Import packages
import os
import datetime
from datetime import date
from nylas import APIClient
from dotenv import load_dotenv
load_dotenv()

# Initialize Nylas client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ICLOUD"),
)

# Get today’s date
today = date.today()
# Specify from and to dates, so we take the whole day
AFTER = int(datetime.datetime(today.year, today.month, today.day, 0, 0, 0).strftime('%s'))
BEFORE = int(datetime.datetime(today.year, today.month,today.day, 23, 59, 59).strftime('%s'))

# Call the events endpoint
events = nylas.events.where(calendar_id="CALENDAR_ID", starts_after=AFTER, ends_before=BEFORE)
# Print the Id, title, start, end and participants
# Determine whether is a timed event or a whole day event
for event in events:
    if "start_time" in event.when:
        print("Id: {} | Title: {} | Start: {} | End: {} | Participants: {}\n".format(
                        event.id,
                        event.title, 
                        datetime.datetime.fromtimestamp(event.when['start_time']).strftime('%Y-%m-%d %H:%M:%S'), 
                        datetime.datetime.fromtimestamp(event.when['end_time']).strftime('%Y-%m-%d %H:%M:%S'), 
                        event.participants
             ))		
        else:
            print("Id: {} | Title: {} | Date: {} | Participants: {}\n".format(
                   event.id, event.title, event.when["date"],event.participants
            ))

This will return all the events on that particular calendar:

Read iCloud events

With that, we have successfully integrated our iCloud account with Nylas.

What’s next?

To learn more about our Email APIs, go to our documentation Email API Overview.

To learn more about our Calendar APIs, go to our documentation Calendar API Overview.

You can sign up for Nylas for free and start building!

Don’t miss the action, join our LiveStream Coding with Nylas!

You May Also Like

The Nylas mascot, Nyla, standing next to a scheduling UI
Introducing Nylas Scheduler v3: Experience the future of in-app scheduling
Transactional Email APIs vs Contextual Email APIs
Best email tracker
Find the best email tracker and elevate your app’s email game

Subscribe for our updates

Please enter your email address and receive the latest updates.