Documentation

Write data with the InfluxDB API

See the equivalent InfluxDB v2 documentation: Write data to InfluxDB.

Write data into InfluxDB using the command line interface, client libraries, and plugins for common data formats such as Graphite.

Note: The following examples use curl, a command line tool that transfers data using URLs. Learn the basics of curl with the HTTP Scripting Guide.

Create a database using the InfluxDB API

To create a database, send a POST request to the /query endpoint and set the URL parameter q to CREATE DATABASE <new_database_name>.

The following example shows how to send a request to InfluxDB running on localhost to create the mydb database:

curl -i -XPOST http://localhost:8086/query \
--data-urlencode "q=CREATE DATABASE mydb"

Write data using the InfluxDB API

The InfluxDB API is the primary means of writing data into InfluxDB.

  • To write to a database using the InfluxDB API, send a POST request to the /write endpoint and include the following:

    • db=DATABASE_NAME query parameter: Specifies the database to write data to.
    • rp=RETENTION_POLICY query parameter: Optional. If set, InfluxDB uses the specified retention policy; otherwise, uses the default retention policy.
    • A request body that contains time series data in InfluxDB line protocol format.

    For a complete list of the available query parameters, see the InfluxDB API Reference documentation.

    The following example shows how to write a single point to the mydb database:

    curl -i -XPOST 'http://localhost:8086/write?db=mydb' \
    --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
    

    The following example shows how to write a point to the mydb database and the 90d retention policy:

    curl -i -XPOST 'http://localhost:8086/write?db=mydb&rp=90d' \
    --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
    
  • To write to a database using the InfluxDB 2.0 API (compatible with InfluxDB 1.8+), send a POST request to the /api/v2/write endpoint and include the ?bucket=DATABASE_NAME/RETENTION_POLICY query parameter–for example:

    curl -i -XPOST 'http://localhost:8086/api/v2/write?bucket=db/rp&precision=ns' \
    --header 'Authorization: Token username:password' \
    --data-raw 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
    

The preceding examples use a curl --data-<format> option to include a POST request body that contains InfluxDB line protocol for the time series data that you want to store.

// Syntax
<measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]

// Example
cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000

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 tag 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, booleans. Default is float.
  • timestamp: Unix timestamp associated with the data. InfluxDB supports up to nanosecond precision. If the precision of the timestamp in your data is not in nanoseconds, specify the precision when writing the data to InfluxDB. For more information, see line protocol data types.

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

Note: Avoid using the following reserved keys: _field, _measurement, and time. If reserved keys are included as a tag or field key, the associated point is discarded.

Configure gzip compression

InfluxDB supports gzip compression to reduce the bandwidth consumed during API requests.

  • To accept compressed data from InfluxDB, include the Accept-Encoding: gzip header in your InfluxDB API request.

  • When sending gzip-compressed data to InfluxDB, include the Content-Encoding: gzip header in your InfluxDB API request.

    For example, to use curl to write compressed data to InfluxDB, do the following:

    1. Use gzip to create a file that contains compressed line protocol–for example, enter the following command in your terminal:

      echo "mem,host=host1 used_percent=23.43234543 1641024000
      mem,host=host2 used_percent=26.81522361 1641027600
      mem,host=host1 used_percent=22.52984738 1641031200
      mem,host=host2 used_percent=27.18294630 1641034800" | gzip > system.gzip
      
    2. In your curl command, include the Content-Encoding: gzip header and the --data-binary <FILE> option–for example:

      curl "http://localhost:8086/write?db=mydb&rp=90d&precision=s" \
      --header "Content-Type: text/plain; charset=utf-8" \
      --header "Content-Encoding: gzip" \
      --data-binary @system.gzip
      

For details about enabling gzip for client libraries, see the client library documentation.

Enable gzip compression in the Telegraf InfluxDB output plugin

  1. In your editor, open the telegraf.conf Telegraf configuration file and find [[outputs.influxdb]].
  2. In the [[outputs.influxdb]] section, replace content_encoding = "identity" (default) with content_encoding = "gzip".

Note Writes to InfluxDB 2.x [[outputs.influxdb_v2]] are configured to compress content in gzip format by default.

Writing multiple points

Post multiple points to multiple series at the same time by separating each point with a new line. Batching points in this manner results in much higher performance.

The following example writes three points to the database mydb. The first point belongs to the series with the measurement cpu_load_short and tag set host=server02 and has the server’s local timestamp. The second point belongs to the series with the measurement cpu_load_short and tag set host=server02,region=us-west and has the specified timestamp 1422568543702900257. The third point has the same specified timestamp as the second point, but it is written to the series with the measurement cpu_load_short and tag set direction=in,host=server01,region=us-west.

curl -i -XPOST 'http://localhost:8086/write?db=mydb' \
--data-binary 'cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'

Writing points from a file

Write points from a file by passing @filename to curl. The data in the file should follow the InfluxDB line protocol syntax.

Example of a properly formatted file (cpu_data.txt):

cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257

Write the data in cpu_data.txt to the mydb database with:

curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt

Note: If your data file has more than 5000 points, it may be necessary to split that file into several files to write your data in batches to InfluxDB. By default, the HTTP request times out after five seconds. InfluxDB still attempts to write the points after the time out, but the server doesn’t send a confirmation that they were successfully written.

Schemaless Design

InfluxDB is a schemaless database. You can add new measurements, tags, and fields at any time. Note that if you attempt to write data with a different type than previously used (for example, writing a string to a field that previously accepted integers), InfluxDB rejects those points.

A note on REST

InfluxDB uses HTTP as a convenient and widely supported data transfer protocol.

Modern web APIs use REST because it addresses a common need. As the number of endpoints grows, the need for an organizing system becomes pressing. REST is a pattern for organizing large numbers of endpoints and interacting with resources. This pattern provides consistency and predictability for those designing and consuming the API: everyone knows what to expect.

The InfluxDB v1 API is simple by design and implements certain features of REST, such as standard HTTP headers and methods to perform operations. However, the v1 API architecture doesn’t aim to be completely RESTful.

HTTP response summary

  • 2xx: Data was successfully written; status code is HTTP 204 No Content.
  • 4xx: InfluxDB could not understand the request.
  • 5xx: The system is overloaded or significantly impaired.

Examples

Writing a float to a field that previously accepted booleans
curl -i -XPOST 'http://localhost:8086/write?db=hamlet' \
--data-binary 'tobeornottobe booleanonly=true'

curl -i -XPOST 'http://localhost:8086/write?db=hamlet' \
--data-binary 'tobeornottobe booleanonly=5'

returns:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Request-Id: [...]
X-Influxdb-Version: 1.11.5
Date: Wed, 01 Mar 2017 19:38:01 GMT
Content-Length: 150

{"error":"field type conflict: input field \"booleanonly\" on measurement \"tobeornottobe\" is type float, already exists as type boolean dropped=1"}
Writing a point to a database that doesn’t exist
curl -i -XPOST 'http://localhost:8086/write?db=atlantis' \
--data-binary 'liters value=10'

returns:

HTTP/1.1 404 Not Found
Content-Type: application/json
Request-Id: [...]
X-Influxdb-Version: 1.11.5
Date: Wed, 01 Mar 2017 19:38:35 GMT
Content-Length: 45

{"error":"database not found: \"atlantis\""}

Next steps

Learn how to query your data stored in InfluxDB with the Querying data guide! To learn more about using the InfluxDB API, see the InfluxDB API reference.


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.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following: