Record usage for billing using the API
Learn how to record and report usage for your customers.
To report usage data to Stripe, create a Meter and send meter events that include the event name configured on the meter, customer ID, numerical value, and, optionally, a timestamp. The frequency at which you submit usage data is flexible; you can report it as it occurs or in batches (for example, every day). Learn more about pricing on usage in the implementation guide.
Stripe processes meter events asynchronously, so usage aggregates in meter event summaries and on upcoming invoices might not immediately reflect recently received meter events.
To create Meter Events:
Idempotency
Use idempotency keys to prevent reporting usage more than one time due to latency or other issues. Every meter event corresponds to an identifier that you can specify in your request (if you don’t specify it, we auto-generate one).
Event timestamps
Make sure that the timestamp falls within the past 35 calendar days and isn’t more than 5 minutes in the future (the 5-minute window accounts for clock drift between your server and Stripe’s systems).
Usage values
The numerical usage value in the payload only accepts whole number values. If the overall cycle usage is negative, Stripe reports the invoice line item usage quantity as 0.
Interested in using decimal values?
Contact us at usage-based-billing@stripe.com so we can understand your use case better.
Rate limits
The Meter Events endpoint allows 1000 calls per second in live mode, and one concurrent call per customer per meter. If your service might exceed this limit, you can “bundle” your product into amounts. For example, if you charge per 1000 requests, base your product on “per 1k transactions” and send 1 usage record every 1000 times.
Monitor for 429
status codes and implement a retry mechanism with an exponential back-off schedule to manage request volume.
Also, consider incorporating some randomness into the back-off schedule to avoid a thundering herd effect.
In test and sandbox mode, calls to the meter events
and meter event stream
endpoint count toward the basic limit.
Note
If you’re a Connect platform that is making requests on behalf of a connected account using the Stripe-Account
header, you’re subject to regular Stripe rate limits, which is 100 operations per second.
High-throughput ingestion with higher rate limits
With the API v2, you can send up to 10,000 events per second to Stripe using meter event streams. Contact sales to access sending up to 100,000 events per second.
This endpoint uses stateless authentication sessions. First, use the Meter Event Session API to create a meter event session and receive an authentication token. Then, use the returned authentication token to create your high-throughput meter events with the Meter Event Stream API.
Authentication tokens are only valid for 15 minutes, so you will need to create a new meter event session when your token expires. Here’s an example high-throughput integration:
Note
Due to the large volume of API requests, we don’t include Meter Event Stream requests in the Workbench Logs tab.
Monitor for 429
status codes and implement a retry mechanism with an exponential back-off schedule to manage request volume.
Also, consider incorporating some randomness into the back-off schedule to avoid a thundering herd effect.
Handle meter errors with webhooks
Stripe asynchronously processes meter events.
If an error is found, Stripe creates one of the following events:
Warning
To create an event destination that subscribes to thin events, enable Workbench in your Developer settings.
v1.
: Summarizes recent errors with processing meter eventsbilling. meter. error_ report_ triggered v1.
: Reports an incorrect meter IDbilling. meter. no_ meter_ found
Stripe supports two payload types for events: thin
and snapshot
. The v1.
and v1.
events only support thin event payloads.
View an example payload for each event type below:
The data
attribute gives you fields about the context of the failure, helping you fix the validation errors and reprocess your invalid meter events.
developer_
: Provides a summarized report of all the validation errors identified.message_ summary validation_
: Provides the start time bound when the validation error was triggered.start validation_
: provides the end time bound when the validation error was triggered.end reason
: Provides an array of attributes explaining why the validation error was triggered.reason.
: Provides the total number of validation errors triggered.error_ count reason.
: Provides an array of different categories of validation errors triggered.error_ types reason.
: Provides the error categorization that was triggered. Possible error codes are the following:error_ types. code meter_
event_ customer_ not_ found meter_
event_ no_ customer_ defined meter_
event_ dimension_ count_ too_ high archived_
meter timestamp_
too_ far_ in_ past timestamp_
in_ future meter_
event_ value_ not_ found meter_
event_ invalid_ value no_
: Only supported for themeter v1.
event type.billing. meter. no_ meter_ found
reason.
: Provides the total number of validation errors of a given error type.error_ types. error_ count sample_
: Provides an array of individual error messages triggered.errors sample_
: Provides a sample error message explaining the specific error triggered.errors. error_ message sample_
: Provides the identifier of the meter event, allowing you to make adjustments to the erroneous meter event.errors. request. identifier
To programmatically listen to these events, set up an event destination by following these steps:
Open the Event destinations tab in Workbench, then click Create new destination. Alternatively, use this template to configure a new destination in Workbench with these two event types pre-selected.
Click Show advanced options, then select the Thin payload style.
Select
v1.
andbilling. meter. error_ report_ triggered v1.
from the list of events.billing. meter. no_ meter_ found Create a handler to process the event. For example:
Test your handler. Configure a local listener with the Stripe CLI to send events to your local machine for testing before deploying the handler to production. Use the
--forward-thin-to
flag to specify which URL to forward thethin
events to and the--thin-events
flag to specify which thin events to forward. You can forward all thin events with an asterisk (*
), or a subset of thin events to your application.$ stripe listen --forward-thin-to localhost:4242/webhooks --thin-events "*"
Trigger test events to your handler. Use the trigger function to run the following commands, which simulates the respective events in your account for testing:
$ stripe trigger v1.billing.meter.error_report_triggered --api-key <your-secret-key> $ stripe trigger v1.billing.meter.no_meter_found --api-key <your-secret-key>
If you’re processing events with a webhook endpoint, verify the webhook signatures to secure your endpoint and validate all requests are from Stripe.
Correct and resend invalid events for re-processing.
Best practices
Usage data is crucial for accurate user billing. Make your system resilient to network failures. For example, use a reliable queue such as Amazon SQS to push data to Stripe so you can retry if necessary.