Google Apps Script Quickstart

Complete the steps described in the rest of this page to create a simple Google Apps Script that makes requests to the Google Calendar API.


To run this quickstart, you need the following prerequisites:

  • A Google account with Google Calendar enabled.

  • Access to Google Drive

Step 1: Create the script

  1. Create a new script by going to
  2. Replace the contents of the script editor with the following code:

 * Lists 10 upcoming events in the user's calendar.
 * @see
function listUpcomingEvents() {
  const calendarId = 'primary';
  // Add query parameters in optionalArgs
  const optionalArgs = {
    timeMin: (new Date()).toISOString(),
    showDeleted: false,
    singleEvents: true,
    maxResults: 10,
    orderBy: 'startTime'
    // use other optional query parameter here as needed.
  try {
    // call Events.list method to list the calendar events using calendarId optional query parameter
    const response = Calendar.Events.list(calendarId, optionalArgs);
    const events = response.items;
    if (events.length === 0) {
      Logger.log('No upcoming events found');
    // Print the calendar events
    for (const event of events) {
      let when = event.start.dateTime;
      if (!when) {
        when =;
      Logger.log('%s (%s)', event.summary, when);
  } catch (err) {
    // TODO (developer) - Handle exception from Calendar API
    Logger.log('Failed with error %s', err.message);

New editor

  1. Click Save .
  2. Click Untitled project at the top left, type Quickstart, and click Rename.

Legacy editor

  1. Click File > Save, name your project “Quickstart”, and click OK.

Step 2: Turn on the Google Calendar API

Enable the Calendar API advanced service in your script.

Step 3: Run the sample

New editor

In the Apps Script editor, click Run.

The first time you run the sample, it will prompt you to authorize access:

  1. Click Review permissions.
  2. Choose an account.
  3. Click Allow.

The script's execution log appears at the bottom of the window.

Legacy editor

In the Apps Script editor, click Run > listUpcomingEvents.

The first time you run the sample, it will prompt you to authorize access:

  1. Click the Continue button.
  2. Click the Accept button.

To view the script's output, click View > Logs.

Further reading


This section describes some common issues that you may encounter while attempting to run this quickstart and suggests possible solutions.

ReferenceError: "[API NAME]" is not defined

This error occurs when the API hasn't been toggled on in the Apps Script code editor. Revisit Step 2.b and ensure the corresponding toggle is set to on.

This app isn't verified

If the OAuth consent screen displays the warning "This app isn't verified," your app is requesting scopes that provide access to sensitive user data. If your application uses sensitive scopes, your app must go through the verification process to remove that warning and other limitations. During the development phase, you can continue past this warning by selecting Advanced > Go to {Project Name} (unsafe).