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 Gmail API.

Prerequisites

To run this quickstart, you need the following prerequisites:

  • A Google account with Gmail enabled
  • * Access to Google Drive

    Step 1: Create the script

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

    gmail/quickstart/quickstart.gs
    /**
     * Lists the labels in the user's account.
     */
    function listLabels() {
      var response = Gmail.Users.Labels.list('me');
      if (response.labels.length == 0) {
        Logger.log('No labels found.');
      } else {
        Logger.log('Labels:');
        for (var i = 0; i < response.labels.length; i++) {
          var label = response.labels[i];
          Logger.log('- %s', label.name);
        }
      }
    }

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

    Step 2: Turn on the Gmail API

    1. In the Apps Script editor, click Resources > Advanced Google Services.
    2. Locate Gmail API in the dialog and click the corresponding toggle, setting it to on.
    3. Click the Google API Console link.
    4. Enter "Gmail API" into the search box and click on the corresponding entry in the results.
    5. Click the Enable API button.
    6. Return to the Apps Script editor and click the OK button on the Advanced Google Services dialog.

    Step 3: Run the sample

    In the Apps Script editor, click Run > listLabels.

    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

    Troubleshooting

    "This app isn't verified" Dialog

    Follow these steps if you are presented with a dialog that states "This app isn't verified":

    1. Click the Advanced link.
    2. Click the Go to {Your Project Name} button.
    3. Click the Allow button.

    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.

    Send feedback about...

    Need help? Visit our support page.