Polar billing platform integration
Use this guide after your framework-level integration is complete. This page covers Polar-specific setup:- Choose a metering strategy (nanos or itemized).
- Create the
@ai-billing/polardestination. - Map usage events to Polar customers.
- Sync user identity with
externalId.
Metering strategy
Polar supports two ways to bill LLM usage with Narev:
The rest of this guide covers nanos metering: one
llm_usage event, customer matched via userId → externalId.
Narev Cloud no longer hosts a Connect dashboard for Polar. Credentials, meters, and destinations live in your application. The removed Connect flow also provisioned top-up credit products and itemized per-model products with a daily price sync cron. Replicate any of that in your own infrastructure using the Narev Prices API and the Polar Products API.
Itemized metering (advanced)
Itemized billing creates one Polar product per model with meters for each token dimension. Update prices by callingpolar.products.update with metered_unit entries whose unitAmount uses the same cents-per-token conversion as Stripe (pricePerToken * 100). The removed Connect sync ran this daily for itemized connections only.
Install polar dependencies
Configure environment variables
Set these on your server:sandbox until your metering flow is verified.
Create Polar billing destinations
Createlib/ai/destinations.ts:
lib/ai/destinations.ts
externalCustomerIdKey: 'userId'. Your billing tags must include userId.
Sync Polar customers on signup
Createlib/polar-client.ts:
lib/polar-client.ts
Required tag contract
In your generation routes, include:userIdinai-billing-tagsmust match PolarexternalId.- Each user should have one Polar customer record.
Verify polar metering
- Register a new user and confirm a Polar customer exists with
externalId = userId. - Trigger a model call and confirm an event named
llm_usageappears. - Validate metadata contains
userId,modelId, and route context. - Confirm your environment uses the expected Polar server (
sandboxorproduction).
Common issues
Events appear but don’t map to customers
- Confirm
userIdexists inai-billing-tags. - Confirm destination uses
externalCustomerIdKey: 'userId'. - Confirm customer creation uses
externalId = userId.
No events arrive in Polar
- Confirm
POLAR_ACCESS_TOKENis present at runtime. - Confirm
POLAR_SERVERmatches the Polar workspace you are checking. - Confirm framework routes call the billing-wrapped model helper.