Documentation

InfluxDB v2 JavaScript client library for web browsers

Use InfluxDB 3 clients

The /api/v2/query API endpoint and associated tooling, such as InfluxDB v2 client libraries and the influx CLI, can’t query an InfluxDB Cloud Serverless cluster.

InfluxDB 3 client libraries and Flight SQL clients are available that integrate with your code to write and query data stored in InfluxDB Cloud Serverless.

InfluxDB 3 supports many different tools for writing and querying data. Compare tools you can use to interact with InfluxDB Cloud Serverless.

Use the InfluxDB v2 JavaScript client library in browsers and front-end clients to write data to an InfluxDB Cloud Serverless bucket.

This library supports both front-end and server-side environments and provides the following distributions:

  • ECMAScript modules (ESM) and CommonJS modules (CJS)
  • Bundled ESM
  • Bundled UMD

This guide presumes some familiarity with JavaScript, browser environments, and InfluxDB. If you’re just getting started with InfluxDB, see Get started with InfluxDB.

Tokens in production applications

The examples below configure the authentication token in source code for demonstration purposes only. To protect your data, take the following steps:

  1. Avoid sending tokens to public clients such as web browsers and mobile apps. Regard any application secret sent to client devices as public and not confidential.

  2. Use short-lived, read-only tokens whenever possible to prevent unauthorized writes and deletes.

Before you begin

  1. Install Node.js to serve your front-end app.

  2. Ensure that InfluxDB is running and you can connect to it.

Use with module bundlers

If you use a module bundler like Webpack or Parcel, install @influxdata/influxdb-client-browser.

Use bundled distributions with browsers and module loaders

  1. Configure InfluxDB properties for your script.

    <script>
      window.INFLUX_ENV = {
        url: 'https://cloud2.influxdata.com',
        token: 'API_TOKEN'
      }
    </script>
    
    • Copy
    • Fill window

    Replace the following:

    • API_TOKEN: An InfluxDB token with WRITE permission to the bucket.
  2. Import modules from the latest client library browser distribution. @influxdata/influxdb-client-browser exports bundled ESM and UMD syntaxes.

    <script type="module">
      import {InfluxDB, Point} from 'https://unpkg.com/@influxdata/influxdb-client-browser/dist/index.browser.mjs'
    
      const influxDB = new InfluxDB({INFLUX_ENV.url, INFLUX_ENV.token})
    </script>
    
    • Copy
    • Fill window
    <script src="https://unpkg.com/@influxdata/influxdb-client-browser"></script>
    <script>
      const Influx = window['@influxdata/influxdb-client']
    
      const InfluxDB = Influx.InfluxDB
      const influxDB = new InfluxDB({INFLUX_ENV.url, INFLUX_ENV.token})
    </script>
    
    • Copy
    • Fill window

After you’ve imported the client library, you’re ready to get started writing data with the example app.

Get started with the example app

The client library includes an example browser app that writes to your InfluxDB instance.

  1. Clone the influxdb-client-js repository.

  2. Navigate to the examples directory:

    cd examples
    
    • Copy
    • Fill window
  3. Update ./env_browser.js with your InfluxDB Cloud Serverless region URL, your bucket, an arbitrary string as org, and your API token.

  4. Run the following command to start the application at http://localhost:3001/examples/index.html

    npm run browser
    
    • Copy
    • Fill window

    index.html loads the env_browser.js configuration, the client library ESM modules, and the application in your browser.

For more examples, see how to write data using the JavaScript client library for Node.js.


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out:

InfluxDB Cloud Serverless