Write data with the InfluxDB JavaScript client library
This page documents an earlier version of InfluxDB OSS. InfluxDB 3 Core is the latest stable version.
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
- Instantiate an - InfluxDBclient. Provide your InfluxDB URL and API token.- import {InfluxDB, Point} from '@influxdata/influxdb-client' const influxDB = new InfluxDB({YOUR_URL, YOUR_API_TOKEN})- Replace the following: - YOUR_URL: InfluxDB URL
- YOUR_API_TOKEN: InfluxDB API token
 
- Use the - getWriteApi()method of the client to create a write client. Provide your InfluxDB organization ID and bucket name.- const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)- Replace the following: - YOUR_ORG: InfluxDB organization ID
- YOUR_BUCKET: InfluxDB bucket name
 
- To apply one or more tags to all points, use the - useDefaultTags()method. Provide tags as an object of key/value pairs.- writeApi.useDefaultTags({region: 'west'})
- Use the - Point()constructor to create a point.- Call the constructor and provide a measurement.
- To add one or more tags, chain the tag()method to the constructor. Provide anameandvalue.
- To add a field of type float, chain thefloatField()method to the constructor. Provide anameandvalue.
 - const point1 = new Point('temperature') .tag('sensor_id', 'TLM010') .floatField('value', 24)
- 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.- writeApi.writePoint(point1) writeApi.close().then(() => { console.log('WRITE FINISHED') })
Complete example
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')
})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.jsResponse codes
For information about InfluxDB API response codes, see InfluxDB API Write documentation.
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.
