Usage Based Billing
Set up usage based pricing models effortlessly.
Usage is in a private, invite-only preview. Please reach out to the Flowglad team over Discord to access it.
Startups face unexpected setbacks when implementing usage based pricing. Flowglad has designed usage based pricing to abstract away as much complexity as possible.
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 Type
that 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. APrice
of 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 Event
is recorded. Each event is tied to:- The
Customer
- Their
Subscription
- The specific
Price
they are subscribed to - And, through the price, the relevant
Usage Meter
- The
How Usage is Aggregated
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 theamount
from all usage events. For example, if a customer makes 100 API calls, and eachUsage Event
has anamount
of 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 Event
each time a user is active, including auserId
in theproperties
field. The meter would then count the number of uniqueuserId
s 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 “Usage Meters” section in your Flowglad dashboard.
This section is only visible if the “Usage” feature flag is enabled for your organization.
- 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 unique property values. If you choose this, you’ll also need to specify theproperty_name
you’ll be sending in your usage events (e.g.,userId
,email
).
- Save the meter. You’ll need its
id
for 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
Type
to “Usage”. - Fill in the price details (name, unit price, currency, interval, etc.).
- Crucially, select the
Usage Meter
you created in Step 1 from theUsage Meter Id
dropdown. This links this price to that specific meter. - Save the price.
Step 3: Create a Usage Subscription
Customers subscribe to this “Usage” Price
just like any other Flowglad price. You can use:
- Checkout Sessions: Integrate Flowglad’s checkout, and customers can select plans that include your new usage price.
- Direct API Calls: Create subscriptions directly via the API if you have a custom subscription flow.
Once a customer is subscribed to a usage price, their SubscriptionId
becomes key for reporting their usage.
Step 4: 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 the FlowgladServer.createUsageEvent
method. Here’s a TypeScript example of how you might do this in a Node.js backend:
Key parameters for 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
: Forsum
aggregation, 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 acreateUsageEvent
call with atransactionId
it 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 Meter
has anAggregation Type
ofcount_distinct_properties
. This object should contain the property you want to count distinct values of (e.g.,{ "userId": "some_unique_user_id" }
).
By following these steps, you can effectively model, track, and bill for various types of usage with Flowglad. Remember that usage events roll up and are used to calculate charges at the end of each billing period.