Usage-based billing allows API providers to charge customers based on actual usage through Stripe's tiered pricing model, where a base monthly fee covers a certain number of requests and additional requests are charged at a per-unit rate; this is implemented by creating a product with tiered pricing in the Stripe dashboard, configuring usage meters to track events, using service filters in ASP.NET Core to capture usage after each API call, aggregating usage reports in batches via background workers, and leveraging Stripe test clocks to simulate time passage for validating billing scenarios.
Usage-Based Billing with Stripe in ASP.NET Core: A Practical Guide
Added:Let's imagine that we built an API that we want to monetize and charge our users based on the amount of API requests that they made. Luckily, with Stripe usagebased billing, we can implement this fairly easily. Why don't we take a look and see how we can get this done?
Here, we're looking at the API that we want to monetize. It's a recipes API that allows users to manage their different recipes or even retrieve a random one whenever they need some inspiration. What we want to be able to do is charge users for the amount of requests that they make to this API.
Let's head over to our Stripe dashboard and see how we can get this done. In the product catalog, I've gone ahead and set up a product that'll represent the pricing tier for how we're going to charge for these API requests. Notice that this product has two prices. The monthly base price, that's $40 a month, and the API overages that start at $0 a month. Now, this first price, if we take a look at it, is just our standard monthly price that we're going to charge users for having access to the API. The second price represents a tiered pricing model. And if we scroll down to the bottom, you can notice that it's associated with two tiers. The first tier represents a quantity from 1 to 40 and the second tier captures all the rest. So what this means is that for the first 40 API calls, we're not going to be charged any extra to the account. But after we pass that, we're going to be charged 25 cents per additional call that we make to the account. Now to capture the usage, we also had to create what's called a meter. And notice this price is associated with the standard API request meter. So I'm going to click on that.
And on the right side you can see some of the various details that you need to start using this meter. Some important information to pay attention to in this section are the event name, the aggregation method, and the payload key.
Since the aggregation method is set to sum, what it'll do is add up all of the usage reports that we submit to the Stripe API. Now let's head over to our IDE and generate some requests to see what this looks like. to help me generate some requests. I'm going to have GitHub Copilot help me out a little bit. So, I'm going to add my task file as some context. And this just contains some handy helper methods that I already set up. And now I'm just going to issue a prompt and ask it to call the generate request task 10 times. The script that it generates looks good to me. And I'm going to go ahead and hit continue. And now what it's doing is just running requests against my API. If I head back over to the dashboard, you can see that my meter is starting to collect those usage reports. If we scroll down just a little bit, we can see that the meter is able to identify the customer and also that we're sending usage reports in batches of 10. Now, let's head over to James and see what his subscription looks like. So, we're going to check out James and let's click on his active subscription. So, as we scroll down to the bottom and we go to his invoice in the upcoming invoice, we can see that James has gone above the allotted 40 requests per month and has added an additional 60 on top of that, which is getting charged at 25 cents per request.
So now when the next billing cycle comes around, he's going to be charged $55, which includes his monthly base price and the overages from the previous month. Back in our ID, we can see how this was implemented. So, let's close these windows. Now here I have my API controller that has all those different methods. I can open up the get random recipe operation and you can see what I'm doing here is just querying the database for a random record and returning it to the user. So where exactly does the reporting happen?
Well, if we head back to the top of our class, you can see that I'm using what's called a service filter. And what the service filter does is allow us to run code before and after each operation is called. So that means before or after I make a call to get random recipe, I can execute some code. Now in this case, I only want to collect the usage report after the operation is completed. And how we do that is by plugging in our code after calling the next delegate in lines 16 through 20. All that we're doing is we're getting the Stripe customer ID from the logged user and using a channel to have the usage aggregated in the background. Next, what happens is that we have a worker that's receiving all those usage reports. So if I open up this execute async method, you can see that I'm reading the messages off the channel and I'm just aggregating them in memory for now. But whenever the usage count gets greater than 10, then I'm just going to make a batch call to send all those usage reports over to Stripe. Now, if we take a look at the report usage batch, what we're doing is collecting all the usage reports and grouping them by user. Then for each user, I'm going to create a meter event and I'm going to specify the name, which in this case is the event name that we saw in those meter details. And in the payload, we supply the customer ID as well as the usage count. So pretty much what this background worker is doing is saying every time I get 10 usage reports, I'm going to send that information over to Stripe. Now, there's one more thing I want to show you inside the dashboard. If we head back over to our customer section, and I'm going to click on James and then the top of the screen, you see that section that says simulation time. And this is because the James Moriati customer is attached to test clock. So, one of the things that we can do is we can use a test clock and advance the time. So, I'm going to say I want to advance this ahead by 1 month.
And what that did was simulate the passage of time for James' account. Now, if I scroll down a little bit, I should be able to see James' invoices. And as we move through time, we'll notice that the statuses of these invoices will change. Let's head back to the top and move it forward a few more days.
Let's say we advance this like another week.
Now, if we head back down and we take a look at our payments, we see that James made a successful payment for that second invoice.
And in his invoices section, we'll see that he was able to successfully pay for his two invoices.
And this just showed us that using test clocks provides a convenient way for simulating the passage of time whenever we want to validate our usagebased billing scenarios. Hopefully after watching this video, you should feel a little bit more comfortable about implementing usagebased billing inside of your ASP.NET Corebased web applications and also using test clocks to validate your subscription logic. If you're interested in getting the code for this application, well, I'm going to have that in the link below inside of the description. Also, if you want to learn more about usagebased billing, about Stripe, and some of our different APIs, definitely make sure you check out our documentation and some of the other videos that are available here on the Stripe Developers YouTube channel.
[Music]
Up Next

Usage vs Predictability: Pricing Strategies for SaaS Value Metrics
@AcrossTheFunnel
1.3K views•2025-10-16

Building Iconic Brands: Marketing Strategies from Rohan Oza
@CNBC
16.7K views•2017-09-28

Decoy Effect: How Pricing Psychology Influences Consumer Spending
@bobinvestsUS
90K views•2026-01-05

The Planned Obsolescence of Light Bulbs and Tech
@veritasium
25.3M views•2021-03-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Business





























![#008 Reverse Engineering (Scaffold) [ شرح بالعربي ] #ef_reverse_engineering #efcore #scaffold](https://i.ytimg.com/vi/vBDvSqiseP0/maxresdefault.jpg)









