JavaScript client library

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

    1. Install .

    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.

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

    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 , organization, , and proxy which relies upon proxy to forward requests to the target InfluxDB.

    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, , token, and proxy which relies on a proxy to forward requests to the target InfluxDB instance.

    3. Instantiate the InfluxDB JavaScript client and pass in the proxy and token parameters.

      1. const influxDB = new InfluxDB({proxy, token})

    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 .

      1. const writeApi = InfluxDB.getWriteApi(org, bucket)
    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()

    Use the Javascript library to query data from InfluxDB.

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

    Complete example query script

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