Documentation

Query data with the JavaScript client library

Use the Javascript library to query data from InfluxDB.

  1. Use the getQueryApi method of the InfluxDB client to create a new query client. Provide your InfluxDB org.

    const queryApi = influxDB.getQueryApi(org)
    
  2. Create a Flux query (including your bucket parameter).

    const fluxQuery =
      'from(bucket:"<my-bucket>")
      |> range(start: 0)
      |> filter(fn: (r) => r._measurement == "temperature")'
    

    The query client sends the Flux query to InfluxDB and returns line table metadata and rows.

  3. Use the next method to iterate over the rows.

    queryApi.queryRows(fluxQuery, {
      next(row: string[], tableMeta: FluxTableMetaData) {
        const o = tableMeta.toObject(row)
        // console.log(JSON.stringify(o, null, 2))
        console.log(
          `${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
        )
      }
    }
    

Complete example query script

// performs query and receive line table metadata and rows
// https://v2.docs.influxdata.com/v2.0/reference/syntax/annotated-csv/
queryApi.queryRows(fluxQuery, {
  next(row: string[], tableMeta: FluxTableMetaData) {
    const o = tableMeta.toObject(row)
    // console.log(JSON.stringify(o, null, 2))
    console.log(
      '${o._time} ${o._measurement} in '${o.location}' (${o.example}): ${o._field}=${o._value}`
    )
  },
  error(error: Error) {
    console.error(error)
    console.log('\nFinished ERROR')
  },
  complete() {
    console.log('\nFinished SUCCESS')
  },
})

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


Select your region

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.