Install the InfluxDB JavaScript client library
This page documents an earlier version of InfluxDB. InfluxDB v2.7 is the latest stable version. View this page in the v2.7 documentation.
Install Node.js
- Install Node.js.
- 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.
- Start a new Node.js project. The
npmpackage manager is included with Node.js.
1. npm init -y influx-node-app
Install TypeScript
Many of the client library examples use TypeScript. Follow these steps to initialize the TypeScript project.
- Install TypeScript and type definitions for Node.js.
1. npm i -g typescript && npm i --save-dev @types/node
2. Create a TypeScript configuration with default values.
1. tsc --init
3. Run the TypeScript compiler. To recompile your code automatically as you make changes, pass the watch flag to the compiler.
1. tsc -w -p
Install dependencies
The JavaScript client library contains two packages: @influxdata/influxdb-client and @influxdata/influxdb-client-apis. Add both as dependencies of your project.
- Open a new terminal window and install
@influxdata/influxdb-clientfor querying and writing data:
1. npm install --save @influxdata/influxdb-client
2. Install @influxdata/influxdb-client-apis for access to the InfluxDB management APIs:
1. npm install --save @influxdata/influxdb-client-apis
Next steps
Once you’ve installed the Javascript client library, you’re ready to write data to InfluxDB or get started with other examples from the client library.
Get started with examples
The client examples include an env module for accessing your InfluxDB properties from environment variables or from env.js. The examples use these properties to interact with the InfluxDB API.
- Set environment variables or update
env.jswith your InfluxDB bucket, organization, token, and url.
1. export INFLUX_URL=http://localhost:8086
2. export INFLUX_TOKEN=YOUR_API_TOKEN
3. export INFLUX_ORG=YOUR_ORG
4. export INFLUX_BUCKET=YOUR_BUCKET
Replace the following:
YOUR_API_TOKEN: InfluxDB API tokenYOUR_ORG: InfluxDB organization IDYOUR_BUCKET: InfluxDB bucket name- Run an example script.
1. query.ts
For more examples and information, see the JavaScript client on GitHub.
