Write data with the InfluxDB JavaScript client library

    The Javascript client library includes the following convenient features for writing data to InfluxDB:

    • Apply default tags to data points.
    • Buffer points into batches to optimize data transfer.
    • Automatically retry requests on failure.
    • Set an optional HTTP proxy address for your network.
    1. Instantiate an client. Provide your InfluxDB URL and API token.

      Replace the following:

      • YOUR_URL: InfluxDB URL
      • YOUR_API_TOKEN: InfluxDB API token
    2. To apply one or more to all points, use the useDefaultTags() method. Provide tags as an object of key/value pairs.

    3. Use the Point() constructor to create a point.

      1. Call the constructor and provide a .
      2. To add one or more tags, chain the tag() method to the constructor. Provide a name and value.
      3. To add a field of type , chain the floatField() method to the constructor. Provide a name and value.
      1. const point1 = new Point('temperature')
      2. .tag('sensor_id', 'TLM010')
      3. .floatField('value', 24)

    Curl

    1. curl --request POST \
    2. --header "Authorization: Token YOUR_API_TOKEN" \
    3. --header "Accept: application/json" \
    4. --data-binary '
    5. airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630424257000000000
    6. airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630424257000000000
    7. '

    To run the example from a file, set your InfluxDB environment variables and use node to execute the JavaScript file.

    1. export INFLUX_URL=http://localhost:8086 && \
    2. export INFLUX_TOKEN=YOUR_API_TOKEN && \
    3. export INFLUX_ORG=YOUR_ORG && \
    4. export INFLUX_BUCKET=YOUR_BUCKET && \
    5. node write.js

    For information about InfluxDB API response codes, see InfluxDB API Write documentation.