Add-in Expert logo
  • About
  • Portfolio
  • Blog
  • FAQs
Book a CallGet in Touch
Add-in Expert logo
  • About
  • Portfolio
  • Blog
  • FAQs
Book a CallGet in Touch
Add-in Expert logo
  • About
  • Portfolio
  • Blog
  • FAQs
Book a CallGet in Touch
  1. Home
  2. Blog
  3. Google Workspace
  4. Build a Gmail Add-on with Apps Script: A Step-by-Step Guide
Google Workspace

Build a Gmail Add-on with Apps Script: A Step-by-Step Guide

Learn how to build a Gmail add-on with Google Apps Script — from the manifest and CardService UI to reading the open email, calling external APIs, and publishing.

MGBy M Gulab Yar
|April 15, 2025|8 min read
Share:
Build a Gmail Add-on with Apps Script: A Step-by-Step Guide

Introduction

Gmail add-ons put your tools right where people already work — in the sidebar next to the email they’re reading. They can pull up CRM context, create tickets, translate messages, or log activity, all without the user leaving their inbox. And with Google Apps Script, you can build one without provisioning a single server.

In this guide we’ll build a Gmail add-on from scratch with Apps Script: the manifest that registers it, a card-based UI, reading the currently open message, handling button actions, and finally deploying it. No prior add-on experience needed.

What you’ll need

Just a Google account and basic JavaScript. There’s no server to set up and no billing to enable — Apps Script hosts and runs everything for you.

What Is a Gmail Add-on?

A Gmail add-on is a Google Workspace add-on that appears in the right-hand sidebar of Gmail on web and mobile. It’s built entirely with Apps Script and Google’s CardService, which renders a card-based UI Google displays for you — so you describe the interface in code and Google handles the rendering across every client.

Because the whole thing runs on Apps Script, there are no servers to host or scale. Google runs your code, manages authentication, and shows the cards — you just write the logic.

Set Up the Apps Script Project

Head to script.google.com and create a new project. You can work in the browser editor or, for version control, develop locally with clasp — the command-line tool for Apps Script.

bash
npm install -g @google/clasp
clasp login
clasp create --type standalone --title "Gmail Add-on"

This gives you a project with a Code.gs file and an appsscript.json manifest — the two files that define your add-on.

Configure the Manifest

The appsscript.json manifest registers your add-on with Gmail: which function draws the homepage, and which draws the card when a message is open. This is the piece that turns plain Apps Script into a Gmail add-on.

appsscript.json
{
  "oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly"
  ],
  "addOns": {
    "common": {
      "name": "Gmail Add-on",
      "logoUrl": "https://www.gstatic.com/images/branding/product/1x/apps_script_48dp.png",
      "homepageTrigger": { "runFunction": "onHomepage" }
    },
    "gmail": {
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "onGmailMessage"
        }
      ]
    }
  }
}

Build the Homepage Card

The homepage trigger runs when the user opens your add-on without a message selected. It returns a Card built with CardService — the building block for every screen in your add-on.

Code.gs
function onHomepage(e) {
  const section = CardService.newCardSection()
    .addWidget(
      CardService.newTextParagraph()
        .setText('Open an email to see contextual actions.')
    );

  return CardService.newCardBuilder()
    .setHeader(CardService.newCardHeader().setTitle('Gmail Add-on'))
    .addSection(section)
    .build();
}

Read the Open Email

The contextual trigger fires when a message is open. Google passes the message id in the event; use it to load the message with GmailApp and build a card from its contents.

Code.gs
function onGmailMessage(e) {
  const messageId = e.gmail.messageId;
  const accessToken = e.gmail.accessToken;
  GmailApp.setCurrentMessageAccessToken(accessToken);

  const message = GmailApp.getMessageById(messageId);
  const subject = message.getSubject();
  const from = message.getFrom();

  const section = CardService.newCardSection()
    .addWidget(CardService.newKeyValue().setTopLabel('From').setContent(from))
    .addWidget(CardService.newKeyValue().setTopLabel('Subject').setContent(subject))
    .addWidget(
      CardService.newTextButton()
        .setText('Log this email')
        .setOnClickAction(
          CardService.newAction().setFunctionName('onLogEmail')
        )
    );

  return CardService.newCardBuilder()
    .setHeader(CardService.newCardHeader().setTitle('Email Details'))
    .addSection(section)
    .build();
}

Set the message access token

Calling setCurrentMessageAccessToken with the token from the event is what grants your add-on temporary access to the open message under the current-message-readonly scope.

Handle Actions & Call APIs

When the user taps a button, the function named in its action runs. That handler can call an external API with UrlFetchApp — for example to log the email into a CRM — then return a notification to confirm it worked.

