Query data with the InfluxDB JavaScript client library

    The following example sends a Flux query to an InfluxDB bucket and outputs rows from an observable table.

    Query InfluxDB

    1. Change to your new project directory and create a file for your query module.

      1. import { InfluxDB, Point } from '@influxdata/influxdb-client'

      Replace the following:

      • YOUR_URL: InfluxDB URL
      • YOUR_API_TOKEN: InfluxDB API token
      • YOUR_ORG: InfluxDB organization ID
    2. Create a Flux query for your InfluxDB bucket. Store the query as a string variable.

      Replace YOUR_BUCKET with the name of your InfluxDB bucket.

    3. Use the method of the query client to query InfluxDB. queryRows() takes a Flux query and an RxJS Observer object. The client returns metadata and rows as an RxJS Observable. queryRows() subscribes your observer to the observable. Finally, the observer logs the rows from the response to the terminal.

      1. const observer = {
      2. next(row, tableMeta) {
      3. const o = tableMeta.toObject(row)
      4. `${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
      5. )
      6. }
      7. queryApi.queryRows(fluxQuery, observer)
    1. export INFLUX_URL=http://localhost:8086 && \
    2. export INFLUX_TOKEN=YOUR_API_TOKEN && \
    3. node query.js

    For more examples and information, see the .