Node-RED - Access the InfluxDB API Using the HTTP Request Node
Within Node-RED you can use the InfluxDB nodes. But it is also possible to query the InfluxDB v2 API via the HTTP Request node. This note contains a brief description of how this works.
As an example I will delete a measurement via the API. The InfluxDB v2 API makes it very easy to remove a measurement. I wrote another note on how this works through the CLI without using Node-RED.
And below that is an example of a write request that can also be used for bulk processing.
Node-RED Flow Setup
- First create a new flow within Node-RED. In the menu choose
Flows
and thenAdd
- Go to the menu again and choose
Import
. Then theImport nodes
screen opens. Paste the JSON below into the text field and clickImport
:[{"id":"6b0580dde8c9a5f5","type":"inject","z":"c05125d23d84e735","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":640,"y":240,"wires":<span title='There is no note that matches this link.' class='invalid-link'> <span class='invalid-link-brackets'>[[</span> "c6a4fc2ecafa1940" <span class='invalid-link-brackets'>]]</span></span>},{"id":"c6a4fc2ecafa1940","type":"function","z":"c05125d23d84e735","name":"set payload and headers","func":"msg.payload = {\n \"start\": \"1970-01-01T00:00:00Z\",\n \"stop\": \"2025-12-31T23:59:00Z\",\n \"predicate\": \"_measurement=\\\"YOUR_MEASUREMENT\\\"\"\n}\nmsg.headers = {};\nmsg.headers['Authorization'] = 'Token YOUR_TOKEN';\nmsg.headers['Content-Type'] = 'application/json';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":240,"wires":<span title='There is no note that matches this link.' class='invalid-link'> <span class='invalid-link-brackets'>[[</span> "005b31a1e984701e" <span class='invalid-link-brackets'>]]</span></span>},{"id":"005b31a1e984701e","type":"http request","z":"c05125d23d84e735","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://YOUR_IP:YOUR_PORT/api/v2/delete?org=YOUR_ORGANIZATION&bucket=YOUR_BUCKET","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1110,"y":240,"wires":<span title='There is no note that matches this link.' class='invalid-link'> <span class='invalid-link-brackets'>[[</span> "79d522108d85555d" <span class='invalid-link-brackets'>]]</span></span>},{"id":"79d522108d85555d","type":"debug","z":"c05125d23d84e735","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1290,"y":240,"wires":[]}]
Now the following nodes are within the flow:
- Double click on the
set payload and headers
node. This function node contains the following javascript:msg.payload = { "start": "1970-01-01T00:00:00Z", "stop": "2025-12-31T23:59:00Z", "predicate": "_measurement=\"YOUR_MEASUREMENT\"" } msg.headers = {}; msg.headers['Authorization'] = 'Token YOUR_TOKEN'; msg.headers['Content-Type'] = 'application/json'; return msg;
Adjust the following:
YOUR_MEASUREMENT
*Replace with the name of the measurement you want to delete
YOUR_TOKEN
Replace with your InfluxDB token
- Click
Done
to close theEdit function node
screen - Double click on the
http request
node. Make sure the following is set:- Method:
POST
- URL:
http://YOUR_IP:YOUR_PORT/api/v2/delete?org=YOUR_ORGANIZATION&bucket=YOUR_BUCKET
- Method:
Adjust the following in the URL:
YOUR_IP
Replace with the IP address of InfluxDB. In my case this was the IP address of the Docker host
YOUR_PORT
Replace with the port used for InfluxDB. Usually this is port8086
. My own InfluxDB container runs on port3004
YOUR_ORGANIZATION
Replace with the name of your InfluxDB Organization. In my case this wasubuntuvm
YOUR_BUCKET
Replace with the name of your InfluxDB Bucket. In my case this wasstock
- Click
Done
to close theEdit http request node
screen -
Deploy
the flow and test by clicking theinject
node.
Write Example
It is also possible to write time series data in much the same way. Make sure you have followed the steps above and then do the following:
- Double click on the
set payload and headers
node and adjust:msg.payload = "YOUR_MEASUREMENT,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096"; msg.headers = {}; msg.headers['Authorization'] = 'Token YOUR_TOKEN'; msg.headers['Content-Type'] = 'text/plain; charset=utf-8'; msg.headers['Accept'] = 'application/json'; return msg;
The payload is in line protocol format this time.
Adjust the following:
YOUR_MEASUREMENT
Replace with the name of the measurement
YOUR_TOKEN
Replace with your InfluxDB token
sensor_id=TLM0201
This is an example of a tag
temperature=73.97038159354763,humidity=35.23103248356096”
These are examples of fields
- Click
Done
to close theEdit function node
screen - Double click on the
http request
node. Make sure the following is set:- Method:
POST
- URL:
http://YOUR_IP:YOUR_PORT/api/v2/write?org=YOUR_ORGANIZATION&bucket=YOUR_BUCKET&precision=ms
- Method:
You may notice that in this case the precision has been added.
Adjust the following in the URL:
YOUR_IP
Replace with the IP address of InfluxDB. In my case this was the IP address of the Docker host
YOUR_PORT
Replace with the port used for InfluxDB. Usually this is port8086
. My own InfluxDB container runs on port3004
YOUR_ORGANIZATION
Replace with the name of your InfluxDB Organization. In my case this wasubuntuvm
YOUR_BUCKET
Replace with the name of your InfluxDB Bucket. In my case this wasstock
- Click
Done
to close theEdit http request node
screen -
Deploy
the flow and test by clicking theinject
node.
Just a Tip About the InfluxDB Nodes
I did quite a bit of testing and finally made the choice not to use the InfluxDB nodes because I feel like I have more control if I call the InfluxDB API in my own way. That is a personal choice.
If you still want to use the InfluxDB nodes then it’s good to know that with the complete
node you can receive the result when the influxdb out
or influxdb batch
node is finished:
Read other notes
Tags
Notes mentioning this note
- InfluxDB 2.x - Delete Measurement Using API or Docker Container Shell
I regularly delete a ‘sandbox’ measurement with test data. It is not very complicated to delete a measurement, but it...
Comments
No comments found for this note.
Join the discussion for this note on this ticket. Comments appear on this page instantly.