Documentation

Get started writing data

This tutorial walks you through the fundamental of creating line protocol data and writing it to InfluxDB.

InfluxDB provides many different options for ingesting or writing data, including the following:

  • Influx user interface (UI)
  • InfluxDB HTTP API
  • influx CLI
  • Telegraf
  • InfluxDB client libraries

If using tools like Telegraf or InfluxDB client libraries, they can build the line protocol for you, but it’s good to understand how line protocol works.

Line protocol

All data written to InfluxDB is written using line protocol, a text-based format that lets you provide the necessary information to write a data point to InfluxDB. This tutorial covers the basics of line protocol, but for detailed information, see the Line protocol reference.

Line protocol elements

Each line of line protocol contains the following elements:

* Required
  • * measurement: String that identifies the measurement to store the data in.
  • tag set: Comma-delimited list of key value pairs, each representing a tag. Tag keys and values are unquoted strings. Spaces, commas, and equal characters must be escaped.
  • * field set: Comma-delimited list of key value pairs, each representing a field. Field keys are unquoted strings. Spaces and commas must be escaped. Field values can be strings (quoted), floats, integers, unsigned integers, or booleans.
  • timestamp: Unix timestamp associated with the data. InfluxDB supports up to nanosecond precision. If the precision of the timestamp is not in nanoseconds, you must specify the precision when writing the data to InfluxDB.

Line protocol element parsing

  • measurement: Everything before the first unescaped comma before the first whitespace.
  • tag set: Key-value pairs between the first unescaped comma and the first unescaped whitespace.
  • field set: Key-value pairs between the first and second unescaped whitespaces.
  • timestamp: Integer value after the second unescaped whitespace.
  • Lines are separated by the newline character (\n). Line protocol is whitespace sensitive.

measurement,tag1=val1,tag2=val2 field1="v1",field2=1i 0000000000000000000


For schema design recommendations, see InfluxDB schema design.

Construct line protocol

With a basic understanding of line protocol, you can now construct line protocol and write data to InfluxDB. Consider a use case where you collect data from sensors in your home. Each sensor collects temperature, humidity, and carbon monoxide readings. To collect this data, use the following schema:

  • measurement: home
    • tags
      • room: Living Room or Kitchen
    • fields
      • temp: temperature in °C (float)
      • hum: percent humidity (float)
      • co: carbon monoxide in parts per million (integer)
    • timestamp: Unix timestamp in second precision

Data is collected hourly beginning at 2022-01-01T08:00:00Z (UTC) until 2022-01-01T20:00:00Z (UTC). The resulting line protocol would look something like the following:

Home sensor data line protocol
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200

Write line protocol to InfluxDB

The following examples show how to write the sample data, already in line protocol format, to an InfluxDB Cloud Serverless bucket.

To learn more about available tools and options, see Write data.

All API, cURL, and client library examples in this getting started tutorial assume your InfluxDB host, organization, url, and token are provided by environment variables.

  1. Go to cloud2.influxdata.com in a browser to log in and access the InfluxDB UI.

  2. Navigate to Load Data > Buckets using the left navigation bar.

  3. Click Add Data on the bucket you want to write the data to and select Line Protocol.

  4. Select Enter Manually.

  5. Important In the Precision drop-down menu above the line protocol text field, select Seconds (to match to precision of the timestamps in the line protocol).

  6. Copy the line protocol above and paste it into the line protocol text field.

  7. Click Write Data.

The UI will confirm that the data has been written successfully.

  1. If you haven’t already, download, install, and configure the influx CLI.

  2. Use the influx write command to write the line protocol above to InfluxDB.

    Provide the following:

influx write \
  --bucket get-started \
  --precision s "
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200
"

Use Telegraf to consume line protocol, and then write it to InfluxDB Cloud Serverless.

  1. If you haven’t already, follow the instructions to download and install Telegraf.

  2. Copy and save the home sensor data sample to a file on your local system–for example, home.lp.

  3. Run the following command to generate a Telegraf configuration file (./telegraf.conf) that enables the inputs.file and outputs.influxdb_v2 plugins:

    telegraf --sample-config \
      --input-filter file \
      --output-filter influxdb_v2 \
      > telegraf.conf
    
  4. In your editor, open ./telegraf.conf and configure the following:

    • file input plugin: In the [[inputs.file]].files list, replace "/tmp/metrics.out" with your sample data filename. If Telegraf can’t find a file when started, it stops processing and exits.

      [[inputs.file]]
        ## Files to parse each interval.  Accept standard unix glob matching rules,
        ## as well as ** to match recursive files and directories.
        files = ["home.lp"]
      
    • output-influxdb_v2 output plugin: In the [[outputs.influxdb_v2]] section, replace the default values with the following configuration for your InfluxDB Cloud Dedicated cluster:

      [[outputs.influxdb_v2]]
        # InfluxDB Cloud Serverless region URL
        urls = ["${INFLUX_URL}"]
      
        # INFLUX_TOKEN is an environment variable you assigned to your API token
        token = "${INFLUX_TOKEN}"
      
        # An empty string (InfluxDB ignores this parameter)
        organization = ""
      
        # Bucket name
        bucket = "get-started"
      

      The example configuration uses the following InfluxDB credentials:

      • urls: an array containing your INFLUX_URL environment variable
      • token: your INFLUX_TOKEN environment variable
      • organization: an empty string (InfluxDB ignores this parameter)
      • bucket: the name of the bucket to write to
  5. To write the data, start the telegraf daemon with the following options:

    • --config: Specifies the filepath of the configuration file.
    • --once: Runs a single Telegraf collection cycle for the configured inputs and outputs, and then exits.

    Enter the following command in your terminal:

    telegraf --once --config ./telegraf.conf
    

    If the write is successful, the output is similar to the following:

    2023-05-31T20:09:08Z D! [agent] Starting service inputs
    2023-05-31T20:09:19Z D! [outputs.influxdb_v2] Wrote batch of 52 metrics in 348.008167ms
    2023-05-31T20:09:19Z D! [outputs.influxdb_v2] Buffer fullness: 0 / 10000 metrics
    

Telegraf and its plugins provide many options for reading and writing data. To learn more, see how to use Telegraf to write data.

To write data to InfluxDB using the InfluxDB HTTP API, send a request to the InfluxDB API /api/v2/write endpoint using the POST request method.

POST http://localhost:8086/api/v2/write

Include the following with your request:

  • Headers:
    • Authorization: Token <INFLUX_TOKEN>
    • Content-Type: text/plain; charset=utf-8
    • Accept: application/json
  • Query parameters:
    • bucket: InfluxDB bucket name
    • precision: timestamp precision (default is ns)
  • Request body: Line protocol as plain text

The following example uses cURL and the InfluxDB v2 API to write line protocol to InfluxDB:

curl --request POST \
"https://cloud2.influxdata.com/api/v2/write?bucket=get-started&precision=s" \
  --header "Authorization: Token $INFLUX_TOKEN" \
  --header "Content-Type: text/plain; charset=utf-8" \
  --header "Accept: application/json" \
  --data-binary "
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200
"

To write data to InfluxDB Cloud Serverless using Python, use the influxdb_client_3 module. The following steps include setting up a Python virtual environment to scope dependencies to your current project.

  1. Create a new module directory and navigate into it–for example:

    mkdir influxdb_py_client && cd $_
    
  2. Setup your Python virtual environment. Inside of your module directory:

    python -m venv envs/virtual-env
    
  3. Activate the virtual environment.

    source ./envs/virtual-env/bin/activate
    
  4. Install the following dependencies:

    • pyarrow
    • influxdb_client_3
    pip install pyarrow influxdb_client_3
    
  5. In your terminal or editor, create a new file for your code–for example: write.py.

    touch write.py
    
  6. Inside of write.py, enter the following sample code:

    from influxdb_client_3 import InfluxDBClient3
    
    # INFLUX_TOKEN is an environment variable you assigned to your
    # API token value.
    token = os.getenv('INFLUX_TOKEN')
    
    # host is the URL without protocol or trailing slash
    client = InfluxDBClient3(
        host='cloud2.influxdata.com',
        org='',
        token=token,
        database='get-started'
    )
    
    lines = [
        "home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000",
        "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000",
        "home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600",
        "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600",
        "home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200",
        "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200",
        "home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800",
        "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800",
        "home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400",
        "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400",
        "home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000",
        "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000",
        "home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600",
        "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600",
        "home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200",
        "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200",
        "home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800",
        "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800",
        "home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400",
        "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400",
        "home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000",
        "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000",
        "home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600",
        "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600",
        "home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200",
        "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200"
    ]
    
    client.write(lines,write_precision='s')
    

    The sample does the following:

    1. Imports the InfluxDBClient3 object from the influxdb_client_3 module.

    2. Calls the InfluxDBClient3() constructor to instantiate an InfluxDB client configured with the following credentials:

      • host: InfluxDB Cloud Serverless region hostname (URL without protocol or trailing slash)
      • org: an empty or arbitrary string (InfluxDB ignores this parameter)
      • token: an InfluxDB API token with write access to the target bucket
      • database: the name of the InfluxDB Cloud Serverless bucket to write to
    3. Defines a list of line protocol strings where each string represents a data record.

    4. Calls the client.write() method with the line protocol record list and write options.

      Because the timestamps in the sample line protocol are in second precision, the example passes the write_precision='s' option to set the timestamp precision to seconds.

  7. To execute the module and write line protocol to your InfluxDB Cloud Serverless bucket, enter the following command in your terminal:

    python write.py
    

