JavaScript client library
Use the InfluxDB JavaScript client library to integrate InfluxDB into JavaScript scripts and applications. This client supports both client-side (browser) and server-side (NodeJS) environments.
This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB. If just getting started, see Get started with InfluxDB.
Before you begin
Install NodeJS.
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.
Easiest way to get started
- Clone the examples directory in the influxdb-client-js repo.
Navigate to the
examples
directory:cd examples
Install
yarn
ornpm
dependencies as needed:yarn install
npm install
Update your
./env
andindex.html
with the name of your InfluxDB bucket, organization, token, andproxy
which relies upon proxy to forward requests to the target InfluxDB.Run the following command to run the application at [http://localhost:3001/examples/index.html](](http://localhost:3001/examples/index.html)
npm run browser
Boilerplate for the InfluxDB Javascript client library
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, organization, token, and
proxy
which relies on a proxy to forward requests to the target InfluxDB instance.const proxy = '/influx'
const token = 'example-token'
const org = 'example-org'
const bucket = 'example-bucket'
Instantiate the InfluxDB JavaScript client and pass in the
proxy
andtoken
parameters.const influxDB = new InfluxDB({proxy, token})
Write data to InfluxDB with JavaScript
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
andbucket
.const writeApi = InfluxDB.getWriteApi(org, bucket)
The useDefaultTags
method instructs the write api to use default tags when writing points. Create a point and write it to InfluxDB using the writePoint
method. The tag
and floatField
methods add key value pairs for the tags and fields, respectively. Close the client to flush all pending writes and finish.
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', 24)
writeApi.writePoint(point1)
console.log(`${point1}`)
writeApi.close()
Complete example write script
const influxDB = new InfluxDB({proxy, token})
const writeApi = influxDB.getWriteApi(org, bucket)
// setup default tags for all writes through this API
writeApi.useDefaultTags({location: 'browser'})
const point1 = new Point('temperature')
.tag('example', 'index.html')
.floatField('value', 24)
writeApi.writePoint(point1)
console.log(` ${point1}`)
// flush pending writes and close writeApi
writeApi
.close()
.then(() => {
console.log('WRITE FINISHED')
})
Query data from InfluxDB with JavaScript
Use the Javascript library to query data from InfluxDB.
Use the
getQueryApi
method of theInfluxDB
client to create a new query client. Provide your InfluxDBorg
.const queryApi = influxDB.getQueryApi(org)
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
// 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 information, see the JavaScript client README on GitHub.