---
title: Write data with the InfluxDB v2 JavaScript client library
description: The InfluxDB v2 JavaScript client library integrates with Node.js applications to write data to the InfluxDB v2 API.
url: https://docs.influxdata.com/influxdb3/core/reference/client-libraries/v2/javascript/nodejs/write/
estimated_tokens: 1212
product: InfluxDB 3 Core
version: core
publisher: InfluxData
canonical: https://docs.influxdata.com/influxdb3/core/reference/client-libraries/v2/javascript/nodejs/write/
date: '2025-04-09T17:03:44-05:00'
lastmod: '2025-04-09T17:03:44-05:00'
---

#### Use InfluxDB 3 clients to query

InfluxDB 3 supports [compatibility endpoints for *writing data*](/influxdb3/core/write-data/compatibility-apis/) using InfluxDB v2 and v1 tools. However, the `/api/v2/query` API endpoint and associated tooling, such as InfluxDB v2 client libraries and the `influx` CLI, *can’t query* data stored in InfluxDB 3 Core.

[InfluxDB 3 client libraries](/influxdb3/core/reference/client-libraries/v3/) are available that integrate with your code to write and query data stored in InfluxDB 3 Core.

[**Compare tools you can use**](/influxdb3/core/get-started/#tools-to-use) to interact with InfluxDB 3 Core.

Use the [InfluxDB v2 JavaScript client library](https://github.com/influxdata/influxdb-client-js) to write data from a Node.js environment to InfluxDB.

The JavaScript client library includes the following convenient features for writing data to InfluxDB:

* Apply default tags to data points.
* Buffer points into batches to optimize data transfer.
* Automatically retry requests on failure.
* Set an optional HTTP proxy address for your network.

### Before you begin

* [Install the client library and other dependencies](/influxdb3/core/reference/client-libraries/v2/javascript/nodejs/install/).

### Write data with the client library

1. Instantiate a client by calling the `new InfluxDB()` constructor with your InfluxDB URL and database token (environment variables you already set in the [Install section](/influxdb3/core/reference/client-libraries/v2/javascript/nodejs/install/)).

   ```
   import {InfluxDB, Point} from '@influxdata/influxdb-client'

   const influxDB = new InfluxDB({url: process.env.INFLUX_URL,
                                  token: process.env.INFLUX_TOKEN})
   ```

2. Use the `getWriteApi()` method of the client to create a **write client**.
   Provide your InfluxDB organization ID and database name.

   ```
   const writeApi = influxDB.getWriteApi(process.env.INFLUX_ORG,
                                          process.env.INFLUX_DATABASE)
   ```

3. To apply one or more [tags](/influxdb3/core/reference/glossary/#tag) to all points, use the `useDefaultTags()` method.
   Provide tags as an object of key/value pairs.

   ```
   writeApi.useDefaultTags({region: 'west'})
   ```

4. Use the `Point()` constructor to create a [point](/influxdb3/core/reference/glossary/#point).

   1. Call the constructor and provide a [measurement](/influxdb3/core/reference/glossary/#measurement).
   2. To add one or more tags, chain the `tag()` method to the constructor.
      Provide a `name` and `value`.
   3. To add a field of type `float`, chain the `floatField()` method to the constructor.
      Provide a `name` and `value`.

   ```
   const point1 = new Point('temperature')
     .tag('sensor_id', 'TLM010')
     .floatField('value', 24.0)
   ```

5. Use the `writePoint()` method to write the point to your InfluxDB bucket.
   Finally, use the `close()` method to flush all pending writes.
   The example logs the new data point followed by “WRITE FINISHED” to stdout.

   ```
   writeApi.writePoint(point1)

   writeApi.close().then(() => {
     console.log('WRITE FINISHED')
   })
   ```

### Complete example

```js
'use strict'
/** @module write
 * Writes a data point to InfluxDB using the JavaScript client library with Node.js.
**/

import {InfluxDB, Point} from '@influxdata/influxdb-client'

/**
 * Instantiate the InfluxDB client
 * with a configuration object.
 **/
const influxDB = new InfluxDB({url: process.env.INFLUX_URL,
                              token: process.env.INFLUX_TOKEN})

/**
 * Create a write client from the getWriteApi method.
 * Provide your org and database.
 **/
const writeApi = influxDB.getWriteApi(process.env.INFLUX_ORG,
                                      process.env.INFLUX_DATABASE)

/**
 * Apply default tags to all points.
 **/
writeApi.useDefaultTags({region: 'west'})

/**
 * Create a point and write it to the buffer.
 **/
const point1 = new Point('temperature')
  .tag('sensor_id', 'TLM01')
  .floatField('value', 24.0)
console.log(` ${point1}`)

writeApi.writePoint(point1)

/**
 * Flush pending writes and close writeApi.
 **/
writeApi.close().then(() => {
  console.log('WRITE FINISHED')
})
```

In your terminal with [environment variables or `env.js` set](/influxdb3/core/reference/client-libraries/v2/javascript/nodejs/install/#configure-credentials), run the following command to execute the JavaScript file:

```sh
node write.js
```

### Response codes

*For information about **InfluxDB API response codes**, see[InfluxDB API Write documentation](/influxdb3/core/api/write-data/#operation/PostWrite).*

#### Related

* [Troubleshoot issues writing data](/influxdb3/core/write-data/troubleshoot/)

[client libraries](/influxdb3/core/tags/client-libraries/)[JavaScript](/influxdb3/core/tags/javascript/)
