Skip to content

feat(api): public metrics API v1 under /api/v1 with docs - #422

Merged
gkhngyk merged 1 commit into
mainfrom
feature/public-metrics-api
Jul 14, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/public-metrics-api

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a versioned, documented Public Metrics API so agencies can pull AI visibility metrics into Looker Studio, Google Sheets, Power BI or custom dashboards next to their GA4 / Search Console data.

The heavy lifting already existed — the api_keys table, the Settings → API Keys UI, ans_ bearer auth (mcp-auth.ts) and the org-ownership-checked data layer (lib/mcp/data.ts) all shipped with the MCP server. What was missing was a canonical public path and docs:

  • /api/v1/* surface — 9 read-only endpoints. Seven are thin re-exports of the existing /api/mcp/* REST handlers (whoami, brands, visibility-summary, visibility-trend, competitor-comparison, prompt-performance, shopping-cards), keeping one source of truth; both paths serve identical responses.
  • Two new endpoints — GET /api/v1/citations (with source_filter=all|owned|competitor|external) and GET /api/v1/ai-traffic (platform / top-page / country breakdown). Their data functions existed for MCP tools but had no REST surface.
  • Docs — new api-reference/metrics.mdx page (auth, conventions, endpoint reference, Looker Studio notes), added to mint.json navigation, plus an API-keys section in api-reference/authentication.mdx.

No schema changes and no new auth code.

Follow-ups (out of scope)

  • Rate limiting per key (in-memory limiters aren't reliable on serverless; needs a shared store)
  • A native Looker Studio Community Connector (Apps Script) consuming these endpoints
  • Plan gating for API access if we want it as a paid-tier feature

Related Issue

N/A — built directly on request (agency user story: blend Ansvisor metrics with GA4/Search Console in one dashboard).

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🧹 Chore / refactor
  • 📝 Documentation
  • 🚨 Breaking change

How to Test

  1. In the dashboard, create a key under Settings → API Keys and copy the ans_... token.
  2. curl http://localhost:3000/api/v1/whoami -H "Authorization: Bearer ans_..." → returns your user/org ids; a bogus key returns 401.
  3. curl "http://localhost:3000/api/v1/brands" -H "Authorization: Bearer ans_..." → lists your org's brands; grab an id.
  4. Hit visibility-trend, citations and ai-traffic with brand_id + a date range → JSON matches what the dashboard shows for the same window.
  5. Use a brand id from another org → 404.
  6. Confirm /api/mcp/visibility-trend and /api/v1/visibility-trend return identical payloads.
  7. Docs: cd docs && npx mint dev → "Metrics API" appears under API Reference.

Checklist

  • My code follows the project's style guidelines (Prettier clean)
  • I have performed a self-review of my code
  • npx tsc --noEmit passes
  • npm test passes (45/45)
  • Manually tested with a real API key

Expose the existing API-key-authenticated metric endpoints under a
versioned, canonical /api/v1 path (thin re-exports of the /api/mcp REST
handlers), add the two missing read surfaces (citations, ai-traffic),
and document the whole surface as a Metrics API reference page in the
docs site. Lets agencies pull visibility/SoV/citations/traffic data
into Looker Studio, Sheets or custom dashboards next to GA4 data.

No schema changes: the api_keys table, Settings -> API Keys UI and
ans_ bearer auth already exist.
@gkhngyk
gkhngyk merged commit 50f3487 into main Jul 14, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant