JavaScript client library
This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB. If just getting started, see Get started with InfluxDB.
Install .
Ensure that InfluxDB is running and you can connect to it. For information about what URL to use to connect to InfluxDB OSS or InfluxDB Cloud, see InfluxDB URLs.
- Clone the in the influxdb-client-js repo.
Navigate to the directory:
Install
yarn
ornpm
dependencies as needed:yarn install
npm install
Update your
./env
andindex.html
with the name of your InfluxDB , organization, , andproxy
which relies upon proxy to forward requests to the target InfluxDB.
Use the Javascript library to write data to and query data from InfluxDB.
To write a data point to InfluxDB using the JavaScript library, import the latest InfluxDB Javascript library in your script.
import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client/dist/index.browser.mjs'
Define constants for your InfluxDB bucket, , token, and
proxy
which relies on a proxy to forward requests to the target InfluxDB instance.Instantiate the InfluxDB JavaScript client and pass in the
proxy
andtoken
parameters.const influxDB = new InfluxDB({proxy, token})
Use the Javascript library to write data to InfluxDB.
Use the
getWriteApi
method of the InfluxDB client to create a write client. Provide your InfluxDBorg
and .const writeApi = InfluxDB.getWriteApi(org, bucket)
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', 24)
writeApi.writePoint(point1)
console.log(`${point1}`)
writeApi.close()
Use the Javascript library to query data from InfluxDB.
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.
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
For more information, see the JavaScript client README on GitHub.