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.
Instantiate an client. Provide your InfluxDB URL and API token.
Replace the following:
YOUR_URL
: InfluxDB URLYOUR_API_TOKEN
: InfluxDB API token
To apply one or more to all points, use the
useDefaultTags()
method. Provide tags as an object of key/value pairs.Use the
Point()
constructor to create a point.- Call the constructor and provide a .
- To add one or more tags, chain the
tag()
method to the constructor. Provide aname
andvalue
. - To add a field of type , chain the
floatField()
method to the constructor. Provide aname
andvalue
.
const point1 = new Point('temperature')
.tag('sensor_id', 'TLM010')
.floatField('value', 24)
curl --request POST \
--header "Authorization: Token YOUR_API_TOKEN" \
--header "Accept: application/json" \
--data-binary '
airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630424257000000000
airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630424257000000000
'
To run the example from a file, set your InfluxDB environment variables and use node
to execute the JavaScript file.
export INFLUX_URL=http://localhost:8086 && \
export INFLUX_TOKEN=YOUR_API_TOKEN && \
export INFLUX_ORG=YOUR_ORG && \
export INFLUX_BUCKET=YOUR_BUCKET && \
node write.js
For information about InfluxDB API response codes, see InfluxDB API Write documentation.