Somewhere to keep those API keys: google apps script rest library entry

In the builtwith.com api rest library entry for Excel I covered how to get data about a given web site using the builtwith.com API. I went against my usual rule here, by including an api that needs an API Key. Here’s how to securely store your API keys in one place, and use them without exposing them in your code. This is a subset of the topic, using scriptDB as a lockbox, and before it will work, you would need to follow the instructions there for publishing a scriptDB lockbox.

Assuming you’ve done that, here is the Google Apps Script code for getting your api key out, and running the rest query. You would just need to substitute mcpher.getMyStuff() with yourlibraryname.getMyStuff().

function getApiKey(api) {
  // my private store
  var myKey = mcpher.getMyStuff(api, myStuff.myStuffDb());
  if (myKey.status.code == "good") {
      return myKey.result.myStuff.consumerKey;
  }
  else {
    mcpher.MsgBox("unable to find your key for " + api);
    return null;
  }
}

 

function testBuiltWith() {
  // get key from my private store
  var myKey = getApiKey("builtwithapi");
  if (myKey) {
     mcpher.generalQuery ("builtwith", "builtwith",
         mcpher.URLEncode(mcpher.InputBox("Enter web site name")),
         undefined, undefined , myKey);
  }
}

You would, of course had to have stored your own api key once off, with a function like this (again You substituting mcpher.getMyStuff() with yourlibraryname.getMyStuff() ) .

function createBuiltWithApiKey(){
  return mcpher.createStuff ("builtwithapi",  myStuff.myStuffDb(),
     {
      consumerKey: 'whatever your api key is',
    } );
}

 

Here’s the final result (run on the ibm.com website)

The rest library entry is

   w = cj.add("builtwith");
      w.add ("restType", ERRESTTYPE.erSingleQuery);
      w.add ("url", "http://api.builtwith.com/api.json?lookup=");
      w.add ("results", "Technologies");
      w.add ("treeSearch", false);
      w.add ("ignore");
      w.add ("append","&key=");

 

Next post, I’ll show you how to get the same api Key into Excel, so you won’t need to hard code it into your VBA module.

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.