Start with usage-based billing so you can measure usage and bill customers correctly from day one. This guide walks you through connecting a Next.js app to Narev Cloud using the Narev SDK and sending billable AI events to Polar.Documentation Index
Fetch the complete documentation index at: https://narev.ai/docs/llms.txt
Use this file to discover all available pages before exploring further.
Get your API keys
To authenticate your app, you need API keys from both Narev Cloud and your billing provider.
- Create a free Narev Cloud account.
- Navigate to Settings → API Keys and generate a new
NAREV_API_KEY. - Get your billing provider credentials. If you are using Polar, create a sandbox workspace and generate a
POLAR_ACCESS_TOKEN.
.env.local
This guide uses Polar because it has a concise setup flow. You can use the exact same framework pattern with Stripe, OpenMeter, Lago, or other supported destinations.
Install the packages
Install the core Narev SDK billing package, your preferred billing provider destination, and the Next.js UI components.
Configure your billing destination
Configure the destination that receives your billing events.Create a
lib/destinations.ts file to configure Polar. The externalCustomerIdKey is critical because it tells Polar which Customer Tagging field maps the usage to a specific customer.- Next.js
lib/destinations.ts
Add the backend middleware
Use the
wrapLanguageModel function from the Vercel AI SDK to attach the Narev billing middleware to your model. This wrapper automatically tracks token usage and sends it to your billing provider.Create a central helper for your billed models that uses your configured destinations.- Next.js
lib/billing.ts
Use the model in your API route
Pass the wrapped model into Vercel’s
streamText function. Use the providerOptions object to pass Customer Tagging metadata. The billing destination uses this data to know which customer to charge for the tokens consumed.- Next.js
app/api/chat/route.ts
Add the pre-built billing components
Embed the pre-built React components in your Next.js frontend to show users their real-time usage and provide a native checkout experience for buying credits.
- Next.js
app/components/usage-dashboard.tsx
Choose your next guide
Next.js billing integration
Build the full framework integration with reusable helpers and coverage checks.
Polar billing platform integration
Configure Polar customers, event names, and usage mapping for metered billing.
Narev SDK overview
Learn how provider middleware, price resolvers, and destinations work together.
Benchmarking quickstart
Compare model quality and cost before changing production routing.

