Usage Events
Usage Events
are the mechanism by which Merchants
report their Customers
’ consumption of Billable Metrics
to Sulu. Sending Usage Events
is the final step in the core loop, triggering real-time billing and payment deductions from the Consumer
’s Wallet
.
Role in Billing and Payments
Whenever a Customer
interacts with your Product
in a way that consumes a Billable Metric
defined in their active Subscription
’s Plan
, you should report this consumption by sending a Usage Event
to Sulu.
Upon receiving a valid Usage Event
, Sulu does the following:
- Identifies the
Customer
and their activeSubscription
. - Looks up the
Plan
and thePrice
associated with thebillableMetricId
(s) reported in the event. - Calculates the cost based on the reported
quantity
and the applicablePrice
(considering the pricing model - standard, dynamic, volume). - Instantly deducts the calculated cost from the
Consumer
Organization
’sWallet
. - Instantly credits the calculated cost (minus any applicable fees) to the
Merchant
Organization
’sWallet
. - Records the transaction and updates analytics.
This real-time processing is core to Sulu’s PAYG capabilities.
Usage Event Properties
A Usage Event
object contains several key pieces of information. Here are some of the most important ones:
The unique identifier automatically assigned to the event by Sulu upon successful ingestion.
A unique key provided by the Merchant
when creating the event. Sulu uses this key to deduplicate events. If you send the same event (with the same idempotencyKey
) multiple times within a reasonable window, it will only be processed once, preventing accidental double-billing. It’s crucial to generate a unique key for each distinct usage occurrence.
The ID of the Customer
resource representing the relationship between the Merchant
and the Consumer
who consumed the resource.
The ID of the Merchant
Organization
reporting the usage.
The timestamp (in ISO 8601 format) indicating when the usage actually occurred, as reported by the Merchant
. This should be accurate and fall within the Customer
’s current billing period for their Subscription
.
An array detailing the specific Billable Metrics
consumed and their quantities in this event. See structure below.
An object added by Sulu after the event has been successfully processed and billed. It contains details like the calculated price
(in smallest currency unit) and the ID of the internal billing transaction.
Optional key-value pairs provided by the Merchant
for storing additional context or internal references related to the event.
Ingestion
Usage events are sent to Sulu via its REST API.
- Single Event: Use the Create Usage Event endpoint (
POST /usage
). - Multiple Events: For efficiency, you can send multiple events in one request using the Batch Create Usage Events endpoint (
POST /usage/batch
).
Required Fields at Ingestion
When sending a request to create a usage event, you must include:
idempotencyKey
customerId
merchantId
(usually inferred from authentication, but required in the body per spec)timestamp
properties
(detailing the usage)
The properties
Array
This array is the core of the usage report. Each object within the array represents the consumption of one Billable Metric
.
The ID of the Billable Metric
that was consumed.
The amount of the Billable Metric
’s unit that was consumed in this event (e.g., 1
for one API call, 1024
for 1024 tokens).
Required only if the Billable Metric
uses the dynamic
pricing model. This field specifies the total price (not per unit) to charge for the consumed quantity in this specific event, provided as a decimal string (e.g., “0.05”). This price must fall within the minPrice
and maxPrice
defined in the Price
object on the Plan
.
Example properties
array:
Timing and Idempotency
- Reporting Window: Ensure the
timestamp
you report falls within the current billing period of the customer’s activeSubscription
. Events reported outside this window may be rejected. - Idempotency: Always generate and send a unique
idempotencyKey
for each distinct usage event you intend to bill for. Retrying a failed request with the sameidempotencyKey
is safe.
Reporting Usage Events
accurately and promptly is key to leveraging Sulu’s real-time billing capabilities.