Creating a Credit Card Bill Notification using Gmail API, Google Calendar API, and Python


Recently, I read about the importance of paying your credit card bills on time and the repercussions of failing to do so. I have been fortunate to be able to pay my credit card bills on time so far, however I realized there aren’t enough measures in place to prevent me from missing a bill payment. Thankfully my bank sends out a notification email to my Gmail Account, informing me of when my next payment is due and how much I need to pay.

I often read the email and forget to set a Calendar notification for myself and I figured I could probably write a script to do just this. So the purpose of this blog is to teach you how to retrieve relevant information from your Gmail Account and how to create a Google Calendar notification with this information.

Full disclaimer: The authentication portion of the code should work for everyone however the specific details of the email content and the email address needs to be modified for your use case.


You should have python installed on your machine first. If you do not then simply go to this link and install the latest version of python. Follow the interactive wizard in order to setup python locally on your machine.

You also would need a Google Account for this since you will be interacting with both the Gmail and the Google Calendar API.

Next, you need to set up authentication to the Gmail API as well as the Google Calendars API. Kindly follow the tutorials below to enable the API as well as to install the necessary libraries.

Google Calendar:


To enable both APIs, you can put any name for the project name, then select Desktop App and finally click Download Client Configuration. Copy this file to the working directory of your application and rename the Calendar API file to calendarCredentials.json and the Gmail API file to gmailCredentials.json.


As you can see in the code below, this main file will import 3 files that we will define for each major functionality. The scope variables are API parameters that will be provided to authenticate to the Gmail and Calendar API respectively.

To summarize what the code is doing above, we first will authenticate to the Gmail API, if the authentication works then we will extract the necessary information from the Notification Email, next we will authenticate to the Calendar API, and finally create a Google Calendar Notification with the email information.

Main function to run the entire application

Now that we have the main file, we need to define the 3 imported files to make this application work. Next we will define the file to handle the authentication.

Please see below for the file called and the purpose of it is to set up the authentication to both Gmail and Calendar API. As you can see in the code, this function will take in a type and a scope variable from the main function and this will define whether we want to authenticate to the Gmail API or the Calendar API in the current function call.

The scope is an API parameter that is defined in the file. So what this does is if we need to authenticate to the Gmail API, we will supply our login configuration via the json files we downloaded from the API setup guide. Then we will create a pickle file that will be used to store the session token.

First Time Authentication

If the session token file does not exist then that means we need to authenticate to Gmail for the first time and you will get a browser redirect to do this. If the session token file is expired or not valid anymore, then we will refresh the session token.

Set up session tokens to Google Calendar API and Gmail API

Next, we need to set up the logic to access our email, find the notification email, and extract the necessary data from it.

In this code, we use our authenticated credentials to access the Gmail API. Line number 14 is the crucial command. This is the API command that will extract all the emails from your bank’s email address. So you would insert your bank’s email after the from: portion, and this will fetch all of their emails.

We then have to parse the messages, and loop through each email to request more details about each email in another API command. The reason for this is that Gmail will only return a short portion of an email’s heading to deal with large requests. Therefore, we need to get the full heading for each of these emails to find the notification email of interest to us.

In my case, my bank usually includes the keyword “e-Statement” in their notification emails so I simply searched for that and extracted the matching email from the list. The Gmail API will return emails in chronological order so the newest email will be at the beginning of the list. Next, I extracted the entire body of the email and parsed the fields of importance to me. If this format is different for your bank’s emails then lines 42–47 is where you would need to change to cater to their email structure.

File to extract information from an email using the Gmail API

Now that we have the crucial information, we need to set up a Google Calendar event for this.

Below is the final file called which will simply create a Google Calendar Event using the email information we extracted. In this file, we first login to the Google Calendar API and then convert the due date from a string into a datetime format that Google will accept.

Line 9 is very specific to Google datetime formats so that is why it may seem strange. Then we want to set up the calendar notification to the day before the actual due date and that is the purpose of Line 11. We then check if the due date has already passed the current date and if it has not then we will search for the last 10 events from the due date until now.

The purpose of this is to check if we have already created this credit card notification event previously. Then we parse the event results and check if the credit card notification event exists in the list of events we extracted. If it does not then we create the notification event between Lines 31 and 41.

We first create our json body and pass in the necessary parameters for the event, and then we actually insert this new event using the Calendar API.

File to create a calendar event using Google Calendar API

Final Step

Now that you have placed the file, file, file, file, the gmailCredentials.json file, and the calendarCredentials.json file into a folder you are ready to run the final command! Your final folder structure should look like the screenshot below.

Final folder structure

See below for the command to run your application!


After running the command above, you should receive a new window popup asking you to sign into your Gmail account. After selecting your email address to use, click Advanced and Go to “your project’s name”. Allow permissions to the API and you should be done.

You should then get a second pop up to grant permissions to the Calendar API and should follow the same approach above to do this. Hopefully you will get a print message stating that the Event was created along with a link to the calendar event.


As you can see below, my next upcoming bill payment is on March 3.

My upcoming bill payment

Your event will be on the day right before the due date of your upcoming bill payment. I hope this guide worked for you and prevented you from missing your upcoming bill payment! See you on my next blog post!

Software Developer with a passion for Mobile Application Development, Machine Learning, and DevOps.