To write data to InfluxDB Cloud Serverless using Go, use the influxdb-client-go module.

  1. Inside of your project directory, create a new module directory and navigate into it.

    mkdir influxdb_go_client && cd $_
    
  2. Initialize a new Go module in the directory.

    go mod init influxdb_go_client
    
  3. In your terminal or editor, create a new file for your code–for example: write.go.

    touch write.go
    
  4. Inside of write.go, enter the following sample code:

    package main
    
    import (
      "context"
      "fmt"
      "log"
      "os"
      "time"
    
      influxdb2 "github.com/influxdata/influxdb-client-go/v2"
    )
    
    // Write line protocol data to InfluxDB
    func dbWrite(ctx context.Context) error {
    
      // INFLUX_URL is an environment variable you assigned to your
      // region URL.
      url := os.Getenv("INFLUX_URL")
    
      // INFLUX_TOKEN is an environment variable you assigned to your
      // API token value.
      token := os.Getenv("INFLUX_TOKEN")
      client := influxdb2.NewClientWithOptions(url,
                      token,
                      influxdb2.DefaultOptions().SetPrecision(time.Second))
    
      // Define write API
      org := "ignored"
      bucket := "get-started"
      writeAPI := client.WriteAPIBlocking(org, bucket)
    
      // Define line protocol records to write.
      // Use a raw string literal (denoted by backticks)
      // to preserve backslashes and prevent interpretation
      // of escape sequences--for example, escaped spaces in tag values.
      lines := [...]string{
        `home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000`,
        `home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000`,
        `home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600`,
        `home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600`,
        `home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200`,
        `home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200`,
        `home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800`,
        `home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800`,
        `home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400`,
        `home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400`,
        `home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000`,
        `home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000`,
        `home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600`,
        `home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600`,
        `home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200`,
        `home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200`,
        `home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800`,
        `home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800`,
        `home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400`,
        `home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400`,
        `home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000`,
        `home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000`,
        `home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600`,
        `home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600`,
        `home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200`,
        `home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200`,
      }
    
      // Iterate over the lines array and write each line
      // separately to InfluxDB
    
      for _, lp := range lines {
        err := writeAPI.WriteRecord(context.Background(), lp)
        if err != nil {
          log.Fatalf("Error writing line protocol: %v", err)
        }
      }
    
      fmt.Println("Data has been written successfully.")
      client.Close()
      return nil
    }
    
    // Module main function
    func main() {
      if err := dbWrite(context.Background()); err != nil {
        fmt.Fprintf(os.Stderr, "error: %v\n", err)
        os.Exit(1)
      }
    }
    

    The sample does the following:

    1. Imports required packages.

    2. Defines a dbWrite() function that does the following:

      1. Calls the influxdb2.NewClientWithOptions() with InfluxDB URL, API token, and options to create client.

        Because the timestamps in the sample line protocol are in second precision, the example passes DefaultOptions.SetPrecision(time.Second) for the precision option in order to set the timestamp precision to seconds.

      2. Calls the writeClient.WriteAPIBlocking() method to create a writeAPI client configured to write to the bucket (the method requires an org argument, but InfluxDB ignores it).

      3. Defines an array of line protocol strings where each string represents a data record.

      4. Iterates through the array of line protocol and calls the write client’s WriteRecord() method to write each line of line protocol separately to InfluxDB.

    3. Defines a main function that executes the dbWrite function for the module.

  5. In your terminal, enter the following command to install the packages listed in imports:

    go get ./...
    
  6. To execute the module and write the line protocol to your InfluxDB Cloud Serverless bucket, enter the following command:

    go run ./write.go
    

