Skip to content
Create account
or
Sign in
The Stripe Docs logo
/
Ask AI
Create account
Sign in
Get started
Payments
Finance automation
Platforms and marketplaces
Money management
Developer tools
Get started
Payments
Finance automation
Get started
Payments
Finance automation
Platforms and marketplaces
Money management
Overview
Billing
    Overview
    About the Billing APIs
    Subscriptions
    Invoicing
    Usage-based billing
      Choose a usage-based billing setup
      Record usage for billing
        Create a meter
        Configure a meter
        Record usage with the API
        Record usage in the Dashboard
        Record usage with S3
        Configure grace period
      Offer billing credits
      Monitor usage
      Usage-based pricing models
    Connect and Billing
    Tax and Billing
    Quotes
    Revenue recovery
    Automations
    Scripts
    Revenue recognition
    Customer management
    Entitlements
    Test your integration
Tax
Reporting
Data
Startup incorporation
HomeFinance automationBillingUsage-based billingRecord usage for billing

Record usage for billing with the API

Learn how to record usage using the Stripe API.

Copy page

You must record usage in Stripe to make sure you 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.

Program.cs
.NET
// Set your secret key. Remember to switch to your live secret key in production. // See your keys here: https://dashboard.stripe.com/apikeys StripeConfiguration.ApiKey =
"sk_test_BQokikJOvBiI2HlWgH4olfQ2"
; var options = new Stripe.Billing.MeterEventCreateOptions { EventName = "alpaca_ai_tokens", Payload = new Dictionary<string, string> { { "value", "25" }, { "stripe_customer_id", "{{CUSTOMER_ID}}" }, }, }; var service = new Stripe.Billing.MeterEventService(); Stripe.Billing.MeterEvent meterEvent = service.Create(options);

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 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.

Higher rate limit

Contact sales if you need to send up to 100,000 events per second.

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.

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.

.NET
namespace MeterEventExample { using System; using System.Collections.Generic; using System.Threading.Tasks; using Stripe; using Stripe.V2.Billing; public class MeterEventExample {

Handle meter event errors

Stripe asynchronously processes meter events. If we find an error, we create one of the following Events:

EventDescriptionPayload type
v1.billing.meter.error_report_triggeredThis event occurs when a meter has invalid usage events.thin
v1.billing.meter.no_meter_foundThis 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

The following is an example payload for a v1.billing.meter.error_report_triggered event.

{ "id": "evt_test_65R2GpwDsnmpzihMjdT16R2GDhI4SQdXJGRbvn7JA8mPEm", "object": "v2.core.event", "created": "2024-08-28T20:54:12.051Z", "data": { "developer_message_summary": "There is 1 invalid event", "reason": { "error_count": 1, "error_types": [ {

Error codes

The reason.error_types.code 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_meter (supported only for the v1.billing.meter.no_meter_found event type)

Listen to events

You can listen to events by setting up a webhook endpoint or another type of event destination.

  1. On the Webhooks 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.

  2. Click Show advanced options, then select the Thin payload style.

  3. Select v1.billing.meter.error_report_triggered and v1.billing.meter.no_meter_found from the list of events.

  4. Create a handler to process the event.

    .NET
    using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Stripe; using Stripe.Events; [Route("api/[controller]")] [ApiController] public class WebhookController : ControllerBase {
  5. 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 the thin 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 "*"
  6. 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>
  7. If you process events with a webhook endpoint, verify the webhook signatures to secure your endpoint and validate all requests are from Stripe.

  8. Correct and resend invalid events for re-processing.

Was this page helpful?
YesNo
Need help? Contact Support.
Join our early access program.
Check out our changelog.
Questions? Contact Sales.
LLM? Read llms.txt.
Powered by Markdoc
Related Guides
Usage-based pricing models
Products Used
Billing