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.
Next.js billing integration
Use this guide to integrate Narev Cloud billing in a Next.js App Router project without coupling your app to a single billing provider. You’ll:- Wrap all model calls with
@ai-billingmiddleware. - Attach consistent billing tags for attribution.
- Keep your provider logic modular so you can use Polar, Stripe, or another destination.
Before you start
- You already use Next.js App Router and Vercel AI SDK.
- You already send AI calls through a shared model helper.
- You have a billing destination selected.
Install dependencies
Install the framework-level package:- Polar:
@ai-billing/polar - Stripe:
@ai-billing/stripe
Add framework-level environment variables
Set variables that your framework integration needs in.env.local:
Build a platform-agnostic billing wrapper
Createlib/ai/billing.ts and inject destinations from a separate module:
lib/ai/billing.ts
Route all model access through one provider helper
Create a single model entrypoint inlib/ai/providers.ts:
lib/ai/providers.ts
getLanguageModel(...) everywhere. If a route bypasses this helper, that usage is not billed.
Attach billing tags on every generation call
In routes such asapp/api/chat/route.ts, include providerOptions['ai-billing-tags']:
userId
modelIdchatIdorrequestIduserType,workspaceId, orfeature
Organize your integration
Use this structure:lib/ai/billing.ts: framework middleware wrapper.lib/ai/destinations.ts: provider-specific destinations.lib/ai/providers.ts: shared model resolver.app/api/**/route.ts: generation routes with billing tags.
Verify billing coverage
- Trigger chat generation and confirm Polar or your billing platform receives usage events.
- Trigger non-chat AI features and confirm your billing platform receives those usage events.
- Confirm every AI path uses
getLanguageModel(...). - Confirm
userIdis always present inai-billing-tags.
Troubleshooting
No usage events appear
- Confirm your destination config loads during server startup.
- Confirm each route uses
getLanguageModel(...). - Confirm required provider credentials are available at runtime.
Some routes bill and others don’t
- Audit
streamText,generateText, andgenerateObjectusage. - Move direct provider calls behind your shared helper.