To write data to InfluxDB Cloud Serverless using Node.js, use the influxdb-client-js package.

  1. Inside of your project directory, create an NPM or Yarn package and install the @influxdata/influxdb-client InfluxDB v2 JavaScript client library.

    npm init -y && npm install --save @influxdata/influxdb-client
    
  2. In your terminal or editor, create a new file for your code–for example: write.js.

    touch write.js
    
  3. Inside of write.js, enter the following sample code:

    'use strict'
    /** @module write
    * Writes line protocol strings to InfluxDB using the Javascript client
    * library with Node.js.
    **/
    import {InfluxDB} from '@influxdata/influxdb-client';
    
    /** Get credentials from environment variables. **/
    const url = process.env.INFLUX_URL;
    const token = process.env.INFLUX_TOKEN;
    const org = process.env.INFLUX_ORG;
    
    /**
    * Instantiate the InfluxDB client with a configuration object.
    **/
    const influxDB = new InfluxDB({url, token});
    
    /** 
    * Define an array of line protocol strings to write.
    * Include an additional backslash to preserve backslashes
    * and prevent interpretation of escape sequences---for example,
    * escaped spaces in tag values.
    */
    const lines = [
      'home,room=Living\\ Room temp=21.1,hum=35.9,co=0i 1641024000',
      'home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000',
      'home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1641027600',
      'home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600',
      'home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1641031200',
      'home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200',
      'home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1641034800',
      'home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800',
      'home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1641038400',
      'home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400',
      'home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1641042000',
      'home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000',
      'home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1641045600',
      'home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600',
      'home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1641049200',
      'home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200',
      'home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1641052800',
      'home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800',
      'home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1641056400',
      'home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400',
      'home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1641060000',
      'home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000',
      'home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1641063600',
      'home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600',
      'home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1641067200',
      'home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200',
    ];
    
    /**
    * Create a write client from the getWriteApi method.
    * Provide your org and bucket.
    **/
    const writeApi = influxDB.getWriteApi(org, 'get-started', 's');
    
    /**
    * Write line protocol to the batch
    */
    writeApi.writeRecords(lines);
    
    /**
    * Flush pending writes from the buffer and close the write client.
    **/
    writeApi.close().then(
      () => {
        console.log('Data has been written successfully.');
      },
      (err) => {
        console.log('Error writing line protocol');
      }
    );
    

    The sample does the following:

    1. Calls the new InfluxDB() constructor to instantiate a client configured with the InfluxDB URL and API token.

    2. Defines an array of line protocol strings where each string represents a data record.

    3. Calls the client’s getWriteApi() method to create a write client configured to write to the bucket (the method requires an org argument, but InfluxDB ignores it).

      Because the timestamps in the sample line protocol are in second precision, the example passes 's' for the precision option in order to set the timestamp precision to seconds.

    4. Calls the write client’s writeRecords() method with the line protocol array to write the records in batches to InfluxDB.

    5. Calls the write client’s close() method with callback functions for success and error. The close() method sends any records remaining in the buffer, executes callbacks, and releases resources.

  4. To execute the file and write the line protocol to your InfluxDB Cloud Serverless bucket, enter the following command in your terminal:

    node write.js
    

If successful, the output is the success message; otherwise, error details and the failure message.

View the written data

Congratulations! You have written data to InfluxDB. With data now stored in InfluxDB, let’s query it.


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB 3.0

The new core of InfluxDB built with Rust and Apache Arrow. Available today in InfluxDB Cloud Dedicated.

Learn more

State of the InfluxDB Cloud Serverless documentation

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.

InfluxDB Cloud Serverless powered by IOx