google.script.run vs. doGet/doPost
By Justin Poehnelt, 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.run | doGet and doPost | |
|---|---|---|
| Best for | Simple Google Apps Script Web app | Web framework served as Google Apps Script Web app |
| Communication | Direct | REST API calls |
| Function Calls | Function specified by name | Requires logic routing |
| Asynchronous | Yes (Callbacks) | Yes (Promises) |
| Testing | More challenging but still easy enough | Easier mocking |
| File Downloads | No | Yes |
| Access | Only Apps Script Web apps | Any app (unless accessing Google services from function) |
| Error Handling | Limited | Full control |
- If you’re building an Apps Script Web app,
google.script.runis 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
doGetanddoPostfunction in a Google Apps Script project. You will need a mechanism to route requests to the appropriate function.google.script.rundoes this automatically. doGetanddoPostendpoints require you to build the response using theContentService. This is a benefit if you want to control the response format and possibly have the browserdownloadAsFilethe response.- If you need to test your frontend, consider using
doGetanddoPostendpoints. You can then mock the API calls in your tests.
Documentation links
Frequently asked questions
- When should I use google.script.run?
I use
google.script.runfor 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
doGetanddoPostwhen 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.runanddoGet/doPost? google.script.runis direct, calling specific server functions.doGet/doPostare more like a REST API, requiring me to handle routing.google.script.runis simpler for basic apps, butdoGet/doPostoffer more flexibility, especially with web frameworks.- Can I download files using
google.script.run? No,
google.script.rundoesn't support file downloads directly. For that, I usedoGet/doPostendpoints and useContentServiceto generate the file for download.- How do I handle errors with
google.script.run? google.script.runoffers a.withFailureHandler()for errors. But I get much more granular control over error handling and HTTP status codes when I usedoGet/doPost.- Can I use
google.script.runwith 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/doPostto build a standard REST API.