---
title: Query with the InfluxDB API
description: Use the InfluxDB API to query InfluxDB data.
url: https://docs.influxdata.com/influxdb/cloud/query-data/execute-queries/influx-api/
estimated_tokens: 4323
product: InfluxDB Cloud (TSM)
version: cloud
---

# Query with the InfluxDB API

The [InfluxDB v2 API](/influxdb/cloud/reference/api) provides a programmatic interface for all interactions with InfluxDB. To query InfluxDB Cloud, do one of the following:

-   [Send a Flux query request](#send-a-flux-query-request)
-   [Send an InfluxQL query request](#send-an-influxql-query-request)

## Send a Flux query request

Send a Flux query request to the following endpoint:

[POST /api/v2/query](/influxdb/cloud/api/query-data/)

In your request, set the following:

-   Your organization via the `org` or `orgID` URL parameters
-   Headers:
    -   `Authorization: Token <API_TOKEN>`
    -   `Accept: application/csv`
    -   `Content-type: application/vnd.flux`
-   Your Flux query text in the request body

#### Use gzip to compress a large query response

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

We recommend only using gzip compression on responses that are larger than 1.4 KB. If the response is smaller than 1.4 KB, gzip encoding will always return a 1.4 KB response, despite the uncompressed response size. 1500 bytes (~1.4 KB) is the maximum transmission unit (MTU) size for the public network and is the largest packet size allowed at the network layer.

#### Flux - Example query request

The following example shows how to use cURL to send a Flux query to InfluxDB Cloud:

<!-- Tabbed content: Select one of the following options -->

**Without compression:**

```bash
curl \
  --request POST \
  http://cloud2.influxdata.com/api/v2/query?orgID=ORG_ID  \
  --header 'Authorization: Token API_TOKEN' \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/vnd.flux' \
  --data 'from(bucket:"BUCKET_NAME")
        |> range(start: -12h)
        |> filter(fn: (r) => r._measurement == "example-measurement")
        |> aggregateWindow(every: 1h, fn: mean)'
```

**With compression:**

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

<!-- End tabbed content -->

Replace the following with your values:

-   `ORG_ID` - the ID of the [organization](/influxdb/cloud/admin/organizations/) that owns the bucket.
-   `API_TOKEN` - your [token](/influxdb/cloud/admin/tokens/).
-   `BUCKET_NAME` - the name of the [bucket](/influxdb/cloud/admin/buckets/) to query.

## Send an InfluxQL query request

To query InfluxDB Cloud using the [InfluxQL query language](/influxdb/v2/reference/syntax/influxql/), send a request to the v1-compatible API endpoint:

[GET /query](/influxdb/v2/api/query-data-v1-compatible/#operation/GetLegacyQuery)

[POST /query](/influxdb/v2/api/query-data-v1-compatible/#operation/PostQueryV1)

In your request, set the following:

-   [1.x-compatible or 2.x authentication](/influxdb/v2/api-guide/influxdb-1x/#authentication) credentials
-   Headers:
    -   `Accept: application/csv` or `Accept: application/json`
    -   `Content-type: application/vnd.influxql`
-   The database and retention policy mapped to the bucket you want to query
-   Your InfluxQL query text

If you have an existing bucket that doesn’t follow the **database/retention-policy** naming convention, you **must** [manually create a database and retention policy mapping](/influxdb/v2/query-data/influxql/dbrp/#create-dbrp-mappings) to query that bucket with the `/query` compatibility API. Use the `db` and `rp` query parameters to specify the database and retention policy for the bucket you want to query.

#### InfluxQL - Example query request

The following example shows how to use cURL to send an InfluxQL query to InfluxDB Cloud using v1-compatible authentication:

<!-- Tabbed content: Select one of the following options -->

**HTTP POST:**

```bash
# 1.x compatible POST request using Basic authentication and InfluxQL
curl --request POST \
  "http://cloud2.influxdata.com/query?db=BUCKET_NAME&p=API_TOKEN&u=ignored" \
  --header "Content-type: application/vnd.influxql" \
  --data "SELECT * FROM home WHERE time > now() - 1h"
```

**HTTP GET:**

```bash
# 1.x compatible GET request using Basic authentication and InfluxQL
curl --get "http://cloud2.influxdata.com/query" \
  --header "Accept: application/json" \
  --data-urlencode "q=SELECT * FROM home WHERE time > now() - 1h" \
  --data-urlencode "db=BUCKET_NAME" \
  --data-urlencode "u=ignored" \
  --data-urlencode "p=API_TOKEN"
```

<!-- End tabbed content -->

Replace the following with your values:

-   `API_TOKEN` - your [token](/influxdb/cloud/admin/tokens/).
-   `BUCKET_NAME` - the name of the [bucket](/influxdb/cloud/admin/buckets/) to query.

<!-- Tabbed content: Select one of the following options -->

**Without compression:**

```bash
curl --get "http://cloud2.influxdata.com/query" \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/json' \
  --data-urlencode "db=BUCKET_NAME" \
  --data-urlencode "p=API_TOKEN" \
  --data-urlencode "u=ignored" \
  --data-urlencode "q=SELECT used_percent FROM example-db.example-rp.example-measurement WHERE host=host1"
```

**With compression:**

```bash
curl --get "http://cloud2.influxdata.com/query" \
  --header 'Accept: application/csv' \
  --header 'Content-type: application/json' \
  --header 'Accept-Encoding: gzip' \
  --data-urlencode "db=BUCKET_NAME" \
  --data-urlencode "p=API_TOKEN" \
  --data-urlencode "u=ignored" \
  --data-urlencode "q=SELECT used_percent FROM example-db.example-rp.example-measurement WHERE host=host1"
```

<!-- End tabbed content -->

Replace the following with your values:

-   `API_TOKEN` - your [token](/influxdb/cloud/admin/tokens/).
-   `BUCKET_NAME` - the name of the [bucket](/influxdb/cloud/admin/buckets/) to query.

InfluxDB returns the query results in [annotated CSV](/influxdb/cloud/reference/syntax/annotated-csv/).

[query](/influxdb/cloud/tags/query/)
