Analytics API: How to Serve Governed Metrics to Any Consumer A developer built an analytics API backed by a semantic layer that serves governed metrics to any consumer, including AI agents. The API provides a single query interface for metrics like revenue and churn, handling query generation, caching, and access control. The project also includes an MCP charts tool that renders interactive charts from query results for AI agents. 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 https://bonnard.dev/glossary/semantic-layer gives you one query interface that serves every consumer. Define metrics once. Query them from anywhere. 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. 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. 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. npm install @bonnard/mcp-charts js 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. visualize with a query. runSql returns rows. 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. | 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 | You need one when: You don't need one when: Once your analytics API can fetch rows, add a visualize tool so agents can chart them: npm install @bonnard/mcp-charts js 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 https://bonnard.dev/blog/mcp-charts . For the design choices behind the tool: How Bonnard Builds Agent-Friendly MCPs https://bonnard.dev/blog/how-bonnard-builds-agent-friendly-mcps . Source is on GitHub https://github.com/bonnard-data/mcp-charts . 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. 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. 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? https://bonnard.dev/blog/what-is-agentic-semantic-layer . 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.