DEV Community

Alex Spinov
Alex Spinov

Posted on

Prometheus Has a Free Query API That Most Developers Overlook

Prometheus, the open-source monitoring toolkit, ships with a powerful HTTP API that lets you query metrics, manage alerts, and inspect targets — all without installing extra tools.

The PromQL Query API

The core endpoint is simple:

curl http://localhost:9090/api/v1/query?query=up
Enter fullscreen mode Exit fullscreen mode

This returns the current value of the up metric for all targets. But the real power is in range queries:

curl "http://localhost:9090/api/v1/query_range?query=rate(http_requests_total[5m])&start=2026-03-29T00:00:00Z&end=2026-03-29T12:00:00Z&step=60s"
Enter fullscreen mode Exit fullscreen mode

Key Endpoints You Should Know

1. Instant Query

GET /api/v1/query?query=<PromQL>&time=<timestamp>
Enter fullscreen mode Exit fullscreen mode

2. Range Query

GET /api/v1/query_range?query=<PromQL>&start=<ts>&end=<ts>&step=<duration>
Enter fullscreen mode Exit fullscreen mode

3. Series Metadata

GET /api/v1/series?match[]=<selector>
Enter fullscreen mode Exit fullscreen mode

4. Label Discovery

GET /api/v1/labels
GET /api/v1/label/<name>/values
Enter fullscreen mode Exit fullscreen mode

5. Targets and Alerts

GET /api/v1/targets
GET /api/v1/alerts
GET /api/v1/rules
Enter fullscreen mode Exit fullscreen mode

Building a Custom Dashboard

const PROM_URL = "http://localhost:9090";

async function queryPrometheus(promql) {
  const res = await fetch(
    `${PROM_URL}/api/v1/query?query=${encodeURIComponent(promql)}`
  );
  const data = await res.json();
  return data.data.result;
}

const cpuUsage = await queryPrometheus(
  '100 - (avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)'
);

cpuUsage.forEach(series => {
  console.log(`${series.metric.instance}: ${parseFloat(series.value[1]).toFixed(1)}% CPU`);
});
Enter fullscreen mode Exit fullscreen mode

Why This Matters

Most teams only interact with Prometheus through Grafana. But the HTTP API enables:

  • Custom alerting logic beyond what PromQL rules support
  • Automated capacity reports pulled directly from metrics
  • CI/CD gates that check performance metrics before deployment
  • Cost optimization by querying resource utilization programmatically

Need custom monitoring dashboards or automated metric analysis? I build data tools for developers. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.

Top comments (0)