API Help in a Google Spreadsheet

I’ve been getting the API to work in a Google Spreadsheet Script and I’ve been working down all the examples in the API documentation and so far I’ve gotten a proper response back with the info I need. The last one I got to work was /public/zone/:id. (which the documentation example was wrong, “public/device/” should be “public/zone/”)

What I can’t get to work is /public/zone/start. Any idea how I would format that to work out of Google Apps Script?

This works and I get a response back:

function ZoneInfo() {
var APIKey = XXXXX;
var ZoneID = “1b9a8312-d9b3-4ffd-a0c5-6509c2a2c3bf”
var url = “https://api.rach.io/1/public/zone/”+ZoneID;
var headers = {
“contentType”: “application/json”,
“headers”:{“Authorization”: "Bearer " + APIKey}
};

var response = UrlFetchApp.fetch(url, headers);
var data = JSON.parse(response.getContentText());
Logger.log(data);
}

Result:
[20-08-18 17:06:39:328 PDT] {customSlope={sortOrder=0.0, name=ZERO_THREE}, lastWateredDate=1.597777682E12, customNozzle={inchesPerHour=1.0, name=ROTOR_HEAD}, rootZoneDepth=6.0, id=1b9a8312-d9b3-4ffd-a0c5-6509c2a2c3bf, yardAreaSquareFeet=500.0, runtimeNoMultiplier=2239.0, availableWater=0.17000000178813934, customSoil={name=LOAMY_SAND}, runtime=2239.0, managementAllowedDepletion=0.5, scheduleDataModified=false, customCrop={name=Cool Season Grass, coefficient=0.800000011920929}, wateringAdjustmentRuntimes={2=2799.0, 3=2239.0, 4=1679.0, 5=1120.0, 1=3359.0}, imageUrl=https://prod-media-photo.rach.io/34e18bca-535d-443e-a7c4-b203ec1484cc, zoneNumber=1.0, name=Front House, saturatedDepthOfWater=0.56, fixedRuntime=0.0, enabled=true, maxRuntime=10800.0, depthOfWater=0.51, efficiency=0.699999988079071, customShade={name=LOTS_OF_SUN}}

You are making a GET request to “https://api.rach.io/1/public/zone/”+ZoneID endpoint, which correctly returns data on the zone in question. What you want is a POST request with zone id within the post data:

var APIKey = XXXXX;
var ZoneID = “1b9a8312-d9b3-4ffd-a0c5-6509c2a2c3bf”
// Make a POST request with a JSON payload.
var data = {
‘id’: ZoneID,
‘duration’: 60 // value in seconds; required
};
var options = {
‘method’ : ‘post’,
‘contentType’: ‘application/json’,
“headers”:{“Authorization”: "Bearer " + APIKey},
// Convert the JavaScript object to a JSON string.
‘payload’ : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(‘https://api.rach.io/1/public/zone/start’, options);
var data = JSON.parse(response.getContentText());
Logger.log(data);

1 Like

I knew it had to be something where Get was the default. Your example almost worked, but I had to change POST into PUT

It successfully starts the zone, but I get the error below which isn’t really that big of a deal if it already did what I wanted it to… Any idea what to do with this?

[20-08-19 09:31:44:114 CDT] SyntaxError: Unexpected end of JSON input
at StartZone(asdf:39:19)

I’ve missed this, but zone / start endpoint does not respond with any data, thus unexpected end to the json string. If successful, response will have 204 code only:

var response = …;
if ( response.getResponseCode() == 204) { Logger.log(“Success”); }
else Logger.log("Unexpected response: " + response.getResponseCode());

YOU
ARE
AWESOME

I think I have everything working properly. I’m going to tweak this here and I’ll post a full writeup of how to do this in a Google sheet like I am. Thank you so much!

2 Likes