Creating a Billing Meter With Stripe
If you’re looking to monetize your APIs or your application, metered billing could be a perfect solution. With metered billing, users usage is tallied up throughout a designated period and that is how they are billed. Billing meters automatically meter the usage for each company and charge your customers via Stripe. They support a variety of billing models including prepaid, postpaid, Pay As You Go (PAYG) and more. If you have different features or metrics you need to charge on, you can create multiple meters in Moesif, each linked to a different Product and Price in Stripe. The integration also syncs subscription and revenue data from Stripe to Moesif so you can understand how API usage translates to revenue.
A graphical overview of the process looks like this:
In this walk-through, we will create a very simple billing meter that integrates and publishes usage data to Stripe.
This guide is focused on postpaid billing use cases (i.e in arrears). If you’d like to implement prepaid credit based, review the prepaid credit docs.
To follow this guide you will need:
- A Moesif account and successful integration with your API(s)
- A Stripe account
There are a few steps after you’ve integrated with Moesif to get you to the point where you can collect revenue. These steps include:
- Setting up products and prices in Stripe
- Adding the Moesif webhook to Stripe
- Plugging the Stripe API details into Moesif
- Configuring the billing parameters in Moesif
- Activating the billing meter
Let’s get started!
Step 1: Setting Up Products and Prices in Stripe
The first step to monetizing is to actually set up some products in Stripe for usage to be billed against. To set up a product in Stripe you’ll need to click on Products in the header menu.
Once you are on the Products screen, you can click + Add Product in the top right of the screen to create a new product.
The product can contain whatever configuration you need, but you must ensure that at least one Price entry is created. The Price must be configured as:
- Recurring
- Pricing Model set to Usage-based
- Under Usage, check Use usage records
- Adding a Price Description is recommended such as “$0.10 per 1k Requests”
For most consumption-based use cases, you should select Sum of usage values during period. However, if you want to meter on a unique count (like unique users), it’s recommended to pick Maximum usage value during the period as you don’t want to sum up the unique count for the period.
An example configuration for a Product will look like this:
Once your product is configured, click Save product at the top right of the screen.
At this point, we now have a product that we can use with Moesif and begin billing for usage.
Step 2: Configuring Stripe in Moesif
Once your products and prices are created, it’s time to begin to integrate Stripe with Moesif. To begin configuring Stripe in Moesif, go to the Billing Meters page and click the Edit Billing Provider dropdown in the top right corner of the screen.
This will bring up the Stripe configuration screen walking you through the integration. From this screen, you can get all of the info needed to plug Stripe into Moesif. Each step for configuration is covered within the modal.
Step 3: Adding the Moesif Webhook to Stripe
The first step in the integration is to add the Moesif webhook into the configuration in Stripe. Adding this allows Stripe to send subscription updates to Moesif.
To add the Moesif webhook to Stripe, from the upper right-hand side click on Developers, and then Webhooks in the left-side menu. This will bring you to the Webhooks page where you can view existing webhooks and add new ones. To add a new webhook we will click the Add an endpoint button at the bottom of the screen.
From here, we will plug in our Moesif API endpoint URL and configure the events to listen to. You’ll want to copy your Moesif Webhook URL into the Endpoint URL field and then click the + Select Events button.
These details can all be found on the Stripe configuration page in Moesif mentioned in the previous section.
You should select the option under Customer for Select all Customer events. After this, click the Add events button at the bottom of the screen.
After this, you’ll be returned back to the original screen where you added the endpoint details. Scroll to the bottom of the screen and click Add endpoint to save the endpoint to Stripe.
Step 4: Plugging the Stripe API Details Into Moesif
For Moesif to add usage quantities to subscriptions in Stripe, we need to add the Stripe API details into Moesif. This is done in the Stripe configuration screen in Moesif, the same screen we’ve been working with previously.
Currently, Moesif only supports version 2020-08-27 of the Stripe API so that field defaults for the Stripe API Version field.
For the Stripe API Key field, you’ll need to retrieve the API key from Stripe to plug it in. From the Developers screen, the same one we used in the previous step, you’ll click on API Keys. You’ll then be able to see the private key for your API in either the Secret key or a generated Restricted keys field on the screen. Either key can be used.
After copying the key from Stripe, you’ll paste this key into the Stripe API Key field back in Moesif. After doing this, back in Moesif you can scroll down to the bottom of the screen and click Save to save the configuration.
At this point, your Stripe integration is complete in Moesif and you can begin to use it.
Optionally, you have the ability to customize the company_id in Moesif as well. The default should work fine for most purposes. However, you can fully customize it to specify how to map Stripe Subscription and Customer objects to Subscription ID and Company ID in Moesif respectively:
- A Stripe Customer maps to Moesif Company.
- A Stripe Subscription maps to a Moesif Subscription.
Step 5: Configuring the Billing Parameters in Moesif
Once the integration with Stripe is added, you can configure your billing parameters in Moesif. If you haven’t done so already, you’ll want to create a new Billing Meter. To do this, from Moesif you’ll need to click on the Billing Meter link in the left side menu as you did when you started the Stripe integration.
Once you’re on the Billing Meters screen, you’ll click the + Add Billing Meter button to begin creating a new billing meter.
Once on the Add Billing Meter screen, you’ll add in:
- Billing meter name
- Billing provider info
- Add the filter to specify what events to bill upon
In the below example I have set up a billing plan called My Billing Plan which uses Stripe as my billing provider. I’ve also decided to bill on any API call which has a response of 200 OK.
Once you have your details dialed in, you’ll see a visual representation of the filter output at the bottom of the screen. Although billing will only happen going forward, you will be able to see historically how your filter is working with existing data. This can help to make sure, especially with more complex filtering, that you have everything configured the way you require it.
Step 6: Activating the Billing Meter
Our final step in monetization is to save and activate the billing meter. To do this we simply need to make sure that billing meter is turned on at the top of the configuration page.
And lastly, we need to click Create at the top of the screen.
You’ll then be prompted to confirm the billing meters creation in the modal that pops up after clicking on the Create button.
It’s important to note that once a billing meter is created, the criteria for filtering can not be changed nor the billing provider details. Only the name can be changed as well as the status of the billing meter can be toggled on and off. This is for compliance and auditing purposes. Billing meters can also not be deleted but can be archived if no longer in use.
We should now see our new billing meter appear back on the Billing Meters home page.
Now, we have successfully created a billing meter that will begin to send usage data to Stripe. This integration is possible in a matter of minutes with no code required. For further info, you can also check out our documentation.
Step 7: Subscribing to a Product and Price
See Provider Mapping to understand how Stripe concepts like Products map to Moesif.
If you want to sell your product through a subscription model, you must set up Stripe Subscriptions. You can create and manage Stripe subscriptions in different ways.
You may have already integrated Stripe with Moesif. When you create products and prices in Stripe, Moesif automatically creates the corresponding Plans and Prices. You can access them in Product Catalog any time. Whenever a customer confirms a Stripe Subscription, Moesif automatically tracks and captures the subscription data including the associated products and prices. For example, you may have built a subscription page for your product or services with Stripe Checkout. At the end of a successful checkout session, you have a customer who’ve subscribed to pay a recurring price to use your product. Moesif starts tracking the subscription usage according to the Billing Meter you’ve created.
Moesif automatically syncs all the subscription data without any manual supervision.
Create Subscriptions
You can create subscriptions in different ways:
- Create and edit a subscription manually in your Stripe Dashboard.
- Use the Stripe API to create or update subscriptions.
See Create a Subscription in Stripe API Reference for more information about the Stripe Subscriptions API.