Code.gs
function onLogEmail(e) {
  const messageId = e.gmail.messageId;
  GmailApp.setCurrentMessageAccessToken(e.gmail.accessToken);
  const message = GmailApp.getMessageById(messageId);

  UrlFetchApp.fetch('https://your-backend.com/log-email', {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      subject: message.getSubject(),
      from: message.getFrom(),
    }),
    muteHttpExceptions: true,
  });

  return CardService.newActionResponseBuilder()
    .setNotification(
      CardService.newNotification().setText('Email logged \u2713')
    )
    .build();
}

External calls need a scope

Any UrlFetchApp call to a non-Google URL requires the script.external_request scope in your manifest. Add it, or the request throws an authorization error.

Deploy & Publish

To try your add-on, use a test deployment; to share it, publish through the Marketplace.

  1. In the Apps Script editor, choose Deploy → Test deployments → Install to load it into your own Gmail.
  2. Open Gmail, refresh, and find your add-on in the right-hand sidebar.
  3. When it’s ready, configure the Google Workspace Marketplace SDK for the project.
  4. Complete the store listing and submit for OAuth verification if you use sensitive scopes.
  5. Publish privately to your Workspace domain, or publicly to the Marketplace.

Security & Best Practices

  • Request the narrowest OAuth scopes your add-on needs — sensitive scopes trigger Google verification.
  • Prefer current-message scopes over full-mailbox access whenever possible.
  • Keep any secrets or API keys on your own backend, not in the Apps Script project.
  • Add muteHttpExceptions and check response codes so a failed call fails gracefully.
  • Publish a clear privacy policy — it’s required for Marketplace listing and OAuth verification.

Conclusion

Apps Script makes Gmail add-ons remarkably approachable: a manifest, a few card-building functions, and an action handler are enough to ship something genuinely useful — with Google hosting all of it. Start with a homepage card and one contextual action, get it working end to end, then layer on external integrations and richer UI. When you’re ready to publish a polished, secure add-on for your team or the Marketplace, we’d be glad to help.

#Gmail#Apps Script#Google Workspace#Add-ons#CardService#Tutorial
PreviousGoogle Sheets Custom Functions with Apps Script: A Complete GuideNext Security Best Practices for Office Add-ins Development

Related Articles

Getting Started with Google Apps Script for Workspace Add-onsGoogle WorkspaceGetting Started with Google Apps Script for Workspace Add-onsSeptember 5, 2024 · 7 min readGoogle Sheets Custom Functions with Apps Script: A Complete GuideGoogle WorkspaceGoogle Sheets Custom Functions with Apps Script: A Complete GuideMay 5, 2025 · 8 min readAutomate Google Docs Report Generation with Apps ScriptGoogle WorkspaceAutomate Google Docs Report Generation with Apps ScriptMay 15, 2025 · 8 min read

Table of Contents

  1. 1.What Is a Gmail Add-on?
  2. 2.Set Up the Apps Script Project
  3. 3.Configure the Manifest
  4. 4.Build the Homepage Card
  5. 5.Read the Open Email
  6. 6.Handle Actions & Call APIs
  7. 7.Deploy & Publish
  8. 8.Security & Best Practices
  9. 9.Conclusion

Need Help Building Custom Office Add-ins?

We build secure, scalable, and user-friendly Office solutions that drive real business impact.

Contact Our Experts

Categories

  • Excel Add-ins3
  • Outlook Add-ins4
  • Word Add-ins2
  • PowerPoint Add-ins1
  • Google Workspace5
  • Best Practices6
View all categories

Popular Posts

  • How to Build an Excel Add-in That Stands OutHow to Build an Excel Add-in That Stands OutApr 10, 2024
  • Top 10 Outlook Add-ins Use Cases for BusinessesTop 10 Outlook Add-ins Use Cases for BusinessesMar 28, 2024
  • Google Workspace Add-ons vs Macros: What to Choose?Google Workspace Add-ons vs Macros: What to Choose?Mar 15, 2024
View all posts

Stay Updated

Subscribe to get the latest tutorials, insights, and product updates.

No spam. Unsubscribe anytime.

Add-in Expert logo
Addin Expert specializes in developing custom Office add-ins and Google add-ons, empowering businesses to enhance productivity and streamline workflows with tailored, innovative solutions

Office 365 Add-ins

  • Outlook add-ins
  • Word add-ins
  • Excel add-ins
  • Powerpoint add-ins

Google addons

  • Gmail Addon
  • Google Docs Addon
  • Google Sheets Addon
  • Google forms Addon

More Services

  • VSTO Add-ins
  • VSTO Migration
  • Cross-Platform
  • Deployment

Company

  • About Us
  • Portfolio
  • Blog
  • Privacy Policy
Copyright © 2026. All rights reserved by Addin Expert.