Creating custom node and the getting accesstoken and then making api call on single custom node, in which part api call should be mentioned

var axios = require("axios").default;
var qs = require("qs");

module.exports = function (RED) {

 ---------> post call to return token.   
const sendPostRequest = async (clientId, clientSecret) => {
    const data = qs.stringify({
        grant_type: 'client_credentials',
        client_id: clientId,
        client_secret: clientSecret
    const headers = {
        'content-type': 'application/x-www-form-urlencoded'
    const res = await`https://mytokenurl`, data, headers);

function GetMachineLiveSpeedNode(config) {
    RED.nodes.createNode(this, config);
    var node = this;
    let machineId="";
    let token = "";

    this.GetAuthentication = RED.nodes.getNode(config.GetAuthentication);
    this.clientId = this.GetAuthentication.clientId;
    this.clientSecret = this.GetAuthentication.clientSecret;

        sendPostRequest(this.clientId, this.clientSecret)   ----------> called when node is initial loaded in flow
        .then((response) => {
            token = response;
            console.log("token inside : "+token);
        }).catch((err) => {

    node.on('input', function (msg) {
        machineId = msg.payload.machineId;          ------------> machineId(20 plus in number ) coming from previous node

        sendPostRequest(this.clientId, this.clientSecret)   -----------> called multiple times on basis of number of machine Id passed
        .then((response) => {
            token = response;
            console.log("token inside : "+token);
        }).catch((err) => {

        let url = `https://${ISP_HOSTNAME}/someurlcontent/${machineId}/againsomeurlcontent`;
        axios.get(url, { headers: { "Authorization": `Bearer ${token}` } })
RED.nodes.registerType("GetMachineLiveSpeed", GetMachineLiveSpeedNode);


Hi All,

above is my code, it is working but not the appropriate way of coding. so please review and guide.
I am confused that where to put my api calls so as to get the perfect output.

  1. I want that I make post call to get accesstoken one time only, and then make another get call with that access token and machineId as parameter to get response for mutiple machineId passed in input event of node.
  2. And my accesstoken expires in 30 min, so want to get new access token with help of refreshtoken.

Please help me on this.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.