How to set up usage-based billing with Stripe and Moesif for your API

A good business model is one that can easily generate revenue. Often, when developers build something it could easily be packaged and used by another organization. This is extremely true when it comes to APIs. If an API product is solving a well-known problem, there is likely a market for it. Being able to expose an API for public consumption can be done in many ways, a popular option being using an API gateway. The real hurdle comes when you decide to start billing for usage. Monetization is one of the biggest challenges faced by an API provider who wants to implement usage-based billing to capitalize on API consumer revenue.

API monetization generally takes a lot of integrations, a fair amount of code, customization, and can also lead to a large support burden. This is especially true when things don’t go according to plan, possibly through a bug in the billing setup, and billing issues arise. In short, there are challenges both during implementation, and once the billing system is up and running.

What if a simpler approach was possible? At Moesif, we recently introduced a feature for billing meters. This feature allows you to tally up the usage data coming into Moesif from your API products, send this data to a billing provider, and have the users be presented with accurate bills, all with a fraction of the work required to implement a custom solution.

To illustrate how it works, let’s assume we have an API that we would like to charge customers to use. As an example, let’s pretend that we have created a new credit score API that companies can use to bring consumers’ credit ratings back to their app. Our API will be /RetrieveCreditScore, and users will be charged for each query/call they send to the endpoint.

Our API monetization model will be pretty simple. We will have 3 usage subscription plans that will determine how much the users will be charged:

  • 1-100 queries per month ($5 per query)
  • 101-1000 queries per month ($3 per query)
  • 1001+ queries per month ($2 per query)

This tiered pricing scheme is quite common, where the more the service is used, the larger the discount is.

Stripe will be the billing provider that we will use to invoice and charge customers via online payment for their usage. Stripe payments are simple to use and will allow us to easily set up products and pricing to adhere to the pricing scheme above. In Stripe, this type of pricing scheme is called Graduated Pricing.

Of course, there are multiple ways to implement pricing schemes in Stripe that will work with Moesif. This is just one example of how it could be done.

We will now use Moesif to tally up the usage for the endpoint and send the usage metrics to Stripe. Stripe can then use these metrics to bill the customer accordingly, based on the tier that their metrics coincide with, and collect payment.

Integrate your app and APIs with Moesif

In order to use the Billing Meters feature in Moesif, you need to have your APIs integrated with Moesif. This is because Moesif will need the metrics from the API usage to send Stripe the info it requires for accurate direct monetization. Once your APIs are integrated with Moesif, you can also use other features which pair well with our Billing Meters feature, including behavioral emails, governance rules, and alerts.

If you are not currently using Moesif to monitor your APIs, integration can be done in a few different ways. If you are using an API gateway or API management platform, you can use one of our many plugins which allow you to quickly move metrics data from your APIs into Moesif. If you are not using a third-party gateway or management platform, or want to do it at the API code level, you can use one of our SDKs. A Moesif SDK will allow you to easily integrate Moesif with your Node, Python, or Java APIs (plus many, many more languages and frameworks) directly from your code. Either way is easy and simple to support.

Another Moesif feature that you need to deploy in order to make billing work correctly, is to implement user and company tracking. Generally, this can be set up in a few simple steps. We need this feature enabled so that usage data in Moesif can be tied to specific users and companies. This is how Stripe will map usage to a customer within Stripe, so they can be billed accordingly.

Once you have integrated with Moesif, and have user and customer tracking enabled, your next step would be to actually create your products in Stripe, so they can be used in Moesif for accurate usage based billing.

Create plans and add-ons in Stripe

After creating a Stripe account and logging in, you can begin to create products to use for billing. In this example, we will create a product with a graduated pricing scheme.

You’ll need to create a product in Stripe that:

  • Includes a graduated pricing model
  • Has a monthly billing period
  • Uses metered billing
  • Charges for metered usage by the sum of usage values during the period
  • Optionally (but recommended), includes a price description

With all that configured, we will create our pricing scheme. This configuration will look like this in Stripe:


Now, at the end of the month, when Stripe creates the invoice, it will do so based on these tiers. Of course, at this point, we only have the plans defined, but no one will be charged since we don’t yet have any usage data being sent to Stripe.

Integrate Stripe with Moesif

We still need to actually get the data from Moesif over to Stripe, and vice versa. There are two mechanisms that are used for this: a webhook and the Stripe API. Each has a distinct part in facilitating the data sharing between the platforms.

By adding the webhook into Stripe, subscription updates can be sent back to Moesif. By using the Stripe API, Moesif can send usage details to Stripe and can also retrieve details about available products and pricing in Stripe. These two points of contact are all that’s needed for the Stripe and Moesif integration. Thankfully, Moesif walks you through this when you set up Stripe as a billing provider and provides all the needed details. For the specifics, you can also check out our docs on the Stripe integration.

Set your billing parameters

Once you’ve integrated Stripe in Moesif, you can set up your billing meter. For our example, it will be very simple. What we will do is send usage metrics to Stripe whenever an API call to /RetrieveCreditScore returns a 200 OK response. This means we will only bill for successful calls and won’t accidentally charge for calls where an error was experienced.


Once we create the billing meter, every hour the usage for each customer will be sent to Stripe. At the end of the month, Stripe will create an invoice based on our graduated price structure and bill the user.

You can also use Moesif to automatically send behavioral emails for every successful call, or if they are about to cross into the next discount tier. You could also use Moesif’s Governance Rules to block users with overdue invoices from accessing the API until their invoice is settled.

Try it out for yourself

As you can see, you are just a few steps away from robust API monetization that is simple to implement and support. By using Moesif and Stripe together you’ll be able to charge customers for usage in a matter of minutes, manage subscriptions, and can even use other Moesif features to create the ultimate customer experience. Our billing setup is so simple that it can even be done without having any developer skills.

The Billing Meters feature is available for all Moesif users. Sign up for Moesif today to instantly access our Billing Meters feature to begin billing for your customers’ API usage. If you’re already using Moesif, click on Billing Meters in the left-side navigation menu and take a look at our docs to show the exact steps to get you to monetize your APIs.

Learn More About Moesif Easily Monetize Your APIs 14 day free trial. No credit card required. Learn More
Monetizing Your APIs with Moesif and Stripe Monetizing Your APIs with Moesif and Stripe

Monetizing Your APIs with Moesif and Stripe

Learn More