Home > Software engineering >  Best Practice to Store Keys in Google Apps Script
Best Practice to Store Keys in Google Apps Script

Time:03-10

I would like to inquire for best practices in storing keys within the Google AppScript environment.

Currently, while I am prototyping, I just store the keys as local variables in the file where it is used. This key is used by all users of my App Script regardless of their domains.

Moving forward however I would like to safekeep this key in a more reliable storage. And would like to ask for advice on how to best safekeep these keys.

Currently, I am thinking of :

  1. Using PropertiesService.getUserScriptProperties().setProperty(key,value) as this is shared by all users.
  2. as part of the manifest? Is there a way to add userData in the contextual and homepage triggers?
  3. Or just keep using local variables as the code is not visible to the users anyway?

Thank you all in advance.

CodePudding user response:

As you may have noticed, the PropertiesService provides several methods for storing key/value at the document's level, user's level or script level:

I'd recommend to store a property based on who needs access to it. If only the authenticated user should have access to a property (such as for example a setting relevant only to its accounts, like it's locale language), go with the UserProperties. Contrary, if the property is relevant to a document (Google Docs, Google Sheets, etc.) go with the DocumentProperties.

With this said, I wouldn't recommend using the ScriptProperties in general. The main reason being that quota applies to the PropertiesService (see table below). This means that as your add-on gets more and more users, you will hit the quota limit quite rapidly.

| Service | Consumer accounts (e.g. @gmail.com) | Google Workspace accounts | |--|--|--| |Properties read/write | 50,000 / day | 500,000 / day | Source: https://developers.google.com/apps-script/guides/services/quotas

Depending on your use case, you might also be tempted by alternatives to the PropertiesService:

  • using local variables in your code - as you mentionned
  • using the CacheService - which store data for a limited period of time.
  • making request to a distant server where you could query your own database.

We heavily rely on the latest at my company, thanks to the UrlFetchApp service. The main reason being that it allows us to pull a user profile from our database without making updates to the codebase.

CodePudding user response:

I understand that you ask about the best way to store a static key that would be retrieved by anybody who runs your Apps Script project, being indifferent to its domain. I also assume that the script is being run as the owner (you) and that the final users shouldn't be able to read the key, please leave a comment if that isn't the case. With this situation you have the following approaches:

The straightmost approach would be to use the Properties Service. In this particular scenario the key should be accessible to anyone executing the script, therefore PropertiesService.getScriptProperties() is the way to go (you can learn more about other scenarios here).

As an alternative you could store the key in your own database and use the JDBC Service to access it. This method is compatible with Google Cloud, MySQL, Microsoft SQL Server and Oracle databases. Here you can learn more about reading from a database.

Another possible choice is the Drive API. You could take advantage of the application data folder since it is intended to store any files that the user shouldn't directly interact with. For this option you would need to use the Advanced Drive Service on Apps Script.

Please be aware that an admin from your domain could access (or gain access) to the stored key. Also please check the quota limits to see if it fits your usage.

  • Related