content/shared/influxdb-v2/api-guide/client-libraries/nodejs/write.md
Use the InfluxDB JavaScript client library to write data from a Node.js environment to InfluxDB.
The JavaScript client library includes the following convenient features for writing data to InfluxDB:
Instantiate an InfluxDB client. Provide your InfluxDB URL and API token.
import {InfluxDB, Point} from '@influxdata/influxdb-client'
const influxDB = new InfluxDB({YOUR_URL, YOUR_API_TOKEN})
Replace the following:
YOUR_URL: InfluxDB URLYOUR_API_TOKEN: InfluxDB API tokenUse the getWriteApi() method of the client to create a write client.
Provide your InfluxDB organization ID and bucket name.
const writeApi = influxDB.getWriteApi(YOUR_ORG, YOUR_BUCKET)
Replace the following:
YOUR_ORG: InfluxDB organization IDYOUR_BUCKET: InfluxDB bucket nameTo apply one or more tags to all points, use the useDefaultTags() method.
Provide tags as an object of key/value pairs.
writeApi.useDefaultTags({region: 'west'})
Use the Point() constructor to create a point.
tag() method to the constructor.
Provide a name and value.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)
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')
})
{{< code-tabs-wrapper >}} {{% code-tabs %}} Curl Node.js {{% /code-tabs %}} {{% code-tab-content %}}
{{< get-shared-text "api/v2.0/write/write.sh" >}}
{{% /code-tab-content %}} {{% code-tab-content %}}
{{< get-shared-text "api/v2.0/write/write.mjs" >}}
{{% /code-tab-content %}} {{< /code-tabs-wrapper >}}
To run the example from a file, set your InfluxDB environment variables and use node to execute the JavaScript file.
export INFLUX_URL=http://localhost:8086 && \
export INFLUX_TOKEN=YOUR_API_TOKEN && \
export INFLUX_ORG=YOUR_ORG && \
export INFLUX_BUCKET=YOUR_BUCKET && \
node write.js
For information about InfluxDB API response codes, see InfluxDB API Write documentation.