Polar billing platform integration
Use this guide after your framework-level integration is complete. This page covers Polar-specific setup:- Configure a cost-based meter in base currency units.
- Create the
@ai-billing/polardestination. - Map usage events to Polar customers.
- Sync user identity with
externalId.
Meter units
The default@ai-billing/polar destination sends credits_consumed in base currency units and cost_currency alongside it. For USD, a $0.002 cost produces credits_consumed: 0.002. It does not produce 2,000,000 nanos.
Create a Polar meter that filters events named llm_usage and sums credits_consumed. Match your product’s unit pricing to that currency amount. Verify an actual sandbox event against your installed package version before enabling charges.
The destination maps your userId tag to the customer’s externalId. Credentials and meter configuration live in your app and Polar workspace. Document your custom numeric units when you override mapMetadata. Verify those units against a sandbox event.
Install 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
credits_consumedandcost_currency, plus the expectedtag_*metadata and customer identity. - 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.