Query data with the InfluxDB JavaScript client library

This page documents an earlier version of InfluxDB. InfluxDB v2.7 is the latest stable version. View this page in the v2.7 documentation.

Use the InfluxDB JavaScript client library in a Node.js environment to query InfluxDB.

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

Before you begin

Query InfluxDB

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

    1. cd influx-node-app && touch query.js
  2. Instantiate an InfluxDB client. Provide your InfluxDB URL and API token. Use the getQueryApi() method of the client. Provide your InfluxDB organization ID to create a configured query client.

    1. import { InfluxDB, Point } from '@influxdata/influxdb-client'
    2. const queryApi = new InfluxDB({YOUR_URL, YOUR_API_TOKEN}).getQueryApi(YOUR_ORG)

    Replace the following:

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

    To prevent SQL injection attacks, avoid concatenating unsafe user input with queries.

    1. const fluxQuery =
    2. 'from(bucket: "YOUR_BUCKET")
    3. |> range(start: 0)
    4. |> filter(fn: (r) => r._measurement == "temperature")'

    Replace YOUR_BUCKET with the name of your InfluxDB bucket.

  4. Use the queryRows() method of the query client to query InfluxDB. queryRows() takes a Flux query and an RxJS Observer object. The client returns table 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. console.log(
    5. `${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
    6. )
    7. }
    8. }
    9. queryApi.queryRows(fluxQuery, observer)

Complete example

  1. 'use strict'
  2. /** @module query
  3. * Queries a data point in InfluxDB using the Javascript client library with Node.js.
  4. **/
  5. import { InfluxDB, Point } from '@influxdata/influxdb-client'
  6. /** Environment variables **/
  7. const url = process.env.INFLUX_URL || ''
  8. const token = process.env.INFLUX_TOKEN
  9. const org = process.env.INFLUX_ORG || ''
  10. /**
  11. * Instantiate the InfluxDB client
  12. * with a configuration object.
  13. *
  14. * Get a query client configured for your org.
  15. **/
  16. const queryApi = new InfluxDB({url, token}).getQueryApi(org)
  17. /** To avoid SQL injection, use a string literal for the query. */
  18. const fluxQuery = 'from(bucket:"air_sensor") |> range(start: 0) |> filter(fn: (r) => r._measurement == "temperature")'
  19. const myQuery = async () => {
  20. for await (const {values, tableMeta} of queryApi.iterateRows(fluxQuery)) {
  21. const o = tableMeta.toObject(values)
  22. console.log(
  23. `${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
  24. )
  25. }
  26. }
  27. /** Execute a query and receive line table metadata and rows. */
  28. myQuery()

To run the example from a file, set your InfluxDB environment variables and use node to execute the JavaScript file.

  1. export INFLUX_URL=http://localhost:8086 && \
  2. export INFLUX_TOKEN=YOUR_API_TOKEN && \
  3. export INFLUX_ORG=YOUR_ORG && \
  4. node query.js

For more examples and information, see the JavaScript client on GitHub.

client libraries JavaScript