Google Apps Script quickstart

Create a Google Apps Script that makes requests to the Gmail API.

Quickstarts explain how to set up and run an app that calls a Google Workspace API. This quickstart uses a simplified authentication approach that is appropriate for a testing environment. For a production environment, we recommend learning about authentication and authorization before choosing the access credentials that are appropriate for your app.

In Apps Script, Google Workspace quickstarts use Advanced Google services to call Google Workspace APIs and handle some details of the authentication and authorization flow.

Objectives

  • Configure the environment.
  • Create and configure the script.
  • Run the script.

Prerequisites

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

Create the script

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

gmail/quickstart/quickstart.gs
 /** 
 * Lists all labels in the user's mailbox 
 * @see https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list 
 */ 
 function 
  
 listLabels 
 () 
  
 { 
  
 try 
  
 { 
  
 // Gmail.Users.Labels.list() API returns the list of all Labels in user's mailbox 
  
 const 
  
 response 
  
 = 
  
 Gmail 
 . 
 Users 
 . 
 Labels 
 . 
 list 
 ( 
 'me' 
 ); 
  
 if 
  
 ( 
 ! 
 response 
  
 || 
  
 response 
 . 
 labels 
 . 
 length 
  
 === 
  
 0 
 ) 
  
 { 
  
 // TODO (developer) - No labels are returned from the response 
  
 console 
 . 
 log 
 ( 
 'No labels found.' 
 ); 
  
 return 
 ; 
  
 } 
  
 // Print the Labels that are available. 
  
 console 
 . 
 log 
 ( 
 'Labels:' 
 ); 
  
 for 
  
 ( 
 const 
  
 label 
  
 of 
  
 response 
 . 
 labels 
  
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
 '- %s' 
 , 
  
 label 
 . 
 name 
 ); 
  
 } 
  
 } 
  
 catch 
  
 ( 
 err 
 ) 
  
 { 
  
 // TODO (developer) - Handle exception on Labels.list() API 
  
 console 
 . 
 log 
 ( 
 'Labels.list() API failed with error %s' 
 , 
  
 err 
 . 
 toString 
 ()); 
  
 } 
 } 
  1. Click Save.
  2. Click Untitled project , type Quickstart , and click Rename .

Configure the script

Enable the Gmail API

Open the Apps Script project.

  1. Click Editor .
  2. Next to Services, click Add a service .
  3. Select Gmail API and click Add.

Run the sample

In the Apps Script editor, click Run.

The first time you run the sample, it prompts 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.

Next steps

Design a Mobile Site
View Site in Mobile | Classic
Share by: