This is an example application demonstrating how to integrate Usagey with an Express.js server.
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:
- Server-Side Usage Tracking: How to track usage events from your backend
- RESTful API Integration: Example API endpoints for tracking and retrieving usage data
- Pricing Model Implementation: How to calculate costs for different pricing strategies
- Usage Visualization: Displaying usage data with charts and tables
- Node.js 14.x or newer
- npm or yarn
- Clone this repository
git clone https://github.com/8thwanda/usagey-express-example.git
cd usagey-express-example- Install dependencies
npm install
# or
yarn- Set up your environment variables
cp .env.example .env-
Edit
.envto add your Usagey API key or setUSE_MOCK_DATA=truefor demo purposes -
Start the server
npm start
# or
npm run dev # for development with auto-reload- Open http://localhost:3000 in your browser
.
├── lib/ # Library code, including Usagey client setup
│ ├── usagey-client.js # Usagey client wrapper
│ └── pricing-models.js # Pricing model definitions
├── public/ # Static assets
├── routes/ # Express routes
│ ├── api.js # API routes for usagey operations
│ └── web.js # Web routes for serving pages
├── views/ # EJS templates
│ ├── partials/ # Reusable template parts
│ ├── index.ejs # Home page
│ ├── usage-tracking.ejs # Usage tracking demo
│ ├── usage-dashboard.ejs # Dashboard demo
│ └── pricing-demo.ejs # Pricing models demo
├── .env.example # Example environment variables
├── server.js # Express server setup
└── README.md # This file
POST /api/track
Request Body:
{
"event_type": "api_call",
"quantity": 1,
"metadata": {
"endpoint": "/users",
"method": "GET"
}
}GET /api/stats
GET /api/events?event_type=api_call&limit=10
Parameters:
event_type: Filter by event typestart_date: Filter by start date (ISO format)end_date: Filter by end date (ISO format)limit: Number of events to return (default: 50, max: 1000)
POST /api/calculate
Request Body:
{
"model_id": "tiered",
"usage": 5000
}// lib/usagey-client.js
const { UsageyClient } = require('usagey');
function getUsageyClient() {
const apiKey = process.env.USAGEY_API_KEY;
return new UsageyClient(apiKey, {
baseUrl: process.env.USAGEY_API_URL || 'https://api.usagey.com',
});
}// Example route handler
const { trackEvent } = require('../lib/usagey-client');
router.post('/track', async (req, res) => {
try {
const { event_type, quantity = 1, metadata = {} } = req.body;
const result = await trackEvent(event_type, quantity, metadata);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
});// Example route handler
const { getUsageStats } = require('../lib/usagey-client');
router.get('/stats', async (req, res) => {
try {
const stats = await getUsageStats();
res.status(200).json(stats);
} catch (error) {
res.status(500).json({ error: error.message });
}
});MIT