The Usage Data Model
At Flowglad, we’ve designed a flexible system to help you implement usage-based billing. Here’s a breakdown of the key components and how they work together. Think of it like measuring water consumption:-
Usage Meters: This is the central measuring device. For every type of usage you want to track (e.g., API calls, data storage, active users), you’ll create aUsage Meter. It’s like installing a specific water meter for your main water line, another for your garden hose, etc. Each meter has anAggregation Typethat determines how usage is calculated. -
Prices(Usage Type): Once you have a meter, you need to define how much to charge for what it measures. APriceof type “Usage” links directly to aUsage Meter. It sets the cost per unit of usage (e.g., 5 per GB of storage). Multiple prices can be associated with a single usage meter, allowing for different pricing tiers or plans. -
Subscriptions: When a customer signs up for a product or service that has usage-based components, they are subscribed to one of these “Usage”Prices. This links the customer to the specific way their consumption will be measured and billed. -
Usage Events: These are the individual records of consumption. Every time a customer performs a billable action (e.g., makes an API call, an active user logs in), aUsage Eventis recorded. Each event is tied to:- The
Customer - Their
Subscription - The specific
Pricethey are subscribed to - And, through the price, the relevant
Usage Meter
- The
Usage Meters collect all the Usage Events and aggregate them based on the chosen Aggregation Type for a billing period:
Sum: This is the simplest model. It adds up theamountfrom all usage events. For example, if a customer makes 100 API calls, and eachUsage Eventhas anamountof 1, the total aggregated usage is 100. This is ideal for things like tokens consumed, messages sent, or data processed.Count Distinct Properties: This model counts the unique values of a specific property within theUsage Events. For instance, to bill for “monthly active users,” you’d send aUsage Eventeach time a user is active, including auserIdin thepropertiesfield. The meter would then count the number of uniqueuserIds within the billing period. This is great for MAUs, unique workspace users, etc.
By default, Flowglad charges for aggregated usage at the end of the billing period. This ensures all consumption within that period is accurately captured before invoicing.
Setting Up and Using Usage-Based Billing
Here’s how to get started with tracking and billing for usage:Step 1: Create a Usage Meter
- Navigate to the Pricing Models page in your dashboard and select the pricing model you would like to create the usage meter in.
- Click “Create Usage Meter.”
- Give your meter a descriptive
name(e.g., “API Calls,” “Active Users”). - Select the
Aggregation Type:Sum: To add up values from usage events.Count Distinct Properties: To count uniquepropertyvalues.
- Save the meter. You’ll need its
idfor the next step.
Step 2: Create a Usage Price
- Go to “Products” and select the Product this usage component will belong to, or create a new one.
- Within the Product, go to the “Prices” section and click “Create Price.”
- Set the
Typeto “Usage”. - Fill in the price details (name, unit price, currency, interval, etc.).
- Crucially, select the
Usage Meteryou created in Step 1 from theUsage Meterdropdown. This links this price to that specific meter.
Step 3: Report Usage Events
Reporting usage is done via API calls from your backend. This is to ensure data integrity and security, as you’ll be verifying and sending financial data. You’ll use theFlowgladServer.createUsageEvent method. Here’s a TypeScript example of how you might do this in a Node.js backend:
createUsageEvent:
customerId: The ID of the customer who incurred the usage.subscriptionId: The ID of their active usage subscription.priceId: The ID of the specific usage price associated with their subscription and this event.usageMeterId: The ID of the usage meter this event should be recorded against.amount: Forsumaggregation, this is the quantity of usage (e.g., number of API calls, megabytes transferred). Forcount_distinct_properties, this is often1, as the uniqueness is determined by theproperties.transactionId: Crucial for idempotency. This should be a unique ID generated by your system for each usage event. If Flowglad receives acreateUsageEventcall with atransactionIdit has already processed for thatusageMeterId, it will not create a duplicate event.usageDate(optional): Timestamp (in milliseconds since epoch) of when the usage occurred. Defaults to the time of the API call. If usage occurs outside the current billing period, it will still be attached to the current open billing period for that subscription.properties(optional): A JSON object. Required if the linkedUsage Meterhas anAggregation Typeofcount_distinct_properties. This object should contain the property you want to count distinct values of (e.g.,{ "userId": "some_unique_user_id" }).