---
title: Query data with the InfluxDB JavaScript client library
description: Use the JavaScript client library to query data with the InfluxDB API in Node.js.
url: https://docs.influxdata.com/influxdb/v2/api-guide/client-libraries/nodejs/query/
estimated_tokens: 3717
product: InfluxDB OSS v2
version: v2
---

# Query data with the InfluxDB JavaScript client library

This page documents an earlier version of InfluxDB OSS. [InfluxDB 3 Core](/influxdb3/core/) is the latest stable version.

#### API token hashing is enabled by default in InfluxDB OSS 2.9.0

Stronger token security: tokens are stored as hashes on disk, so a copy of the database file doesn’t expose usable tokens. Existing tokens are hashed on first startup and the original strings can’t be recovered afterward — **capture any plaintext tokens you still need before you upgrade**.

For more information, see [Token hashing](/influxdb/v2/admin/tokens/#token-hashing).

Use the [InfluxDB JavaScript client library](https://github.com/influxdata/influxdb-client-js) 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

-   [Install the client library and other dependencies](/influxdb/v2/api-guide/client-libraries/nodejs/install/).

## Query InfluxDB

1. Change to your new project directory and create a file for your query module.
    
    ```sh
    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**.
    
    ```js
    import { InfluxDB, Point } from '@influxdata/influxdb-client'
    
    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.
    
    ```js
    const fluxQuery =
      'from(bucket: "YOUR_BUCKET")
        |> range(start: 0)
        |> 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**](http://reactivex.io/rxjs/manual/overview.html#observer) object. The client returns [table](/influxdb/v2/reference/syntax/annotated-csv/#tables) metadata and rows as an [RxJS **Observable**](http://reactivex.io/rxjs/manual/overview.html#observable). `queryRows()` subscribes your observer to the observable. Finally, the observer logs the rows from the response to the terminal.
    
    ```js
    const observer = {
      next(row, tableMeta) {
        const o = tableMeta.toObject(row)
        console.log(
          `${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
        )
      }
    }
    
    queryApi.queryRows(fluxQuery, observer)
    ```
    

### Complete example

```js
'use strict'
/** @module query 
 * Queries a data point in InfluxDB using the Javascript client library with Node.js.
**/

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

/** Environment variables **/
const url = process.env.INFLUX_URL || ''
const token = process.env.INFLUX_TOKEN
const org = process.env.INFLUX_ORG || ''

/**
 * Instantiate the InfluxDB client
 * with a configuration object.
 *
 * Get a query client configured for your org.
 **/
const queryApi = new InfluxDB({url, token}).getQueryApi(org)

/** To avoid SQL injection, use a string literal for the query. */
const fluxQuery = 'from(bucket:"air_sensor") |> range(start: 0) |> filter(fn: (r) => r._measurement == "temperature")'

const myQuery = async () => {
  for await (const {values, tableMeta} of queryApi.iterateRows(fluxQuery)) {
    const o = tableMeta.toObject(values)
    console.log(
      `${o._time} ${o._measurement} in '${o.location}' (${o.sensor_id}): ${o._field}=${o._value}`
    )
  }
}

/** Execute a query and receive line table metadata and rows. */
myQuery()
```

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

```sh
export INFLUX_URL=http://localhost:8086 && \
export INFLUX_TOKEN=YOUR_API_TOKEN && \
export INFLUX_ORG=YOUR_ORG && \
node query.js
```

For more examples and information, see the [JavaScript client on GitHub](https://github.com/influxdata/influxdb-client-js).

[client libraries](/influxdb/v2/tags/client-libraries/) [JavaScript](/influxdb/v2/tags/javascript/)
