Google Calendar API: How To Use It In 4 Simple Steps (2023)

The average worker spends at least 3 hours a week in meetings, and 30% of all workers report 5 hours or more! Meetings are a necessity in any organization, but they can also be time-consuming and unproductive if not prepared properly efficiently. This is why a tool like Google Calendar has become so important in today's business environment to schedule and manage meetings.

In this article, we'll learn how to use Google Calendar's API to take things a level higher with custom features for your business.

Google Calendar Limitations

There are 3 main use cases for using Google Calendar's API:

  • Custom UI - Google Calendar offers limited customization options. If you want a more custom UI, using the API allows you to leverage all sorts of data visualization libraries and charts to fit your workflows.
  • Collaborative features - Even though you can share events, tasks and calendars, not everyone has access to your Google Workspace features and groups. For more complex workflows requiring fine-grained accesses, you'll need custom code. For example, to invite 50 random people to a public event from a form, or to create a Calendly-like app.
  • Privacy-friendly integrations - Google Calendar integrates with plenty of third-party tools, but is it okay for them to have access to all your calendar data? With a custom API script, you can connect any software to your Google Calendar without them accessing anything.

1. Enable Google Calendar

After creating a new Rowy project, enable the Google Calendar API in your Google Cloud console:

0.webp

Because Rowy is built on top of Google Cloud Functions, you don't need to worry about setting up OAuth2 via service accounts. Rowy directly integrates with Google Auth, so you can create an API client for Google Calendar in just a few lines of code:

const calendar = require('@googleapis/calendar')

const gauth = new calendar.auth.GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/calendar']
  });
const authClient = await gauth.getClient();

const client = await calendar.calendar({
    version: 'v3',
    auth: authClient
});

The API scope /auth/calendar will allow you to have read and write accesses to all your calendars, events, and tasks.

2. Retrieve Google Events

Now that we have an API client, we can retrieve events from our calendars. The following code will retrieve the next 10 events from your primary calendar:

const events = await client.events.list({
    calendarId: 'primary',
    timeMin: new Date().toISOString(),
    maxResults: 10,
    singleEvents: true,
    orderBy: 'startTime',
}).then(res => res.data.items)

events.map((event, i) => {
    const start = event.start.dateTime || event.start.date;
    console.log(`${start} - ${event.summary}`);
})

In a new Rowy table, you can create a webhook to run this code from an API request without having to set up and maintain a web server:

1.webp

You can then use the Firebase collection reference available in all Rowy webhook to import the events you just read as new rows in your Rowy table:

ref = db.collection('events')

ref.add({
    start: event.start,
    summary: event.summary
})

3. Send Email Reminders

With Rowy, you can easily run custom scripts to call any third party API to interact with your data. One interesting use case for Google Calendar's API is to send email reminders before events happen.

One way to do this is to use the Sendgrid API to send emails programmatically. For example, you can create a Rowy Action column to send an email reminder whenever you click on the button:

It only takes a couple lines of code to send an email with Sendgrid from the Action column's settings:

const action: Action = async ({ row }) => {
    await fetch("https://api.sendgrid.com/v3/mail/send", {
        ...
    })
}

If you want to learn more about managing emails with Sendgrid, check out our Sendgrid API tutorial.

4. Add New Google Events

Naturally, you can also use the Google Calendar API to create and update events from Rowy, using another Action column for example:

var event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'lpage@example.com'},
    {'email': 'sbrin@example.com'},
  ],
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
};

await client.events.insert({
  calendarId: 'primary',
  resource: event
})

This is particularly useful to sync third-party tools with Google Calendar to centralize all your appointments in a single source of truth.

Join Rowy's Community

That's a wrap! Hope this article helped you clear out how to create and read events using Google Calendar's API.

Note that this API offers many more features than what we covered in this article: you can also manage entire calendars, access Google Meet data, or even create a Calendly clone! Feel free to check out more of our demos to get your creative juices flowing.

If you have any questions, feel free to join our community on Discord. We're happy to provide assistance with Rowy!

Get started with Rowy in minutes

Continue reading

Browse all