4.2 KiB
| title | seotitle | list_title | description | menu | influxdb/cloud-dedicated/tags | weight | aliases | related | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| JavaScript client library for web browsers | Use the InfluxDB JavaScript client library for web browsers | JavaScript for browsers | Use the InfluxDB JavaScript client library to interact with InfluxDB in web clients. |
|
|
201 |
|
|
Use the InfluxDB JavaScript client library to interact with the InfluxDB v2 API in browsers and front-end clients. 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](/{{% latest "influxdb" %}}/get-started/).
{{% warn %}}
Tokens in production applications
{{% api/browser-token-warning %}} {{% /warn %}}
- Before you begin
- Use with module bundlers
- Use bundled distributions with browsers and module loaders
- Get started with the example app
Before you begin
-
Install Node.js to serve your front-end app.
-
Ensure that InfluxDB is running and you can connect to it. For information about what URL to use to connect to your InfluxDB Cloud Dedicated cluster, contact your InfluxData account representative.
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
-
Configure InfluxDB properties for your script.
<script> window.INFLUX_ENV = { url: 'https://cluster-id.influxdb.io', token: 'YOUR_AUTH_TOKEN' } </script> -
Import modules from the latest client library browser distribution.
@influxdata/influxdb-client-browserexports bundled ESM and UMD syntaxes.{{< code-tabs-wrapper >}} {{% code-tabs %}} ESM UMD {{% /code-tabs %}} {{% code-tab-content %}}
<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>{{% /code-tab-content %}} {{% code-tab-content %}}
<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>{{% /code-tab-content %}} {{< /code-tabs-wrapper >}}
After you've imported the client library, you're ready to [write data](/{{% latest "influxdb" %}}/api-guide/client-libraries/nodejs/write/?t=nodejs) to InfluxDB.
Get started with the example app
This library includes an example browser app that writes to your InfluxDB instance.
-
Clone the influxdb-client-js repo.
-
Navigate to the
examplesdirectory:cd examples -
Update
./env_browser.jswith your InfluxDB Cloud Dedicated cluster URL, your database name asbucket, an arbitrary string asorg, and your database token. -
Run the following command to start the application at http://localhost:3001/examples/index.html
npm run browserindex.htmlloads theenv_browser.jsconfiguration, the client library ESM modules, and the application in your browser.