Write data with the InfluxDB API

Write data to InfluxDB using an HTTP request to the InfluxDB API /api/v2/write endpoint. Use the POST request method and include the following in your request:

RequirementInclude by
OrganizationUse the org query parameter in your request URL.
BucketUse the bucket query parameter in your request URL.
Timestamp precisionUse the precisionquery parameter in your request URL. Default is ns.
API tokenUse the Authorization: Token YOUR_API_TOKEN header.
Line protocolPass 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.7 instance. Customize URLs in examples

Curl Node.js

  1. curl --request POST \
  2. "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
  3. --header "Authorization: Token YOUR_API_TOKEN" \
  4. --header "Content-Type: text/plain; charset=utf-8" \
  5. --header "Accept: application/json" \
  6. --data-binary '
  7. airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630424257000000000
  8. airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630424257000000000
  9. '
  1. 'use strict'
  2. /** @module write
  3. * Writes a data point to InfluxDB using the Javascript client library with Node.js.
  4. **/
  5. import { InfluxDB, Point } from '@influxdata/influxdb-client'
  6. /** Environment variables **/
  7. const url = process.env.INFLUX_URL
  8. const token = process.env.INFLUX_TOKEN
  9. const org = process.env.INFLUX_ORG
  10. const bucket = process.env.INFLUX_BUCKET
  11. /**
  12. * Instantiate the InfluxDB client
  13. * with a configuration object.
  14. **/
  15. const influxDB = new InfluxDB({ url, token })
  16. /**
  17. * Create a write client from the getWriteApi method.
  18. * Provide your `org` and `bucket`.
  19. **/
  20. const writeApi = influxDB.getWriteApi(org, bucket)
  21. /**
  22. * Apply default tags to all points.
  23. **/
  24. writeApi.useDefaultTags({ region: 'west' })
  25. /**
  26. * Create a point and write it to the buffer.
  27. **/
  28. const point1 = new Point('temperature')
  29. .tag('sensor_id', 'TLM01')
  30. .floatField('value', 24.0)
  31. console.log(` ${point1}`)
  32. writeApi.writePoint(point1)
  33. /**
  34. * Flush pending writes and close writeApi.
  35. **/
  36. writeApi.close().then(() => {
  37. console.log('WRITE FINISHED')
  38. })
Use gzip compression with the InfluxDB API

When using the InfluxDB API /api/v2/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.

  1. echo "airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630525358
  2. airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630525358" | gzip > air-sensors.gzip
  3. curl --request POST \
  4. "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
  5. --header "Authorization: Token YOUR_API_TOKEN" \
  6. --header "Content-Encoding: gzip" \
  7. --header "Content-Type: text/plain; charset=utf-8" \
  8. --header "Accept: application/json" \
  9. --data-binary @air-sensors.gzip

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