Skip to main content
Skip table of contents

Making API requests

There are two ways to make HTTP requests through Node-RED. You can use the built-in HTTP request node or you create your code to call an address with a module like Axios. The default node is easier and faster to set up. The custom option provides more flexibility in adding data to your request and gives you complete control over what should happen on success or error.

Default HTTP request node

Node-RED provides HTTP nodes to handle simple requests and responses. You can use the HTTP request function node to make requests to an API. The HTTP request node has two values you can set, the method and URL. To avoid hardcoded values in the node you can declare the properties of the request in a function or change the node. To use external values in the request you have to set the nodes method to -set by msg.method- and leave the URL empty.

As of now, the msg properties can override set values in the requesting node. This behavior is deprecated and will be gone in future versions. After that, the values can only be set if the corresponding ones in the requesting node are empty.

You can set up the request by setting following values right before the request node:

JS
msg.url = 'https://example.url'
msg.method = 'GET'
msg.headers = {
    Authorization: 'token',
    Content-Type: 'application/x-www-form-urlencoded'
    }
msg.payload = {}

Custom function with Axios

Axios is a module handling HTTP requests and is installed as a Node.js package. It can be required through a function node. More on requiring modules in Working with the function node. An Axios POST request in Node-RED would look like this:

JS
var require = global.get('require')
var axios = require('axios')

var config = {
    headers: {'Authorization': global.get('bearer')}
};

axios.post(url, payload, config)
    .then(function (response) {
        node.send({payload:response})
    })
    .catch(function(error){
        node.send({payload:error.response})
    })

The POST request is configured with the URL you want to send the request to, a payload, and additional data in the config variable. You can check out all the options Axios provides to set up additional data for a request.

To export the response to msg.payload you need to use node.send({payload:response.data}) instead of the usual return msg because Axios is an asynchronous process. Asynchronous processes will not return a message.

Using async await or promise function descriptors for the Axios function might not work.

The Axios function returns a JSON Object with the following structure:

CODE
response
├── status: 200
├── statusText: "OK"
├── headers
│   └── object
├── config
│   └── object
├── request
│   └── object
└── data
    └── object

The payload of the response can be found in the data. You can analyze the response inside the Axios function or pass the whole response object on to the next node. If you are adding functions to handle errors, the response of the error message will be accessible through error.response.

To simplify sending requests with Axios, the subflow below (axios.json) can be imported to each flow. It will make HTTP requests with definable methods and parameters.

axios.json

The following components can be imported:

JS
msg = {
   "method": <method>, //required
   "url": <url>, //required
   "headers": <headers>,
   "payload": <payload>,
   "params": <params>,
   "auth": <auth>
}

More keys can be implemented to the subflow if Axios supports them.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.