Record usage for billing with the API
Learn how to record usage using the Stripe API.
You must record usage in Stripe in order to bill your customers the correct amounts each billing period. To record usage, first configure your meter, and then send meter events that include the event name configured on the meter, customer ID, numerical value, and a timestamp (optional).
You can decide how often you record usage in Stripe, for example as it occurs or in batches. Stripe processes meter events asynchronously, so aggregated usage in meter event summaries and on upcoming invoices might not immediately reflect recently received meter events.
Create meter events
Create a meter event using the API.
Idempotency
Use idempotency keys to prevent reporting usage for each event more than one time because of latency or other issues. Every meter event corresponds to an identifier that you can specify in your request. If you don’t specify an identifier, we auto-generate one for you.
Event timestamps
Make sure the timestamp is within the past 35 calendar days and isn’t more than 5 minutes in the future. The 5-minute window is for clock drift between your server and Stripe 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.
Rate limits
The meter event 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, you can bundle your product into “per 1000 transactions” and then send 1 usage record every 1000 times.
In test mode and sandbox mode, calls to the meter event
and meter event stream
endpoint count toward the basic limit.
Note
If you’re a Connect platform 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.
You can monitor for 429
status codes and implement a retry mechanism with an exponential back-off schedule to manage request volume.
High-throughput ingestion with higher rate limits API v2
With the API v2, you can send up to 10,000 events per second to Stripe using meter event streams. This works in live mode only.
This endpoint uses stateless authentication sessions. First, create a meter event session to receive an authentication token. Authentication tokens are only valid for 15 minutes, so you must create a new meter event session when your token expires.
Next, use the returned authentication token to create your high-throughput meter events with the meter event stream API.
Note
Because of the large volume of API requests, we don’t include meter event stream requests in the Workbench Logs tab.
You can monitor for 429
status codes and implement a retry mechanism with an exponential backoff schedule to manage request volume.
Handle meter event errors
Stripe asynchronously processes meter events. If we find an error, we create one of the following events:
Event | Description | Payload type |
---|---|---|
v1. | This event occurs when a meter has invalid usage events. | thin |
v1. | This event occurs when usage events have missing or invalid meter IDs. | thin |
Warning
To create an event destination that subscribes to thin events, enable Workbench in your Developer settings.
Example payloads
Error codes
The reason.
provides the error categorization that triggered the error. Possible error codes include:
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_
(supported only for themeter v1.
event type)billing. meter. no_ meter_ found
Listen to events
You can listen to events by setting up an event destination.
On the Event destinations tab in Workbench, click Create new destination. Alternatively, use this template to configure a new destination in Workbench with the 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.
Test your handler by configuring 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 process 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.