This is an example application demonstrating how to integrate Usagey with a Next.js project using the App Router architecture.
Usagey is a complete toolkit for implementing usage-based pricing in your applications. It provides:
- Real-time usage tracking
- Flexible pricing models (per-unit, tiered, hybrid)
- Automated billing
- Analytics and reporting
This example application demonstrates:
- Usage Tracking: How to track usage events in your application
- Usage Dashboard: How to view and analyze usage data
- Pricing Models: How to implement different pricing models
- Billing Simulation: How billing is calculated based on usage
- Node.js 18.17.0 or newer
- npm or yarn
- Clone this repository
git clone https://github.com/8thwanda/usagey-nextjs-approuter-example.git
cd usagey-nextjs-approuter-example
- Install dependencies
npm install
# or
yarn
- Set up your environment variables
cp .env.local.example .env.local
- Edit
.env.local
to add your Usagey API key
# Server-side API key (not exposed to the client)
USAGEY_API_KEY=your_api_key_here
USAGEY_API_URL=https://api.usagey.com # Optional
- Start the development server
npm run dev
# or
yarn dev
- Open http://localhost:3000 in your browser
.
├── app/ # Next.js app directory
│ ├── api/ # API routes
│ │ └── usage/ # Usagey API endpoints
│ │ ├── track/ # Event tracking endpoint
│ │ ├── stats/ # Usage statistics endpoint
│ │ └── events/ # Usage events endpoint
│ ├── usage-tracking/ # Usage tracking demo page
│ ├── usage-dashboard/# Dashboard demo page
│ ├── pricing-demo/ # Pricing models demo page
│ ├── layout.tsx # Root layout
│ └── page.tsx # Home page
├── components/ # React components
├── lib/ # Library code
│ └── usagey-client.ts # API client for Usagey endpoints
├── public/ # Static assets
├── .env.local.example # Example environment variables
└── README.md # This file
app/api/usage/*.ts
- Server-side API routes that interact with Usagey SDKlib/usagey-client.ts
- Client-side API wrapper for making requests to our API endpointsapp/usage-tracking/page.tsx
- Example of tracking usage eventsapp/usage-dashboard/page.tsx
- Example of displaying usage dataapp/pricing-demo/page.tsx
- Example of implementing pricing models
// app/api/usage/track/route.ts
import { UsageyClient } from 'usagey';
const getUsageyClient = (): UsageyClient => {
const apiKey = process.env.USAGEY_API_KEY;
return new UsageyClient(apiKey, {
baseUrl: process.env.USAGEY_API_URL || 'https://api.usagey.com',
});
};
export async function POST(request: Request) {
const { eventType, quantity, metadata } = await request.json();
const client = getUsageyClient();
const result = await client.trackEvent(eventType, quantity, metadata);
return Response.json(result);
}
// Tracking a usage event
import { trackEvent } from '@/lib/usagey-client';
await trackEvent(
'api_call', // Event type
1, // Quantity
{ // Optional metadata
endpoint: '/users',
method: 'GET'
}
);
// Getting usage statistics
import { getUsageStats } from '@/lib/usagey-client';
const stats = await getUsageStats();
// Returns: { currentUsage, limit, percentage, plan }
When using Usagey with Next.js App Router, keep these considerations in mind:
-
Security: The Usagey API key is securely stored on server-side using
USAGEY_API_KEY
instead ofNEXT_PUBLIC_USAGEY_API_KEY
. -
API Routes: All Usagey operations are handled through API routes in
app/api/usage/*
, keeping sensitive operations server-side. -
Dynamic Imports for Recharts: When using Recharts for visualization, use dynamic imports with
{ ssr: false }
to prevent SSR issues. -
Form Handling: The application handles all form submissions through the new API routes, ensuring secure communication with the Usagey service.
MIT