Skip to main content

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/polar destination.
  • Map usage events to Polar customers.
  • Sync user identity with externalId.
For the server middleware setup, use Node.js billing integration.

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 userIdexternalId.
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 calling polar.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:
Use sandbox until your metering flow is verified.

Create Polar billing destinations

Create lib/ai/destinations.ts:
lib/ai/destinations.ts
The key mapping is externalCustomerIdKey: 'userId'. Your billing tags must include userId.

Sync Polar customers on signup

Create lib/polar-client.ts:
lib/polar-client.ts
Call this when a user signs up:

Required tag contract

In your generation routes, include:
Contract requirements:
  • userId in ai-billing-tags must match Polar externalId.
  • Each user should have one Polar customer record.

Verify polar metering

  1. Register a new user and confirm a Polar customer exists with externalId = userId.
  2. Trigger a model call and confirm an event named llm_usage appears.
  3. Validate metadata contains userId, modelId, and route context.
  4. Confirm your environment uses the expected Polar server (sandbox or production).

Common issues

Events appear but don’t map to customers

  • Confirm userId exists in ai-billing-tags.
  • Confirm destination uses externalCustomerIdKey: 'userId'.
  • Confirm customer creation uses externalId = userId.

No events arrive in Polar

  • Confirm POLAR_ACCESS_TOKEN is present at runtime.
  • Confirm POLAR_SERVER matches the Polar workspace you are checking.
  • Confirm framework routes call the billing-wrapped model helper.