Documentation

JavaScript client library

Use the InfluxDB JavaScript client library to integrate InfluxDB into JavaScript scripts and applications. This client supports both client-side (browser) and server-side (NodeJS) environments.

This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB. If just getting started, see Get started with InfluxDB.

Before you begin

  1. Install NodeJS.

  2. Ensure that InfluxDB is running and you can connect to it. For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see InfluxDB URLs.

Easiest way to get started

  1. Clone the examples directory in the influxdb-client-js repo.
  2. Navigate to the examples directory:

    cd examples
    
  3. Install yarn or npm dependencies as needed:

    yarn install
    npm install
    
  4. Update your ./env and index.html with the name of your InfluxDB bucket, organization, token, and proxy which relies upon proxy to forward requests to the target InfluxDB.

  5. Run the following command to run the application at [http://localhost:3001/examples/index.html]()

    npm run browser

Boilerplate for the InfluxDB Javascript client library

Use the Javascript library to write data to and query data from InfluxDB.

  1. To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.

    import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
    
  2. Define constants for your InfluxDB bucket, organization, token, and proxy which relies on a proxy to forward requests to the target InfluxDB instance.

    const proxy = '/influx'
    const token = 'example-token'
    const org = 'example-org'
    const bucket = 'example-bucket'
    
  3. Instantiate the InfluxDB JavaScript client and pass in the proxy and token parameters.

    const InfluxDB = new InfluxDB({proxy, token})
    

Write data to InfluxDB with JavaScript

Use the Javascript library to write data to InfluxDB.

  1. Use the getWriteApi method of the InfluxDB client to create a write client. Provide your InfluxDB org and bucket.

    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)
        writeApi.writePoint(point1)
        console.log(`${point1}`)
   writeApi.close()

Complete example write script

const writeApi = new InfluxDB({proxy, token})
const writeApi = influxDB.getWriteApi(org, bucket)
// setup default tags for all writes through this API
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
  .tag('example', 'index.html')
  .floatField('value', 24)
writeApi.writePoint(point1)
console.log(` ${point1}`)
// flush pending writes and close writeApi
writeApi
  .close()
  .then(() => {
    console.log('WRITE FINISHED')
  })

Query data from InfluxDB with JavaScript

Use the Javascript library to query data from InfluxDB.

  1. Use the getQueryApi method of the InfluxDB client to create a new query client. Provide your InfluxDB org.

    const queryApi = influxDB.getQueryApi(org)
    
  2. Create a Flux query (including your bucket parameter).

    const fluxQuery =
     'from(bucket:"<my-bucket>")
     |> range(start: 0)
     |> filter(fn: (r) => r._measurement == "temperature")'
    

The query client sends the Flux query to InfluxDB and returns line table metadata and rows.

  1. Use the next method to iterate over the rows.

    queryApi.queryRows(fluxQuery, {
     next(row: string[], tableMeta: FluxTableMetaData) {
       const o = tableMeta.toObject(row)
       // console.log(JSON.stringify(o, null, 2))
       console.log(
         `${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
       )
     }
    }
    

Complete example query script

// performs query and receive line table metadata and rows
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
queryApi.queryRows(fluxQuery, {
  next(row: string[], tableMeta: FluxTableMetaData) {
    const o = tableMeta.toObject(row)
    // console.log(JSON.stringify(o, null, 2))
    console.log(
      '${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
    )
  },
  error(error: Error) {
    console.error(error)
    console.log('\nFinished ERROR')
  },
  complete() {
    console.log('\nFinished SUCCESS')
  },
})

For more information, see the JavaScript client README on GitHub.


New! Cloud or OSS?