Google Calendar API: Connect using Node.js and an API key
Sometimes working out which API method to use to connect to the Google APIs can be a bit confusing — let me help.
There are 3 methods to connect to the Google APIs:
- A simple API key
- A service account with a JSON or P12 private key
But which method to use?
Which method to use depends on what you are trying to achieve. Without diving into great detail, let me explain the differences at a very high level;
- Generally you would use the API key method to consume a Google API which provides you with a consumable service, for example the Google Maps API which provides you with Google Maps data,
- OAuth enables anyone to give you some level of access to their data, for example a calendar SaaS app that integrates with your Google Calendar and asks for your permission to manage your calendar,
- A service account would be used by a Sysadmin or Developer to take full control of a G Suite Domain’s user data, for example to give a business application full control of all users calendars to add and remove calendar events behind the scenes.
What the documentation doesn’t tell you
What the Google Calendar API documentation doesn’t explain (at least anywhere that I could find) is that unlike all the other methods it provides, the freebusy method only requires an API key. I discovered this after much frustration at not being able to make a freebusy query using a service account (like you would with any of the other methods provided by the Google Calendar API).
The example below uses the googleapis library to connect to the Google Calendar API and get freebusy information for a calendar between 3 pm and 4 pm the next day. You could very easily adapt this code to suit your own purposes and to connect to a different Google API — the method is exactly the same.
Let’s get started
You are going to need:
- A Google API key with the Google Calendar readonly scope enabled
- A Google Calendar with its sharing settings set so that freebusy information is publicly available
- A Node.js runtime environment (your local machine, AWS Lambda, Firebase Functions, your server, etc)
Make sure to update your api key and the calendar you want to query.