-
Notifications
You must be signed in to change notification settings - Fork 11
Description
I would like to propose adding a new starter kit focused on building a usage based AI proxy.
This would be a focused, serverless template designed to show developers how to build a metered API gateway using Polar. It acts as a bridge for LLM providers like Google Gemini, Groq, or OpenAI, automatically handling the heavy lifting of authentication, usage tracking, and billing.
This would serve as a practical "sell your own API" boilerplate for devs building AI powered products.
Why this is needed
Many developers struggle with monetizing APIs, especially AI APIs. Two challenges come up repeatedly:
- Billing for usage: Accurately tracking token consumption and charging customers based on that data.
- API key management: Building a secure system to issue, validate, and revoke access keys.
This starter kit addresses both problems by using Polar as the backend infrastructure for an API business.
Proposed Solution
The goal is to create a lightweight, edge-ready API proxy using Hono and Cloudflare Workers that:
- Intercepts requests sent to /v1/chat/completions.
- Authenticates users via Polar License Keys, which serve as the API keys.
- Tracks usage by calculating token counts and reporting them to Polar Meters.
- Forwards the request to the upstream provider (like Gemini or Groq).
Polar features used
- License Keys: These act as the developer's API keys, validated on every request.
- Meters & Events: Usage events (e.g., tokens: 150) are sent to Polar to monitor consumption.
- Cost Insights: We attach
_costmetadata to events, allowing the platform owner to track their margin (Revenue vs. Provider Cost). - Customer State: Used to check if the customer has active credits/quota before processing.
Tech Stack
- Framework: Next.js
- API: Route Handlers for the proxy logic
- UI: React Components for a built-in usage dashboard
- SDK: Polar SDK + AI Provider SDKs (Google/Groq).
This example aims to match the quality of the current Storefront starter but focuses specifically on the API and AI space. It provides both the gateway and the user dashboard in one deployable codebase.