Documentation

Execute Flux queries

This page documents an earlier version of InfluxDB OSS. InfluxDB OSS v2 is the latest stable version. See the equivalent InfluxDB v2 documentation: Execute queries.

There are multiple ways to execute Flux queries with InfluxDB and Chronograf v1.8+. This guide covers the different options:

  1. Chronograf’s Data Explorer
  2. Influx CLI
  3. InfluxDB API

Before attempting these methods, make sure Flux is enabled by setting flux-enabled = true in the [http] section of your InfluxDB configuration file.

Chronograf’s Data Explorer

Chronograf v1.8+ supports Flux in its Data Explorer. Flux queries can be built, executed, and visualized from within the Chronograf user interface.

Influx CLI

To start an interactive Flux read-eval-print-loop (REPL) with the InfluxDB 1.8+ influx CLI, run the influx command with the following flags:

  • -type=flux
  • -path-prefix=/api/v2/query

If authentication is enabled on your InfluxDB instance, use the -username flag to provide your InfluxDB username and the -password flag to provide your password.

Enter an interactive Flux REPL
influx -type=flux -path-prefix=/api/v2/query
  • Copy
  • Fill window
influx -type=flux \
  -path-prefix=/api/v2/query \
  -username myuser \
  -password PasSw0rd
  • Copy
  • Fill window

Any Flux query can be executed within the REPL.

Submit a Flux query via parameter

Flux queries can also be passed to the Flux REPL as a parameter using the influx CLI’s -type=flux option and the -execute parameter. The accompanying string is executed as a Flux query and results are output in your terminal.

influx -type=flux \
  -path-prefix=/api/v2/query \
  -execute '<flux query>'
  • Copy
  • Fill window
influx -type=flux \
  -path-prefix=/api/v2/query \
  -username myuser \
  -password PasSw0rd \
  -execute '<flux query>'
  • Copy
  • Fill window

Submit a Flux query via via STDIN

Flux queries an be piped into the influx CLI via STDIN. Query results are otuput in your terminal.

echo '<flux query>' | influx -type=flux -path-prefix=/api/v2/query
  • Copy
  • Fill window
echo '<flux query>' | influx -type=flux \
  -path-prefix=/api/v2/query \
  -username myuser \
  -password PasSw0rd
  • Copy
  • Fill window

InfluxDB API

Flux can be used to query InfluxDB through InfluxDB’s /api/v2/query endpoint. Queried data is returned in annotated CSV format.

In your request, set the following:

  • Accept header to application/csv
  • Content-type header to application/vnd.flux
  • If authentication is enabled on your InfluxDB instance, Authorization header to Token <username>:<password>

This allows you to POST the Flux query in plain text and receive the annotated CSV response.

Below is an example curl command that queries InfluxDB using Flux:

curl -XPOST localhost:8086/api/v2/query -sS \
  -H 'Accept:application/csv' \
  -H 'Content-type:application/vnd.flux' \
  -d 'from(bucket:"telegraf")
        |> range(start:-5m)
        |> filter(fn:(r) => r._measurement == "cpu")'
  • Copy
  • Fill window
curl -XPOST localhost:8086/api/v2/query -sS \
  -H 'Accept:application/csv' \
  -H 'Content-type:application/vnd.flux' \
  -H 'Authorization: Token <username>:<password>' \
  -d 'from(bucket:"telegraf")
        |> range(start:-5m)
        |> filter(fn:(r) => r._measurement == "cpu")'
  • Copy
  • Fill window

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.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: