Set up a new Node.js project:
> mkdir vinsight-api-test
> cd vinsight-api-test
> npm init --yes
> npm install request --save
> node app.js
app.js:
var request = require("request");
var request = request.defaults({
});
var domain = "https://app.vinsight.net",
apiKey = "abcd00000000000000000000000001234";
var getAs = function (url, acceptType, callback) {
request({
uri: domain + url + ("&api-key=" + apiKey),
headers: {
'Accept': acceptType
}
}, callback);
};
getAs("/StockItems?$select=StockItemCode,UnitsInStock,UnitsInStockUOM&$top=999", "text/csv", function (err, res, body) {
if (err) {
throw new Error(err);
}
console.log("Status: ", res.statusCode);
console.log("Response Body: \n", body);
});
Deserializing JSON responses:
> mkdir vinsight-api-test
> cd vinsight-api-test
> npm init --yes
> npm install request --save
> node app.json.js
app.json.js:
var request = require("request");
require("./jsonReferences.js");
var request = request.defaults({
});
var domain = "https://app.vinsight.net",
apiKey = "abcd00000000000000000000000001234";
var getAs = function (url, acceptType, callback) {
request({
uri: domain + url + ("&api-key=" + apiKey),
headers: {
'Accept': acceptType
}
}, callback);
};
getAs("/TaxRates?$filter=TaxType/Code eq 'GST'&$top=1", "application/json", function (err, res, body) {
if (err) {
throw new Error(err);
}
console.log("Status: ", res.statusCode, "\n");
if (res.statusCode !== 200) {
return;
}
console.log("Response Body: \n", JSON.stringify(JSON.parse(body), null, 4), "\n");
if (body) {
var deserialized = JSON.parseReferences(body);
var taxRate = deserialized[0];
console.log("TaxType:", taxRate.TaxType, "\n");
console.log("Circular reference test:", taxRate.TaxType.TaxRates[0] === taxRate);
}
});