Documentation

Query with the InfluxDB API

This page documents an earlier version of InfluxDB. InfluxDB v2.2 is the latest stable version. View this page in the v2.2 documentation.

The InfluxDB v2 API provides a programmatic interface for all interactions with InfluxDB. To query InfluxDB 2.1, do one of the following:

In your request, set the following:

  • Your organization via the org or orgID URL parameters.
  • Authorization header to Token + your API token.
  • Accept header to application/csv.
  • Content-type header to application/vnd.flux (Flux only) or application/json (Flux or InfluxQL).
  • Query in Flux or InfluxQL with the request’s raw data.

Use gzip to compress the query response

To compress the query response, set the Accept-Encoding header to gzip. This saves network bandwidth, but increases server-side load.

Flux - Example query request

Below is an example curl request that sends a Flux query to InfluxDB 2.1:

curl --request POST \
  http://localhost:8086/api/v2/query?orgID=INFLUX_ORG_ID  \
  --header 'Authorization: Token INFLUX_TOKEN' \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/vnd.flux' \
  --data 'from(bucket:"example-bucket")
        |> range(start: -12h)
        |> filter(fn: (r) => r._measurement == "example-measurement")
        |> aggregateWindow(every: 1h, fn: mean)'
curl --request POST \
  http://localhost:8086/api/v2/query?orgID=INFLUX_ORG_ID \
  --header 'Authorization: Token INFLUX_TOKEN' \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/vnd.flux' \
  --header 'Accept-Encoding: gzip' \
  --data 'from(bucket:"example-bucket")
        |> range(start: -12h)
        |> filter(fn: (r) => r._measurement == "example-measurement")
        |> aggregateWindow(every: 1h, fn: mean)'

InfluxQL - Example query request

Below is an example curl request that sends an InfluxQL query to InfluxDB 2.1:

curl --request -G http://localhost:8086/query?orgID=INFLUX_ORG_ID&database=MyDB&retention_policy=MyRP \
  --header 'Authorization: Token INFLUX_TOKEN' \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/json' \
  --data-urlencode "q=SELECT used_percent FROM example-db.example-rp.example-measurement WHERE host=host1"
curl --request -G http://localhost:8086/query?orgID=INFLUX_ORG_ID&database=MyDB&retention_policy=MyRP \
  --header 'Authorization: Token INFLUX_TOKEN' \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/json' \
  --header 'Accept-Encoding: gzip' \
  --data-urlencode "q=SELECT used_percent FROM example-db.example-rp.example-measurement WHERE host=host1"

InfluxDB returns the query results in annotated CSV.


Set your InfluxDB URL

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.