Analytics

Analytics API: How to Serve Governed Metrics to Any Consumer

You have metrics. Revenue, active users, churn, usage per customer. They live in your data warehouse. Now you need to serve them to multiple consumers: your product’s frontend, your customers’ integrations, an AI agent, a scheduled report generator.

The first instinct is custom API endpoints. One endpoint for revenue by region. Another for churn by plan. Another for the customer dashboard. Each endpoint has its own SQL query, its own response format, its own maintenance burden. By the twentieth endpoint, you’re running a bespoke analytics service.

An analytics API backed by a semantic layer gives you one query interface that serves every consumer. Define metrics once. Query them from anywhere.

What is an analytics API?

An analytics API is a programmatic interface for querying metrics. Instead of connecting to a database and writing SQL, consumers call an API endpoint with the metrics they want and get structured results back.

curl https://analytics.example.com/v1/query \
  -H "Authorization: Bearer bon_pk_..." \
  -d '{
    "measures": ["orders.total_revenue", "orders.order_count"],
    "dimensions": ["orders.region"],
    "timeDimensions": [{
      "dimension": "orders.created_at",
      "granularity": "month",
      "dateRange": ["2026-01-01", "2026-03-31"]
    }],
    "orderBy": { "orders.total_revenue": "desc" }
  }'

The response is structured JSON:

{
  "data": [
    { "orders.region": "EMEA", "orders.total_revenue": 142000, "orders.order_count": 340, "orders.created_at": "2026-01-01" },
    { "orders.region": "APAC", "orders.total_revenue": 98000, "orders.order_count": 220, "orders.created_at": "2026-01-01" }
  ]
}

Every consumer uses the same interface. The dashboard frontend, the customer’s API integration, the scheduled report, and the AI agent all query the same endpoint with the same metric definitions. The analytics API handles query generation, execution, caching, and access control.

Why not just build API endpoints?

Custom endpoints work at small scale. They break at medium scale:

Metric drift. /api/revenue and /api/dashboard/revenue both return “revenue” but use different SQL queries. One gets updated. The other doesn’t.

N+1 endpoint problem. Every new metric or dimension combination is a new endpoint. Revenue by region. Revenue by plan. Revenue by region AND plan. Revenue by region AND plan AND month. The combinatorial explosion is real.

No caching strategy. Each endpoint hits the warehouse on every request. Response times grow with data volume. You end up building a caching layer per endpoint.

No access control. Each endpoint implements its own auth. Customer A’s key works on all endpoints or none. Per-tenant, per-metric access control is custom code.

A shared query interface handles all of this. But there’s a gap that shows up once AI agents become consumers: an agent that fetches rows still has to present them. A wall of JSON tells the user nothing. Agents need to chart query results, not just fetch them.

Charting the results an agent fetches

Fetching rows is half the job. The other half is rendering them so a person can read the answer. That’s where @bonnard/mcp-charts fits: a visualize tool you add to your MCP server that renders interactive charts from your query results.

Add it to your MCP server

npm install @bonnard/mcp-charts
import { addCharts } from "@bonnard/mcp-charts";

// runSql runs against your warehouse and returns typed rows
addCharts(server, { runSql });

addCharts registers a visualize tool (and a visualize_read_me companion) on your server. Bonnard never touches your database. Your runSql does, against Postgres, BigQuery, Snowflake, Databricks, DuckDB, or any source you wire up.

How the flow works

  1. The agent calls visualize with a query.
  2. Your runSql returns rows.
  3. Bonnard infers the chart from the typed result: line, bar, area, pie, scatter, funnel, waterfall, or table.
  4. The host renders an interactive ui:// widget inside Claude or ChatGPT.

The agent gets line, bar, and area charts for trends, scatter for distributions, funnel and waterfall for staged data, pie for shares, and a table when a table is the right answer. Axes, formatting, and gap-fill are decided from the typed schema, so the same query produces the same chart every time.

One query interface for the numbers. One visualize tool for the picture.

Analytics API vs alternatives

Approach Metric governance Multi-tenant Caching Maintenance
Custom REST endpoints None (per-endpoint SQL) DIY DIY High (per endpoint)
GraphQL API Schema-level DIY Per-resolver Medium-high
Direct warehouse access None Manual None Low (but dangerous)
BI tool API (Looker, Metabase) Tool-specific Tool-specific Tool-specific Medium
Bonnard Inherits your runSql definitions Inherits your runSql Inherits your warehouse Low (a few lines on your server). Adds interactive charts via the MCP visualize tool, rendered in Claude/ChatGPT from query results

When you need an analytics API

You need one when:

  • Multiple consumers query the same metrics (frontend + backend + AI agents)
  • Customers integrate with your data via API
  • You’re building embedded analytics with a frontend SDK
  • AI agents need programmatic access to governed data
  • Your data team is tired of building custom endpoints

You don’t need one when:

  • A single dashboard covers all use cases
  • Nobody queries your metrics programmatically
  • The data model is so simple that direct SQL is fine

Getting started

Once your analytics API can fetch rows, add a visualize tool so agents can chart them:

npm install @bonnard/mcp-charts
import { addCharts } from "@bonnard/mcp-charts";

addCharts(server, { runSql });

That registers the visualize tool on your MCP server. Agents call it with a query, your runSql returns rows, and Bonnard renders an interactive chart inside Claude or ChatGPT. Adapters ship for Postgres, BigQuery, Snowflake, Databricks, and DuckDB, or pass your own runSql.

For the full walkthrough: Add Interactive Charts to Your MCP Server. For the design choices behind the tool: How Bonnard Builds Agent-Friendly MCPs.

Source is on GitHub.

Frequently asked questions

What is an analytics API?

An analytics API is a programmatic interface for querying business metrics. Instead of writing SQL against a database, consumers call an API with the measures, dimensions, and filters they want. The API handles query generation, caching, and access control.

How is an analytics API different from a REST API?

A generic REST API exposes resources (users, orders, invoices). An analytics API exposes metrics (revenue, churn rate, active users) with aggregation, filtering, and time dimensions built in. The query interface is designed for analytical questions, not CRUD operations.

Do I need an analytics API for AI agents?

If AI agents query your data, an analytics API is the governed path. The alternative is giving agents direct database access (dangerous) or text-to-SQL (inconsistent). An analytics API backed by a semantic layer gives agents governed access to metric definitions. See What Is an Agentic Semantic Layer?.

What about GraphQL for analytics?

GraphQL works for analytics APIs but you end up building the aggregation, caching, and access control layer yourself. A semantic layer provides these out of the box with a purpose-built query interface for analytical workloads.