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:
1
{
2
"method": "GET",
3
"path": "/users"
4
}
Copied!
Other parameters (like payload for POST and PUT requests) are provided as additional fields in the request body:
1
{
2
"method": "PUT",
3
"path": "/users/4",
4
"payload": {
5
"firstname": "John",
6
"lastname": "Smith"
7
}
8
}
9
Copied!
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.
1
// jQuery
2
const { data } = await $.post({
3
url: apiUrl,
4
data: JSON.stringify({
5
method: "GET",
6
sheet: "users"
7
}),
8
});
9
10
// Axios
11
const { data } = await axios.post(
12
apiUrl,
13
JSON.stringify({
14
method: "GET",
15
sheet: "users"
16
}),
17
{ headers: { "Content-Type": "application/x-www-form-urlencoded" } }
18
);
Copied!
Last modified 1yr ago