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:

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

    1. yarn install
    2. 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](](http://localhost:3001/examples/index.html)

    1. 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.

    1. 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.

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

    1. 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.

    1. 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.

  1. writeApi.useDefaultTags({location: 'browser'})
  2. const point1 = new Point('temperature')
  3. .tag('example', 'index.html')
  4. .floatField('value', 24)
  5. writeApi.writePoint(point1)
  6. console.log(`${point1}`)
  7. writeApi.close()

Complete example write script

  1. const influxDB = new InfluxDB({proxy, token})
  2. const writeApi = influxDB.getWriteApi(org, bucket)
  3. // setup default tags for all writes through this API
  4. writeApi.useDefaultTags({location: 'browser'})
  5. const point1 = new Point('temperature')
  6. .tag('example', 'index.html')
  7. .floatField('value', 24)
  8. writeApi.writePoint(point1)
  9. console.log(` ${point1}`)
  10. // flush pending writes and close writeApi
  11. writeApi
  12. .close()
  13. .then(() => {
  14. console.log('WRITE FINISHED')
  15. })

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.

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

    1. const fluxQuery =
    2. 'from(bucket:"<my-bucket>")
    3. |> range(start: 0)
    4. |> 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.

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

Complete example query script

  1. // performs query and receive line table metadata and rows
  2. // https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
  3. queryApi.queryRows(fluxQuery, {
  4. next(row: string[], tableMeta: FluxTableMetaData) {
  5. const o = tableMeta.toObject(row)
  6. // console.log(JSON.stringify(o, null, 2))
  7. console.log(
  8. '${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
  9. )
  10. },
  11. error(error: Error) {
  12. console.error(error)
  13. console.log('\nFinished ERROR')
  14. },
  15. complete() {
  16. console.log('\nFinished SUCCESS')
  17. },
  18. })

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

client libraries JavaScript