Home » Vinsight API  »  Client Code Examples  »  Node.js Example

Node.js Example

Set up a new Node.js project:

//Create a folder (e.g. vinsight-api-test) and use windows cmd line to cd into it.
> mkdir vinsight-api-test
> cd vinsight-api-test

//Create a new NodeJS project. (Assumes you have installed NodeJS)
> npm init --yes

//Copy the contents of the JS code and save it to app.js in your new folder

//Install required module to project
> npm install request --save

//Run the downloaded file
> node app.js

app.js:


//UNCOMMENT THIS LINE TO PROXY THROUGH FIDDLER
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; // Ignore 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' authorization error


var request = require("request");

//Enable storing of cookies
var request = request.defaults({
    //proxy: "http://127.0.0.1:8888" //UNCOMMENT THIS LINE TO PROXY THROUGH FIDDLER
});

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:



/*

The Vinsight API allows circular references within JSON response data.
The following is an example of how to use our custom deserialization library, jsonReferences.js, to deserialize these types of responses.

*/


//Create a folder (e.g. vinsight-api-test) and use windows cmd line to cd into it.
> mkdir vinsight-api-test
> cd vinsight-api-test

//Create a new NodeJS project. (Assumes you have installed NodeJS)
> npm init --yes

//Copy the contents of the JS code below and save it to app.json.js in your new folder
//Save the latest version of jsonReferences.js in to the same folder


//Install required module to project
> npm install request --save

//Run the downloaded file
> node app.json.js

app.json.js:


//UNCOMMENT THIS LINE TO PROXY THROUGH FIDDLER
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; // Ignore 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' authorization error


var request = require("request");
require("./jsonReferences.js");

//Enable storing of cookies
var request = request.defaults({
    //proxy: "http://127.0.0.1:8888" //UNCOMMENT THIS LINE TO PROXY THROUGH FIDDLER
});

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);
    }
});