Usage-Based Billing with Webhook Integration - No Infrastructure Headache
Your customers want to pay for what they use. But building a metering system from scratch means weeks of work:
- Event capture and storage at scale
- Multi-tenant data isolation
- Time-based aggregation logic
- Cron-based batch processing
- Webhook delivery to billing systems
- Duplicate prevention and idempotency
Deploy all of this in 5 minutes with our open-source template.
Perfect For
Everything You Need for Usage Metering
Capture events, aggregate over billing periods, deliver to your billing system
Multi-Tenant Event Capture
Track usage per customer with flexible event types. Capture API calls, storage, transactions, or any metric you need.
Flexible Aggregation Operations
Sum, average, min, max, count, first, or last. Choose the right operation for each metric type.
Calendar-Based Periods
Hourly, daily, weekly, monthly, or yearly aggregations. Billing-friendly calculations that align with your pricing model.
Automated Batch Processing
Cron-based processing runs every 15 minutes. Aggregates completed periods automatically without manual intervention.
HMAC-Signed Webhook Delivery
Secure webhook notifications for completed periods. HMAC-SHA256 signatures ensure authenticity.
Production-Ready Architecture
Idempotent processing, duplicate prevention, and state tracking. Built for reliability at scale.
How Usage Metering Works
From event capture to billing webhook in four steps
Your App
Sends events
Event Storage
Per customer
Aggregation
Sum, avg, count...
Billing Webhook
HMAC signed
Automated flow: Events captured instantly → Stored per customer → Aggregated every 15 min → Webhook sent when period completes
Simple API for Usage Tracking
Track any metric with a single POST request. The system handles storage, aggregation, and webhook delivery automatically.
- One endpoint per event type
- Customer ID for multi-tenant isolation
- Optional metadata for filtering
- Batch endpoint for high-volume capture
Track API usage per customer:
// Track an API callawait fetch(METERING_URL + '/usage/api.calls', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-apikey': process.env.CODEHOOKS_API_KEY }, body: JSON.stringify({ customerId: 'cust_123', value: 1, metadata: { endpoint: '/api/users' } })});
// At end of billing period, your webhook receives:// { customerId: 'cust_123', eventType: 'api.calls',// period: 'monthly', value: 15420, ... }Aggregation Operations
Choose the right operation for each metric type
| Operation | Use Case | Example |
|---|---|---|
| sum | Totals (API calls, bytes transferred) | [5, 10, 3] → 18 |
| avg | Averages (response times, ratings) | [10, 20, 30] → 20 |
| count | Event frequency (requests, logins) | 3 events → 3 |
| max | Peak usage (concurrent users) | [100, 200, 150] → 200 |
| min | Minimum values (lowest latency) | [100, 50, 75] → 50 |
| last | Final state (closing balance) | [100, 200, 300] → 300 |
Built for Usage-Based SaaS Pricing
Real-world metering for any consumption metric
API Metering
Track API calls, requests per endpoint, and rate limit usage for consumption-based pricing.
api.calls • requests.count • rate.exceededStorage & Compute
Meter storage bytes, compute minutes, and resource consumption for infrastructure billing.
storage.bytes • compute.minutes • bandwidth.gbAI & ML Services
Track tokens, model invocations, and training compute for AI/ML platform billing.
tokens.used • model.calls • training.hoursA Simpler Alternative to Stripe's Usage API
Tired of fighting with complex metering APIs? Own your usage data, aggregate it your way, and stay payment-provider agnostic.
Why build your own metering? Payment processor metering tools often have limited aggregation options, confusing terminology, and lock you into their ecosystem. With this template, you capture events in your own database, aggregate with full flexibility (sum, avg, min, max, count), and create invoices on your terms.
Ready for Usage-Based Billing?
Deploy a production-ready metering system in 5 minutes. Free to start, scales with your business.
SaaS Metering & Usage-Based Billing FAQ
Common questions about usage metering, billing automation, and webhook integration
What are the best systems for usage-based billing with webhook integrations?
What is SaaS usage metering?
How does usage-based billing work with this system?
POST /usage/:eventType with a customer ID and value. The system aggregates these events over your billing period (daily, weekly, monthly). When a period completes, a webhook delivers the totals to your billing system for invoicing.What aggregation operations are supported?
How do I track API usage per customer?
POST /usage/api.calls with {customerId, value: 1}. Configure the event type with sum operation. At the end of each billing period, you'll receive a webhook with the total API calls per customer.Can I aggregate usage over different time periods?
How does the webhook delivery work?
Is this suitable for high-volume event tracking?
How do I test without sending real webhooks?
DRY_RUN=true in your environment. The system logs webhook payloads and signatures without making HTTP requests. This lets you validate your configuration and event flow before going to production.Can I query real-time usage before the period ends?
GET /events to query raw events with filters (customerId, eventType, time range). For real-time dashboards, use POST /aggregations/trigger to manually aggregate the current period without waiting for completion.