Call API from Javascript

Is it possible to call the appsheet api from javascript? Are there any working examples of this? Thank you!

1 5 1,073
5 REPLIES 5

@David_Joyce
For sure itโ€™s possible provided you pass the correct parameters for your HTTP Request call. Provided you can explain what you are trying to achieve from what kinda resource, we can help more.

@David_Joyce - do not fear when @LeventK is near - he is the Appsheet API Guru

This is the (google apps script) code Im trying to run:

function TestAPI() {

var apiUrl = 'https://api.appsheet.com/api/v1/apps/{AppID}/tables/AddressList/Action';

var payload = {
    "Action": "Find",
    "Properties": {
       "Locale": "en-US",
       "Location": "47.623098, -122.330184",
       "Timezone": "Pacific Standard Time"
    },
    "Rows": [ ]
    };

var payloadStr = JSON.stringify(payload);

var options = {
  'headers' : {'ApplicationAccessKey' : '<ApplicationAccessKey>',
               'Accept' : 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
               'Authorization': '<Authorization>' },

  'method' : 'POST',
  'muteHttpExceptions': false,
  'contentType': 'application/json',

  'Action': 'Find',
  'Properties': {
      'Locale': 'en-US',
      'Location': '47.623098, -122.330184',
      'Timezone': 'Pacific Standard Time'
  },
  'Rows': [ ]

};
var optionsStr = JSON.stringify(options);
console.log(optionsStr);

var response = UrlFetchApp.fetch(apiUrl, options);

console.log(response);

}

This is the message I get in StackDriver logging:

{"HasWarning":false,"ReturnedFromCache":false,"DisconnectDetected":false,"RowValues":null,"Success":false,"ErrorDescription":"REST API REST API invoke request failed: The HTTP Body which should contain the API Action, Properties, and Row data is missing.","Timestamp":"2019-08-18T09:36:23.0481327Z","BackendVersion":5.1,"RequiredIOSAppVersion":4.3,"RequiredAndroidAppVersion":3.0}

This is the JSON body I am using:

{"headers":{"ApplicationAccessKey":"<ApplicationAccessKey>","Accept":"application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8","Authorization":"<Authorization>"},"method":"POST","muteHttpExceptions":false,"contentType":"application/json","Action":"Find","Properties":{"Locale":"en-US","Location":"47.623098, -122.330184","Timezone":"Pacific Standard Time"},"Rows":[]}ntentType=application/json}

What is wrong?
EDIT:
I figured out that it was an issue with the json payload. It has to be โ€œstringifyedโ€ before the api call.

@David_Joyce
You donโ€™t need to put your payload inside the options actually. Below function is more appropriate to run:

function TestAPI() {

	var apiUrl = 'https://api.appsheet.com/api/v1/apps/{AppID}/tables/AddressList/Action';

	var payload = {
		"Action": "Find",
		"Properties": {
		   "Locale": "en-US",
		   "Location": "47.623098, -122.330184",
		   "Timezone": "Pacific Standard Time"
		},
		"Rows": []
	};

	var options = {
		'headers' : {'ApplicationAccessKey' : '<ApplicationAccessKey>',
				   'Accept' : 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
				   'Authorization': '<Authorization>'
		},

		'method' : 'POST',
		'muteHttpExceptions': false,
		'contentType': 'application/json',
		'payload' = JSON.stringify(payload);
		
	};

	console.log(JSON.stringify(options));

	var response = UrlFetchApp.fetch(apiUrl, options);

	console.log(response);
}

Writing my first AppSheet API call in AppScript. It should read the contents of an AppSheet table and store it to a row/column array as getValues() would, or to records of key:value pairs.

The above is the only sample code I've found. Is it still current?

Is there sample code posted online to populate Authorization?

  'Authorization': '<Authorization>'

 Much thanks in advance for your advice / assistance.

Top Labels in this Space