Usage

API design

SpreadAPI has to deal with limitations imposed by the Google Apps Script engine. There are two limitations that make running a state-of-art REST API impossible in this environment:

  1. Each script can respond only on one hardcoded URL. It can't handle request comming at subpaths like /users or /transactions/15.

  2. Only GET and POST methods are supported.

Due to the these limitations the SpreadAPI script handles only POST requests on a single URL. The actual HTTP method and resource path are provided in request body as shown in the example below:

{
"method": "GET",
"path": "/users"
}

Other parameters (like payload for POST and PUT requests) are provided as additional fields in the request body:

{
"method": "PUT",
"path": "/users/4",
"payload": {
"firstname": "John",
"lastname": "Smith"
}
}

For more information on this topic visit API Reference documentation page.

Usage in browsers

SpreadAPI can be used as a back-end service for your web applications. To successfully call the API you need to set Content-Type HTTP header to application/x-www-form-urlencoded . If you fail to do that your request will be blocked by CORS. Keep in mind that request payload should still be JSON despite the Content-Type header.

The example below shows how to successfully call SpreadAPI using jQuery and axios.

// jQuery
const { data } = await $.post({
url: apiUrl,
data: JSON.stringify({
method: "GET",
sheet: "users"
}),
});
// Axios
const { data } = await axios.post(
apiUrl,
JSON.stringify({
method: "GET",
sheet: "users"
}),
{ headers: { "Content-Type": "application/x-www-form-urlencoded" } }
);