Skip to content
code google google workspace apps script google script run doget dopost apps script web app

google.script.run vs. doGet/doPost

By , Google Workspace alum Published 2 min read

Last verified in 2024. APIs or CLI flags may have changed; browse newer Apps Script guides.

When building a Google Apps Script web app, you have two primary options for making API calls from the frontend to the backend: google.script.run and GET/POST endpoints (doGet and doPost). The right choice depends on how complex your web app is and whether you use an external frontend framework.

google.script.run

google.script.run works well for simple Google Apps Script web apps. It calls server-side functions asynchronously from client-side code.

function readData() { // This could be a call to Google Sheets return { data: Session.getActiveUser().getEmail() }; }
google.script.run .withSuccessHandler((result) => console.log(result)) .withFailureHandler((error) => console.error(error)) .readData();

You can use the poorly named withUserObject method to pass extra context to the callback function, such as the HTMLElement that triggered the call. It is a helper to avoid complex closures.

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). MDN

google.script.run
  .withSuccessHandler((result, userObject) =>
    console.log({ result, userObject }),
  )
  .withUserObject(this)
  .readData();

doGet/doPost endpoints

doGet and doPost endpoints are more flexible. You can call them from any web framework or external application, though you have to write your own routing logic inside those two entry points.

function doGet(e) {
  if (e.parameter.action === "read") {
    return ContentService.createTextOutput(
      JSON.stringify(readData()),
    ).setMimeType(ContentService.MimeType.JSON);
  } else {
    // return the HTML file, index.html in this case
    return HtmlService.createHtmlOutputFromFile("index");
  }
}

And then to call the endpoint from the frontend:

fetch("https://script.google.com/macros/s/SOME_ID/exec?action=read") .then((response) => response.json()) .then((data) => console.log(data));

If you inspect the network tab in your browser, you will see the request will be 302 redirected to a URL similar to https://script.googleusercontent.com/macros/echo?user_content_key=A3V4uWwF.... The fetch call will follow this redirect automatically by default.

You will still need to access these endpoints from the Apps Script Web app if you want to make authenticated calls from the doGet or doPost functions to Google services such as Sheets.

Decision factors

google.script.rundoGet and doPost
Best forSimple Google Apps Script Web appWeb framework served as Google Apps Script Web app
CommunicationDirectREST API calls
Function CallsFunction specified by nameRequires logic routing
AsynchronousYes (Callbacks)Yes (Promises)
TestingMore challenging but still easy enoughEasier mocking
File DownloadsNoYes
AccessOnly Apps Script Web appsAny app (unless accessing Google services from function)
Error HandlingLimitedFull control
  • If you’re building an Apps Script Web app, google.script.run is the simplest choice.
  • If you are building an Apps Script Web app using a framework like React, Angular, or Vue, consider using GET/POST endpoints for compatibility. (You are probably using something like clasp to manage your Apps Script project in this case.)
  • You can only have a single doGet and doPost function in a Google Apps Script project. You will need a mechanism to route requests to the appropriate function. google.script.run does this automatically.
  • doGet and doPost endpoints require you to build the response using the ContentService. This is a benefit if you want to control the response format and possibly have the browser downloadAsFile the response.
  • If you need to test your frontend, consider using doGet and doPost endpoints. You can then mock the API calls in your tests.

Frequently asked questions

When should I use google.script.run?

I use google.script.run for simple Apps Script web apps where I need direct, asynchronous calls to my server-side functions. It's the easiest way to get things working for basic interactions.

When should I use doGet and doPost endpoints?

I opt for doGet and doPost when building more complex web apps with frameworks like React, Angular, or Vue. They give me full control over the response, like for file downloads, and robust error handling.

What is the difference between google.script.run and doGet/doPost?

google.script.run is direct, calling specific server functions. doGet/doPost are more like a REST API, requiring me to handle routing. google.script.run is simpler for basic apps, but doGet/doPost offer more flexibility, especially with web frameworks.

Can I download files using google.script.run?

No, google.script.run doesn't support file downloads directly. For that, I use doGet/doPost endpoints and use ContentService to generate the file for download.

How do I handle errors with google.script.run?

google.script.run offers a .withFailureHandler() for errors. But I get much more granular control over error handling and HTTP status codes when I use doGet/doPost.

Can I use google.script.run with frameworks like React or Vue?

You can, but I generally don't recommend it for serious projects. For web frameworks like React or Vue, I find it much cleaner to use doGet/doPost to build a standard REST API.