Write data with the InfluxDB API
Write data to InfluxDB using an HTTP request to the InfluxDB API /write
endpoint. Use the POST
request method and include the following in your request:
Requirement | Include by |
---|---|
Organization | Use the org query parameter in your request URL. |
Bucket | Use the bucket query parameter in your request URL. |
Timestamp precision | Use the precision query parameter in your request URL. Default is ns . |
API token | Use the Authorization: Token YOUR_API_TOKEN header. |
Line protocol | Pass as plain text in your request body. |
Send a write request
The URL in the examples depends on the version and location of your InfluxDB 2.1 instance. Customize URLs in examples
curl --request POST \
"http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
--header "Authorization: Token YOUR_API_TOKEN" \
--header "Content-Type: text/plain; charset=utf-8" \
--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
'
'use strict'
/** @module write
* Writes a data point to InfluxDB using the Javascript client library with Node.js.
**/
import { InfluxDB, Point } from '@influxdata/influxdb-client'
/** Environment variables **/
const url = process.env.INFLUX_URL
const token = process.env.INFLUX_TOKEN
const org = process.env.INFLUX_ORG
const bucket = process.env.INFLUX_BUCKET
/**
* Instantiate the InfluxDB client
* with a configuration object.
**/
const influxDB = new InfluxDB({ url, token })
/**
* Create a write client from the getWriteApi method.
* Provide your `org` and `bucket`.
**/
const writeApi = influxDB.getWriteApi(org, bucket)
/**
* Apply default tags to all points.
**/
writeApi.useDefaultTags({ region: 'west' })
/**
* Create a point and write it to the buffer.
**/
const point1 = new Point('temperature')
.tag('sensor_id', 'TLM01')
.floatField('value', 24.0)
console.log(` ${point1}`)
writeApi.writePoint(point1)
/**
* Flush pending writes and close writeApi.
**/
writeApi.close().then(() => {
console.log('WRITE FINISHED')
})
Use gzip compression with the InfluxDB API
When using the InfluxDB API /write
endpoint to write data, compress the data with gzip
and set the Content-Encoding
header to gzip
. Compression reduces network bandwidth, but increases server-side load.
echo "airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630525358
airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630525358" | gzip > air-sensors.gzip
curl --request POST \
"http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
--header "Authorization: Token YOUR_API_TOKEN" \
--header "Content-Encoding: gzip" \
--header "Content-Type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary @air-sensors.gzip
For information about InfluxDB API response codes, see InfluxDB API Write documentation.