Skip to content

How to access the InfluxDB API using the Node-RED request node

Introduction

With Node-RED, you can use the InfluxDB nodes. However, it’s also possible to query the InfluxDB v2 API using the HTTP Request node. This note provides a brief explanation of how it works.

As an example, I will delete a measurement via the API. The InfluxDB v2 API simplifies the process of removing a measurement. I have written another note on how this works using the terminal without using Node-RED.

Below, you will also find an example of a write request that can be used for bulk processing.

How To

Node-RED flow setup

  1. First create a new flow within Node-RED. In the menu choose Flows and then Add

  2. Go to the menu again and choose Import. Then the Import nodes screen opens. Paste the JSON below into the text field and click Import:

    [{"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":[["c6a4fc2ecafa1940"]]},{"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":[["005b31a1e984701e"]]},{"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":[["79d522108d85555d"]]},{"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: Node-RED Flow

  3. 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;
    // IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace YOUR_MEASUREMENT with the name of the measurement you want to delete
    • Required Replace YOUR_TOKEN with your own token
  4. Click Done to close the Edit function node screen

  5. 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
    Instructions:
    • Required Replace YOUR_IP and YOUR_PORT with the IP address and port of InfluxDB. Usually port 8086 is used but my container is using port 3004
    • Required Replace YOUR_ORGANIZATION with your own organization name. I used the name of my virtual machine for example
    • Required Replace YOUR_BUCKET with your own bucket name. Because data of Home Assistant is stored I used homeassistant
  6. Click Done to close the Edit http request node screen

  7. Deploy the flow and test by clicking the inject 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:

  1. Double click on the set payload and headers node and follow the instructions below

    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;
    // IMPORTANT: Please read the instructions below
    Instructions:
    • Required Replace YOUR_MEASUREMENT with the name of the measurement you want to delete
    • Required Replace YOUR_TOKEN with your own token
    • Required The sensor sensor_id=TLM0201 is an example of a tag and temperature=73.97038159354763,humidity=35.23103248356096 are examples of fields. Replace with your own tag and fields
  2. Click Done to close the Edit function node screen

  3. 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
    Instructions:
    • Required Replace YOUR_IP and YOUR_PORT with the IP address and port of InfluxDB. Usually port 8086 is used but my container is using port 3004
    • Required Replace YOUR_ORGANIZATION with your own organization name. I used the name of my virtual machine for example
    • Required Replace YOUR_BUCKET with your own bucket name. Because data of Home Assistant is stored I used homeassistant
  4. Click Done to close the Edit http request node screen

  5. Deploy the flow and test by clicking the inject node.

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. But that is a personal choice.

Favorites

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact