# Querying Last9 using HTTP API

> How to query metrics from Last9 using HTTP API

Source: https://last9.io/docs/http-api/

This step-by-step guide explains how to query Last9 using HTTP API.

## Last9 Read URL

Create a Last9 cluster by following [Getting Started](/docs/onboard/).
Each Last9 Cluster comes with a Read URL which needs to be used when querying metrics data
from Last9.

![Last9 Read Data Settings](../../../../../assets/content/docs/levitate/guides/querying-levitate-using-http/levitate-cluster-read-data-settings-tab.png)

You can grab the Read URL by going to the Last9 Cluster → Settings → Read Data → Bring Your Own Visualization.

:::note
Last9 Read URL mandates authenticated access. Create a Read Token for your
cluster and use it as `password`. Use the cluster id as `username`.
:::

Keep the following information handy after creating the Last9 cluster:

- `$levitate_read_url` - Last9's Read endpoint
- `$levitate_username` - Cluster ID
- `$levitate_password` - Read token created for the cluster

## Authentication

Generate the authorization header for authenticated access to Last9 metrics API as follows.

```bash
USERNAME="$levitate_ cluster_username"
PASSWORD="$levitate_cluster_password"
BASIC_AUTH_HEADER=$(echo -n "$USERNAME:$PASSWORD" | base64)
AUTH_HEADER="Authorization: Basic $BASIC_AUTH_HEADER"
```

:::note
Last9 is Prometheus compatible TSDB. You can query metrics stored in Last9 using Prometheus HTTP API.
:::

## Instant Query

The simplest way to query Last9 is to use `$levitate_read_url` along with `$AUTH_HEADER`, which allows you to execute an instant query.

```bash
curl -XPOST "$levitate_read_url/api/v1/query?query=<promQL>" -H "$AUTH_HEADER"
```

**Example**:

Query the current CPU usage:

```bash
curl -XPOST "$levitate_read_url/api/v1/query?query=node_cpu_seconds_total{}" -H "$AUTH_HEADER"
```

## Range Query

Use the `query_range` endpoint to retrieve data over a time range. You need to specify the start time, end time, and step duration.

```bash
curl -XPOST '$levitate_read_url/api/v1/query_range?query=<promQL>&start=<start-time>&end=<end-time>&step=<step-duration>' -H "$AUTH_HEADER"
```

**Example**:

Query CPU usage over the last hour with a 1-minute step:

```bash
curl -XPOST '$levitate_read_url/api/v1/query_range?query=node_cpu_seconds_total&start=$(date -d "1 hour ago" +%s)&end=$(date +%s)&step=60' -H "$AUTH_HEADER"
```

:::tip
For more details, refer to the [Prometheus HTTP API documentation](https://prometheus.io/docs/prometheus/latest/querying/api/#http-api).
:::

---

## Troubleshooting

Please get in touch with us on [Discord](https://discord.com/invite/Q3p2EEucx9) or [Email](mailto:support@last9.io) if you have any questions.
