r/GoogleAppsScript • • Jun 07 '25

Question Deploy Apps Script as API executable for only certain functions

I have a project that I want to make API executable, but I dont want any function to run. I would prefer certain functions be entrypoints that I define. Is this possible?

5 Upvotes

10 comments sorted by

3

u/maxloroll Jun 07 '25

Something like this?

function apiEntry(params) {
  const { method, data } = params;

  const methods = {
    getSummary: () => getSummary(data.id),
    updateRow: () => updateRow(data.rowId, data.values)
  };

  if (!methods[method]) {
    throw new Error("Invalid method");
  }

  return methods[method]();
}

function getSummary(id) {
  // private logic
}

function updateRow(rowId, values) {
  // private logic
}

1

u/nemcrunchers Jun 07 '25

Sure but those other methods can still be called via API if I do this.

1

u/maxloroll Jun 07 '25

yes, since Apps Script doesn’t support something like /private or /internal annotations for Execution API access.

// Single public entry
function apiRouter(params) {
  const { method, data, apiKey } = params;

  // Optional: check a shared secret or API key
  if (apiKey !== 'expected_key') {
    throw new Error('Unauthorized');
  }

  // Routing whitelist
  const routes = {
    getUser: () => getUser(data.id),
    updateName: () => updateName(data.id, data.name)
  };

  if (!(method in routes)) {
    throw new Error('Method not allowed');
  }

  return routes[method]();
}

// Not intended to be called directly
function getUser(id) {
  return { id, name: 'Test' };
}
function updateName(id, name) {
  return `Updated ${id} to ${name}`;
}

Even though getUser() and updateName() are globally visible, you discourage or block calling them by not documenting them and requiring a token in apiRouter.

4

u/nemcrunchers Jun 08 '25

Hmmm. I did find that putting an underscore after the function name makes them "private" and not callable via API. This does make it better but I was still looking for an allowlisting, so I didn't have to keep naming them this way but maintain a list of allowed functions instead

→ More replies (1)

1

u/Vegetable-Two-4644 Jun 07 '25

You could try a wrapper function to run it all through.

1

u/nemcrunchers Jun 07 '25

What I mean is, I'd like to only expose one function. Seems if I deploy the script as an API I will make it possible for any of the functions to be called via API

1

u/ShivKaushal Jun 08 '25

1

u/nemcrunchers Jun 11 '25

This is what we ended up doing. Private functions for everything so that client can only execute on functions we explicitly allow. We also used eslint to force this underscore to be added for all new functions in our repo