Build a Google Workspace Add-on with Node.js

Create a Google Workspace Add-ons in Cloud Functions using the Node.js runtime.

Objectives

  • Set up your environment.
  • Create and deploy a Cloud Function.
  • Create and deploy the add-on.
  • Install the add-on.

Prerequisites

Set up your environment

Open your Cloud project in the Google Cloud console

  1. In the Google Cloud console, go to the Select a project page.

    Select a Cloud project

  2. Select the Google Cloud project you want to use. Or, click Create project and follow the on-screen instructions. If you create a Google Cloud project, you might need to turn on billing for the project.

Configure the OAuth consent screen

Google Workspace Add-ons require a consent screen configuration. Configuring your add-on's OAuth consent screen defines what Google displays to users.

  1. In the Google Cloud console, go to Menu > APIs & Services > OAuth consent screen.

    Go to OAuth consent screen

  2. For User type select Internal, then click Create.
  3. Complete the app registration form, then click Save and Continue.
  4. For now, you can skip adding scopes and click Save and Continue. In the future, when you create an app for use outside of your Google Workspace organization, you must change the User type to External, and then, add the authorization scopes that your app requires.

  5. Review your app registration summary. To make changes, click Edit. If the app registration looks OK, click Back to Dashboard.

Create and deploy a Cloud Function

  1. In a local terminal, turn on the Cloud Functions, Cloud Build, and the Google Workspace Add-ons API:

    gcloud services enable cloudfunctions cloudbuild.googleapis.com gsuiteaddons.googleapis.com
    
  2. In an empty directory, create the file function.js with the following sample code:

    /**
     * Cloud Function that loads the homepage for a
     * Google Workspace Add-on.
     *
     * @param {Object} req Request sent from Google
     * @param {Object} res Response to send back
     */
    exports.loadHomePage = function addonsHomePage (req, res) {
      res.send(createAction());
    };
    
    /** Creates a card with two widgets. */
    function createAction() {
      return {
        "action": {
          "navigations": [
            {
              "pushCard": {
                "header": {
                  "title": "Cats!"
                },
                "sections": [
                  {
                    "widgets": [
                      {
                        "textParagraph": {
                          "text": "Your random cat:"
                        }
                      },
                      {
                        "image": {
                          "imageUrl": "https://cataas.com/cat"
                         }
                      }
                    ]
                  }
                ]
              }
            }
          ]
        }
      };
    }
    
  3. Deploy the function:

    gcloud functions deploy loadHomePage --runtime nodejs12 --trigger-http
    

    If prompted, specify that you don't allow unauthenticated invocations of the function. It might take a couple minutes for the function to deploy.

Create an add-on deployment

  1. Find the service account email for the add-on:

    gcloud workspace-add-ons get-authorization
    
  2. Grant the service account the cloudfunctions.invoker role:

    gcloud functions add-iam-policy-binding loadHomePage \
        --role roles/cloudfunctions.invoker \
        --member serviceAccount:SERVICE_ACCOUNT_EMAIL
    
  3. Get the URL of the deployed function. To get the URL, run the following command and look for the url field under the httpsTrigger section:

    gcloud functions describe loadHomePage
    
  4. Create the file deployment.json with the following sample code. Replace URL with the URL of the deployed function from the previous step.

    {
      "oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.execute"],
      "addOns": {
        "common": {
          "name": "My HTTP Add-on",
          "logoUrl": "https://raw.githubusercontent.com/webdog/octicons-png/main/black/beaker.png",
          "homepageTrigger": {
            "runFunction": "URL"
          }
        },
        "gmail": {},
        "drive": {},
        "calendar": {},
        "docs": {},
        "sheets": {},
        "slides": {}
      }
    }
    
  5. Create the deployment:

    gcloud workspace-add-ons deployments create quickstart \
        --deployment-file=deployment.json
    

Install the add-on

  1. Install the deployment in development mode:

    gcloud workspace-add-ons deployments install quickstart
    
  2. Open or reload Gmail to view the add-on. In the toolbar on the right, look for a beaker icon.

  3. Click the icon to open the add-on. If prompted, authorize the add-on.

Optional: Clean up

To avoid incurring charges to your account, delete the resources that you created:

  1. Uninstall the add-on from your Google Account:

    gcloud workspace-add-ons deployments uninstall quickstart
    
  2. To avoid incurring charges for the resources used in this quickstart, delete the Cloud project:

    gcloud projects delete PROJECT_ID
    

    Replace PROJECT_ID with the ID of the Cloud project that you used for the quickstart. You can find the Cloud project ID in the Google Cloud console on the Dashboard page.

Next steps

To add more functionality to your Google Workspace Add-on, refer to the following guides: