Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
How to call an external API from Google Apps Script

I want to call the Steem API from inside a Google Apps Script function. I want to see the appropriate server response. But I get the following error instead.

GAS error

According to this documentation, I ran the following test.

// @see https://github.com/steemit/steem-js/tree/master/doc#browser

const getHelloWorld = () => {
  const SOURCE_CODE_URL = 'https://cdn.jsdelivr.net/npm/steem/dist/steem.min.js';
  const response = UrlFetchApp.fetch( SOURCE_CODE_URL, );
  const code = response.getContentText();
  const steem = eval( code, );

  steem.api.getAccounts([ 'ned', 'dan', ], ( error, response, ) => {
    Logger.log( error, response, );
  });
}

In my prior research, I read that this answer says:

You can do this: eval(UrlFetchApp.fetch("my url").getContentText())

How can I call the Steem API from inside Google Apps Script?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Summary from comments:

Here is a similar but different SO question about importing modules.

Summary from this article:

Using npm modules inside of Apps Script

2021-07-12

I recently was processing some data using Apps Script, and needed to parse out second-level domain info from a bunch of URLs. This is definitely not the job for regular expressions, but it's perfect for an npm module, psl, that uses the public suffix list.

But while Apps Script has come a long way, and features lots of ES2015+ goodness nowadays, it's not possible to pull in arbitrary code from npm and run it directly.

To work around this, I created the following index.js file locally, exporting the interface that I wanted to call from Apps Script:

import psl from 'psl';
import Url from 'url-parse';

export function parseHostname(sourceURL) {
  const url = new Url(sourceURL);
  return psl.parse(url.hostname);
}

Then, I installed the necessary dependencies from npm, and bundled this code up with esbuild:

npm init -y
npm install --save-dev psl url-parse punycode
npx esbuild index.js --bundle --global-name=psl --outfile=psl.js

I manually copied the contents of the psl.js file into a psl.gs file, alongside my main Code.gs file in the Apps Script editor. (This would be annoying if the bundled output changed frequently, but doing it once by hand wasn't a problem.)

Apps Script will automatically make the contents of all .gs files in a project visible in the same global scope, so I could now write code like

const {sld} = psl.parseHostname(url);

inside of my main Code.gs file.

Caveats

The Apps Script runtime environment still has a bunch of quirks when compared to Node, so don't expect all of your bundled code to work as-is. Polyfills may be needed (I used url-parse, for instance, since the native URL object isn't available in Apps Script.)

Once you copy over the bundled code to Apps Script, it's never going to be updated, so make sure you're prepared to rebundle if and when there are any security or feature updates to the modules you're using.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!