Node.js billing integration
Use this guide to integrate Narev Cloud billing on any Node.js server without coupling your app to a single billing provider. The pattern works wherever you run the Vercel AI SDK on the server (Next.js route handlers, Hono, Express, Fastify, or a plain Node script behind your API). 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 call
generateText,streamText, orgenerateObjectfrom the Vercel AI SDK on the server. - You already send AI calls through a shared model helper.
- You have a billing destination selected.
- Polar billing platform integration
- Stripe billing platform integration
- Lago billing platform integration
- OpenMeter billing platform integration
Install dependencies
Install middleware for your AI provider. This example uses the Vercel AI Gateway:@ai-billing/gateway for a provider package such as @ai-billing/openai, @ai-billing/anthropic, or @ai-billing/openrouter. See the @ai-billing SDK overview.
Install your billing-platform package separately. For example:
- Polar:
@ai-billing/polar - Stripe:
@ai-billing/stripe - Lago:
@ai-billing/lago - OpenMeter:
@ai-billing/openmeter
Configure environment variables
Set provider and destination credentials in your server environment (.env, deployment secrets, or your host’s env config):
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 handler bypasses this helper, that usage is not billed.
Attach billing tags on every generation call
In any server handler that calls the model, includeproviderOptions['ai-billing-tags']:
app.post, an Express router, or any other HTTP entrypoint. The billing contract is the same.
Minimum tag:
userId
modelIdchatIdorrequestIduserType,workspaceId, orfeature
Organize your integration
Use this structure:lib/ai/billing.ts: middleware wrapper andwrapLanguageModelhelper.lib/ai/destinations.ts: billing-platform destinations.lib/ai/providers.ts: shared model resolver.- Your HTTP handlers: generation routes with billing tags.
Verify billing coverage
- Trigger a generation request and confirm 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 handler uses
getLanguageModel(...). - Confirm required provider credentials are available at runtime.
Some routes bill and others don’t
- Audit
streamText,generateText, andgenerateObjectusage across your codebase. - Move direct provider calls behind your shared helper.