Query data with the InfluxDB v2 JavaScript client library

Use the InfluxDB v2 JavaScript client library in a Node.js environment to query data stored in an InfluxDB Cloud Serverless bucket.

The InfluxDB v2 JavaScript client library uses Flux and the InfluxDB API /api/v2/query endpoint to query data.

POST http://localhost:8086/api/v2/query

The following example sends a Flux-wrapped SQL query to an InfluxDB bucket, and then uses RxJS with an observer to process response data.

Before you begin

Query InfluxDB

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

    cd influx-node-app && touch query.js
  2. In query.js:

    1. Import InfluxDB from @influxdata/influxdb-client.

    2. Define an SQL query as a string. Assign a variable to the query.

    3. Define a Flux script as a string that contains the following:

      • import statement for the experimental/iox library.
      • iox.sql(bucket:, query:) function call with your bucket name and the SQL query from the preceding step.

      Assign a variable to the script.

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

    4. Call the new InfluxDB({url, token}) constructor to instantiate an InfluxDB API client. Provide your InfluxDB URL and API token (environment variables you already set in the Install section).

    5. Call the client’s getQueryApi() method with your InfluxDB organization ID to create a QueryApi query client configured for your organization.

    6. Define an RxJS Observer with a next() callback that will process data and table metadata for each row in the result.

    7. Call the query client’s queryRows(query, consumer) method. Provide the Flux script and the observer as arguments. The queryRows method sends the request, and then subscribes the observer to the response data.

Complete example

import {InfluxDB} from '@influxdata/influxdb-client';

// Define the SQL to query data in your bucket.
const sql=`
    DATE_BIN(INTERVAL '2 hours', time, '1970-01-01T00:00:00Z'::TIMESTAMP) AS _time,
    AVG(value) AS 'average temp'
  FROM temperature
  ORDER BY sensor_id, _time

// Define a Flux script that uses iox.sql() to execute the SQL against the bucket.
const fluxQuery = `
  import "experimental/iox"
    bucket: "${process.env.INFLUX_BUCKET}",
    query: "${sql}"

// Instantiate a query client permisssioned to query the bucket in your organization.
const queryApi = new InfluxDB({url: process.env.INFLUX_URL,
                            token: process.env.INFLUX_TOKEN})

console.log('*** QueryRows ***');

// Define an RxJS observer that handles notifications and processes your data.
const observer = {
  next: (row, tableMeta) => {
    // From each row, create an object with column names as keys.
    const o = tableMeta.toObject(row)
    // Process data--for example, output columns to the console.
      `${o.time}: sensor: ${o['sensor_id']}, temp: ${o['average temp']}`
  error: (error) => {
    console.log('\nQueryRows ERROR')
  complete: () => {
    console.log('\nQueryRows SUCCESS')

// Send the request and subscribe the observer to the response data.
queryApi.queryRows(fluxQuery, observer);

In your terminal with environment variables or env.js set, run the following command to execute the JavaScript file:

node query.js

If successful, the observer receives a next notification for each row and outputs data to the terminal.

Was this page helpful?

Thank you for your feedback!

Introducing InfluxDB 3.0

The new core of InfluxDB built with Rust and Apache Arrow. Available today in InfluxDB Cloud Dedicated.

Learn more

State of the InfluxDB Cloud Serverless documentation

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.

InfluxDB Cloud Serverless powered by IOx