Query with the InfluxDB API

The InfluxDB v2 API provides a programmatic interface for all interactions with InfluxDB. To query InfluxDB 2.0, 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 authentication 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.0:

Without compression With compression

  1. curl --request POST \
  2. http://localhost:8086/api/v2/query?org=my-org \
  3. --header 'Authorization: Token YOURAUTHTOKEN' \
  4. --header 'Accept: application/csv' \
  5. --header 'Content-type: application/vnd.flux' \
  6. --data 'from(bucket:"example-bucket")
  7. |> range(start: -12h)
  8. |> filter(fn: (r) => r._measurement == "example-measurement")
  9. |> aggregateWindow(every: 1h, fn: mean)'
  1. curl --request POST \
  2. http://localhost:8086/api/v2/query?org=my-org \
  3. --header 'Authorization: Token YOURAUTHTOKEN' \
  4. --header 'Accept: application/csv' \
  5. --header 'Content-type: application/vnd.flux' \
  6. --header 'Accept-Encoding: gzip' \
  7. --data 'from(bucket:"example-bucket")
  8. |> range(start: -12h)
  9. |> filter(fn: (r) => r._measurement == "example-measurement")
  10. |> aggregateWindow(every: 1h, fn: mean)'

InfluxQL - Example query request

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

Without compression With compression

  1. curl --request -G http://localhost:8086/query?org=my-org?database=MyDB&retention_policy=MyRP \
  2. --header 'Authorization: Token YOURAUTHTOKEN' \
  3. --header 'Accept: application/csv' \
  4. --header 'Content-type: application/json' \
  5. --data-urlencode "q=SELECT used_percent FROM example-db.example-rp.example-measurement WHERE host=host1"
  1. curl --request -G http://localhost:8086/query?org=my-org?database=MyDB&retention_policy=MyRP \
  2. --header 'Authorization: Token YOURAUTHTOKEN' \
  3. --header 'Accept: application/csv' \
  4. --header 'Content-type: application/json' \
  5. --header 'Accept-Encoding: gzip' \
  6. --data-urlencode "q=SELECT used_percent FROM example-db.example-rp.example-measurement WHERE host=host1"

InfluxDB returns the query results in annotated CSV.

query