Documentation

Write data with the JavaScript client library

Write data to InfluxDB with JavaScript

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

  1. Instantiate an InfluxDB client. Provide your InfluxDB url and token.
  2. Use the getWriteApi method of the instantiated InfluxDB client to create a write client. Provide your InfluxDB org and bucket.
import {InfluxDB, Point} from '@influxdata/influxdb-client'

const influxDB = new InfluxDB({url, token})
const writeApi = influxDB.getWriteApi(org, bucket)

The useDefaultTags method instructs the write api to use default tags when writing points. Create a point and write it to InfluxDB using the writePoint method. The tag and floatField methods add key value pairs for the tags and fields, respectively. Close the client to flush all pending writes and finish.

writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
    .tag('example', 'index.html')
    .floatField('value', 24)
console.log(`${point1}`)

writeApi.writePoint(point1)
writeApi.close()

Complete example

curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
  --header "Authorization: Token YOURAUTHTOKEN" \
  --data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"

"use strict"
/** @module write
 * Writes a data point to InfluxDB using the Node.js client.
**/

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)

/**
 * Setup default tags for all writes.
 **/
writeApi.useDefaultTags({ location: "browser" })
const point1 = new Point("temperature")
  .tag("example", "index.html")
  .floatField("value", 24)
console.log(` ${point1}`)

writeApi.writePoint(point1)

/**
 * Flush pending writes and close writeApi.
 **/
writeApi.close().then(() => {
  console.log("WRITE FINISHED")
})

Response codes

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

Compressing data

To compress data when writing to InfluxDB, set the Content-Encoding header to gzip. Compression reduces network bandwidth, but increases server-side load.

curl --request POST "http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s" \
  --header "Authorization: Token YOURAUTHTOKEN" \
  --header "Content-Encoding: gzip" \
  --data-raw "
mem,host=host1 used_percent=23.43234543 1556896326
mem,host=host2 used_percent=26.81522361 1556896326
mem,host=host1 used_percent=22.52984738 1556896336
mem,host=host2 used_percent=27.18294630 1556896336
"


Select your region

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.