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:

example

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

  1. Go to your Stripe Dashboard.
  2. Go to Product Catalog and then select Create product. Going to 'Product Catalog' and then 'Create product' in Stripe Dashboard
  3. Select Recurring and then select More pricing options. Specifying a recurring charging method for a product and selecting for more pricing options in Stripe Dashboard's product creation screen
  4. Select Recurring and choose Usage-based as the pricing model. You can choose the usage amount as package, unit, or tier.
  5. Define your price amounts, for example, $0.01 for each 1k API requests.
  6. Select the horizontal ellipsis icon  in the Usage section and then select Switch to usage records. Switching to usage records in Stripe price creation screen
  7. Select Metered usage charge method as Sum of usage values during period.
  8. In the Advanced section, enter a description for the price.
  9. Select Next and then select Add product to finish.

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, follow one of these methods:

Access From Account Settings Menu

  1. Select your account icon to bring up the settings menu.
  2. Select Extensions.
  3. Search and select Stripe.

Access From Billing Meters Page

  1. Select Billing Meters in the navigation menu to open the Billing Meters screen.
  2. Select Edit Billing Providers dropdown
  3. Select Stripe.

Access From New Billing Meter Creation Page

  1. Select Billing Meters in the navigation menu to open the Billing Meters screen.
  2. Select + Add Billing Meter.
  3. Select Set up Stripe from the Select Billing Provider dropdown.

Step 3: Adding the Moesif Webhook to Stripe

In Stripe, you need to add a webhook through which Stripe can send subscription updates to Moesif. Follow these steps to add the webhook:

  1. Go to your Stripe Dashboard.
  2. Select Developers in the navigation pane to access the Developers Dashboard. Accessing
  3. Go to the Webhooks tab and then select + Add endpoint. Example
  4. In the Listen to Stripe events screen that appears, paste in the Moesif Webhook URL in the Endpoint URL field. Optionally, add a description for the endpoint. Example

    Remember that you can get the Moesif Webhook URL for Stripe in the Stripe configuration dialog. The Endpoint URL value you add to Stripe also contains your Moesif Application ID. The generated URL in the Moesif-Stripe configuration dialog already contains the correct Application ID and have it appended to the URL.

  5. Next, configure the endpoint to listen to all events by selecting Select events. Example

    In the Select events to send screen, select all events for the Customer and Invoice event types and then select Add events at the bottom of the page.

    Example

  6. Back in the Listen to Stripe events screen, select Add endpoint at the bottom (you’ll likely need to scroll to the bottom of the page). Example

The new webhook you’ve just added appears in the Webhooks tab of Stripe Developers Dashboard.

Example

Step 4: Plugging the Stripe API Details Into Moesif

In Moesif, enter your Stripe API key in the Stripe API Key field of the Stripe configuration dialog.

Moesif supports Stripe API Versions 2020-08-27 or higher.

To get your Stripe API key, follow these steps:

  1. Go to your Stripe Dashboard.
  2. Select Developers in the navigation pane to access the Developers Dashboard.
  3. Go the API Keys tab.
  4. In the Standard keys list, in the Secret key row, select Reveal test key.
  5. Copy the key by clicking the key value. If you need more fine-tuned control over access, create a restricted key and copy that.

Example

Stripe may prompt you to enter your password if the account has been inactive for a while.

This example uses test keys. To get a production key, switch over to live mode.

At this point, your Stripe integration is complete in Moesif and you can begin to use it.

Optionally, in Set Id Mapping in the Stripe configuration dialog, you can customize the mappings between Moesif and Stripe objects. 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:

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 create a new Billing Meter, select Billing Meters menu item in the navigation menu and then select + Add Billing Meter. Alternatively, select + Create New in the navigation menu and then select Billing Meter from the API Monetization section.

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.

example

Once you have your details dialed in, Moesif shows a tabular representation of company usage according to the Billing Meter. Although billing will only happen going forward, you will be able to see historically how your Billing Meter with its filters 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.

example

Step 6: Activating the Billing Meter

Our final step in monetization is to save and activate the billing meter. To do this we simply make sure that the billing meter’s On | Off toggle toggle is turned on. Then select Create to finish creating your meter.

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.

example

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:

See Create a Subscription in Stripe API Reference for more information about the Stripe Subscriptions API.

From Moesif

Updated: