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:
| 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 precisionquery 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.7 instance. Customize URLs in examples
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. **/
6. import { InfluxDB, Point } from '@influxdata/influxdb-client'
8. /** Environment variables **/
9. const url = process.env.INFLUX_URL
10. const token = process.env.INFLUX_TOKEN
11. const org = process.env.INFLUX_ORG
12. const bucket = process.env.INFLUX_BUCKET
14. /**
15. * Instantiate the InfluxDB client
16. * with a configuration object.
17. **/
18. const influxDB = new InfluxDB({ url, token })
20. /**
21. * Create a write client from the getWriteApi method.
22. * Provide your `org` and `bucket`.
23. **/
24. const writeApi = influxDB.getWriteApi(org, bucket)
26. /**
27. * Apply default tags to all points.
28. **/
29. writeApi.useDefaultTags({ region: 'west' })
31. /**
32. * Create a point and write it to the buffer.
33. **/
34. const point1 = new Point('temperature')
35. .tag('sensor_id', 'TLM01')
36. .floatField('value', 24.0)
37. console.log(` ${point1}`)
39. writeApi.writePoint(point1)
41. /**
42. * Flush pending writes and close writeApi.
43. **/
44. writeApi.close().then(() => {
45. console.log('WRITE FINISHED')
46. })
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
4. curl --request POST \
5. "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
6. --header "Authorization: Token YOUR_API_TOKEN" \
7. --header "Content-Encoding: gzip" \
8. --header "Content-Type: text/plain; charset=utf-8" \
9. --header "Accept: application/json" \
10. --data-binary @air-sensors.gzip
For information about InfluxDB API response codes, see InfluxDB API Write documentation.
