Search code examples
google-apps-scriptgmail-apiquota

How can I tell where I am on my daily quota?


Going here: https://script.google.com/dashboard , I can see the quotas listed per API. I recently hit a limit using the following function inside the script panel of a google sheet:

function getLastEmail_(entry) {
  var email = entry[0];
  if (email) {
    var emails = [];
    var emails = GmailApp.search(email).map(function (email) {
      return getFormattedDate_(email.getLastMessageDate());
    }).sort();
    return (emails.length) ? [emails.pop()] : [""];
  }
  return [""];
}

My goal is to determine the last date of contact for an email address in a google sheet. There are 5k entries in the sheet, so I prefer to do this via script. After my testing, I got to about 380 or so lines when I got an error msg of Service invoked too many times for one day: gmail. (line 83, file "get_last_visit"). I don't see how I used 50k calls (the limit for a gsuite business account) and I can't find any usage reporting metrics anywhere.

Is there a way to:

A) increase or work-around the limit for an individual user either via subscription, payment option, unbound script, etc B) monitor the usage as it's happening to prevent unintended calls to the API that are running over the limit.

EDIT: From https://console.cloud.google.com/home/dashboard?project=project-id-xxxxxxxxxxxxxxxxxxx I am seeing "This project has no resources" under the "Resources" content block.


Solution

  • Google intends to offer a "Flexible" plan that may allow for extension of quotas but that's currently in beta.

    As for monitoring API usage you can do that via the api console. First you need to open the GCP project associated with your script by navigating to Resources > Cloud Platform Project from the Apps Script IDE. Then open the API Console for your GCP project by clicking the project id (the blue text).

    From the console open the hamburger menu (by clicking the icon made of 3 horizontal bars in the top-left corner) and navigate to API dashboard via API & Services > dashboard. Select the GMail API from the list of APIs and then select the quotas tab.

    The quotas tab should look like the following:

    enter image description here

    Clicking the download icon to the top-right of the bar-chart will download a CSV with usage metrics.

    You should also check Google's API Explorer, there might be a REST API available that allows you to fetch quota information.