How to manage Records using API: JavaScript/Node.js sample code

How to manage Records using API: JavaScript/Node.js sample code

For those who love to do things the “API way”, we have prepared a sample JavaScript/Node.js code, which guides the developer on how to add, update and delete the Records. It is a single-run JavaScript doing all three things at once.

The script should be self-explanatory. All you need to do is to update the apiUser and apiPass variables, which can be taken from the “Account” -> “Integrations” -> “AutoROICalc API” section.

It uses Axios, the promise-based HTTP client for the browser and node.js.

The script starts with defining the API URLs: to add, update and delete Records. Following is the sample Records array. The first API call is adding the sample Records. The original sample Records array is updated with the ids of the Records that were added and also the Records values are changed. Next, it updates those Records and finally, the sample Records are deleted:

// https://www.npmjs.com/package/axios
const axios = require('axios');

// API urls, user and password
const urlAdd = "https://autoroicalc.com/api/auto-roi-calc/v1/add-records";
const urlUpdate = "https://autoroicalc.com/api/auto-roi-calc/v1/update-records";
const urlDelete = "https://autoroicalc.com/api/auto-roi-calc/v1/delete-records";

const apiUser = "<YOUR_API_USERNAME>";
const apiPass = "<YOUR_API_PASSWORD>";

// sample records
let records = [
  {
    date: "2021-04-04",
    time: "16:35",
    type: "exampleType",
    activity: "closed",
    desc: "exampleDescription",
    source: ["exampleSource1", "exampleSource2"],
    value: 13.94
  },
  {
    date: "2021-04-05",
    time: "12:09",
    type: "exampleType",
    activity: "closed",
    desc: "exampleDescription",
    source: ["exampleSource1"],
    value: 20.21
  }
];

// ADD RECORDS
axios.post(urlAdd, records, {
  auth: {
    username: apiUser,
    password: apiPass
  }
}
).then(function (response) {
  // print the results from adding the records
  console.log(response);

  // collect IDs of the added records to demonstrate update
  let idsToUpdate = [];
  for (let result of response.data.results) {
    idsToUpdate.push(result.recordId);
  }

  // update the original records structure with the retrieved record ids
  // nudge the records value
  records[0].id = idsToUpdate[0];
  records[0].value = 1;
  records[1].id = idsToUpdate[1];
  records[1].value = 2;

  // UPDATE RECORDS
  axios.put(urlUpdate, records, {
    auth: {
      username: apiUser,
      password: apiPass
    }
  }
  ).then(function (response) {
    console.log(response);

    // DELETE RECORDS
    axios.delete(urlDelete, {
      data: idsToUpdate,
      headers: {
        "Authorization": 'Basic ' + Buffer.from(apiUser + ':' + apiPass).toString('base64')
      }
    }
    ).then(function (response) {
      console.log(response);
    }).catch(function (error) {
      console.log(error);
    });

  }).catch(function (error) {
    console.log(error);
  });

}).catch(function (error) {
  console.log(error);
});

Feel free to reach us at support@autoroicalc.com in case of any trouble.


Posted

in

Explore the Records-related tutorials:

Explore the Reports-related tutorials

Explore the Dashboards-related tutorials