Write data with the InfluxDB JavaScript client library

This page documents an earlier version of InfluxDB. InfluxDB v2.7 is the latest stable version. View this page in the v2.7 documentation.

Use the InfluxDB Javascript client library to write data from a Node.js environment to InfluxDB.

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.

Before you begin

Write data with the client library

  1. Instantiate an InfluxDB client. Provide your InfluxDB URL and API token.

    1. import {InfluxDB, Point} from '@influxdata/influxdb-client'
    2. const influxDB = new InfluxDB({YOUR_URL, YOUR_API_TOKEN})

    Replace the following:

    • YOUR_URL: InfluxDB URL
    • YOUR_API_TOKEN: InfluxDB API token
  2. Use the getWriteApi() method of the client to create a write client. Provide your InfluxDB organization ID and bucket name.

    1. const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)

    Replace the following:

    • YOUR_ORG: InfluxDB organization ID
    • YOUR_BUCKET: InfluxDB bucket name
  3. To apply one or more tags to all points, use the useDefaultTags() method. Provide tags as an object of key/value pairs.

    1. writeApi.useDefaultTags({region: 'west'})
  4. Use the Point() constructor to create a point.

    1. Call the constructor and provide a measurement.
    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 float, 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)
  5. Use the writePoint() method to write the point to your InfluxDB bucket. Finally, use the close() method to flush all pending writes. The example logs the new data point followed by “WRITE FINISHED” to stdout.

    1. writeApi.writePoint(point1)
    2. writeApi.close().then(() => {
    3. console.log('WRITE FINISHED')
    4. })

Complete example

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. })

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

Response codes

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

client libraries JavaScript