The JavaScript Client that provides a simple and convenient way to interact with InfluxDB 3. This package supports both writing data to InfluxDB and querying data using the FlightSQL client, which allows you to execute SQL queries against InfluxDB IOx.
To write or query InfluxDB 3, add @influxdata/influxdb3-client
as a dependency to your project using your favorite package manager.
npm install --save @influxdata/influxdb3-client
yarn add @influxdata/influxdb3-client
pnpm add @influxdata/influxdb3-client
If you target Node.js, use @influxdata/influxdb3-client. It provides main (CJS), module (ESM), and browser (UMD) exports.
set environment variables:
INFLUXDB_URL
region of your influxdb cloud e.g.https://us-east-1-1.aws.cloud2.influxdata.com/
INFLUXDB_TOKEN
read/write token generated in cloudINFLUXDB_DATABASE
name of database e.g .my-database
linux/macos
export INFLUXDB_URL="<url>"
export INFLUXDB_DATABASE="<database>"
export INFLUXDB_TOKEN="<token>"
windows
set INFLUXDB_URL=<url>
set INFLUXDB_DATABASE=<database>
set INFLUXDB_TOKEN=<token>
$env:INFLUXDB_URL "<url>"
$env:INFLUXDB_DATABASE "<database>"
$env:INFLUXDB_TOKEN "<token>"
To get started with influxdb client import @influxdata/influxdb3-client
package.
import {InfluxDBClient, Point} from '@influxdata/influxdb3-client'
Prepare environmnet variables and instanciate InfluxDBClient
in asynchronous function. Make sure to close
client after.
const url = process.env.INFLUXDB_URL
const token = process.env.INFLUXDB_TOKEN
const database = process.env.INFLUXDB_DATABASE
async function main() {
const client = new InfluxDBClient({url, token})
// following code goes here
client.close()
}
main()
The client
can be now used to insert data using line-protocol:
const line = `stat,unit=temperature avg=20.5,max=45.0`
await client.write(line, database)
Fetch data using SQL
query and print result.
// Execute query
const query = `
SELECT *
FROM "stat"
WHERE
time >= now() - interval '5 minute'
AND
"unit" IN ('temperature')
`
const queryResult = await client.query(query, database)
for await (const row of queryResult) {
console.log(`avg is ${row.avg}`)
console.log(`max is ${row.max}`)
}
For more complex examples see examples folder.
If you need help, please use our Community Slack or Community Page.
New features and bugs can be reported on GitHub: https://github.com/InfluxCommunity/influxdb3-js
If you would like to contribute code you can do through GitHub by forking the repository and sending a pull request into
the main
branch.
As of now, we're responsible for generating the Flight Client by ourself. However, its Protobuf interfaces may undergo changes over time. To re-generate the Flight Client, we have provided a script that can be executed using yarn flight
. This script will clone the Flight Protobuf repository and generate new TypeScript files into the client.
The InfluxDB 3 JavaScript Client is released under the MIT License.