How to pass api request via function node from node-red to home assistant

Hi,
Sorry if i double topic, but i don’t see any answer about it

i want to change the states of multiple entities via one function i wrote in a node-red’s function node

how i can do it? i tried to make this function in the function node to call the api

but i can’t use axios in node-red (maybe you tell me how?)

function ChangeEntityState(varentity_id,varstate) 
{ 
    module.exports = function(RED) {
    const axios = require('axios');
    }
	var ttsUrl = "http://127.0.0.1:8123/api/states?api_password=myapipassword"
	var varToken = "mylonglivetoken" 
	var postData = `{"entity_id": "${varentity_id}", "state": "${varstate}"}`;
	let axiosConfig = {data: null,headers: {'authorization': `Bearer ${varToken}`,'Content-Type': 'application/json',"Access-Control-Allow-Origin": "*",}};
  
	axios.post(ttsUrl, postData, axiosConfig).then((res) => {node.warn("RESPONSE RECEIVED: ", JSON.stringify(res));}).catch((err) => {node.warn("AXIOS ERROR: ", JSON.stringify(err));})
    
}

ChangeEntityState("input_boolean.h32bs2_ch32","on")
return msg;

anyway, is there a solution for calling api from inside of function node?

please, don’t tell me use api call node, i want to do it with the function node only

1 Like

https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules

It is not the correct answer, for example using Axios in code requires an asynchronous call, but in the function node, it is not working. Please add the actual Axios implementation.