> ## Documentation Index
> Fetch the complete documentation index at: https://help.cryptolens.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Metered usage

> Describes how you can record usage for subscriptions that support metered usage (in Stripe).

## Idea

So far we have focused on how to create subscriptions where a constant amount is charged on a recurring basis (eg. \$9/month). Often, however, it is quite useful to be able to charge customers for their actual usage (eg. how many times they have used a certain feature). This is where metered usage feature can be helpful.

## Implementation

If you have used Cryptolens without the recurring payment module, it was most likely implemented [using data objects](https://help.cryptolens.io/licensing-models/usage-based). In the recurring payment module, all usage tracking is taken care of by Stripe, so a different method needs to be used.

### Creating a metered subscription (Stripe)

> Before continuing reading, please go through all the steps in the [setup](https://help.cryptolens.io/recurring-payments/setup) tutorial. It is assumed that you already have a product in Stripe and that you are familiar with how plans work.

When you create a plan, you can choose between `recurring quantity` or `metered usage`. For this tutorial, the `metered usage` option needs to be selected.

<img src="https://mintcdn.com/cryptolensab/UggnyNDOQ_2RBPGh/images/stripe-metered-setup1.png?fit=max&auto=format&n=UggnyNDOQ_2RBPGh&q=85&s=99e1c3fa5aafd124ce037b9971adc24b" alt="Stripe Metered Setup1 Pn" width="1065" height="107" data-path="images/stripe-metered-setup1.png" />

Most of the settings don’t need to be changed; we will cover the important ones below:

* **Usage aggregation mode** - the choice depends on how you plan to charge for usage. If you want to charge your customers each time a feature is used (eg. in an accounting software, this could be each time they generate a new tax report), please set it to “sum up usage during period”.
* **Currency** - it’s important to keep this the same for all customers and plans. Otherwise, Stripe will give an error.
* **Does this pricing plan have multiple price tiers based on quantity** - this option is quite useful if you want to apply a **flat fee** on top of the usage counter. For example, you can have a recurring plan where you charge \$10/month and then add any additional usage to it.

**Note**: it’s important to create a metadata field with the properties of the license key, as described [here](https://help.cryptolens.io/recurring-payments/setup#plans).

### Record usage

To record usage, we can use [Record Usage](https://app.cryptolens.io/docs/api/v3/RecordUsage) method. It will call [Stripe’s metered usage API](https://stripe.com/docs/api/usage_records) and increment the current value with the provided amount. All you need is to create an access token with `Subscription` permission and provide a license key with a product id.

<CodeGroup>
  ```c# C# theme={null}
  var auth = AccessToken.AccessToken.SubscriptionMethods;
  var res = Subscription.RecordUsage(auth, new RecordUsageModel { Amount = 1, ProductId = 3349, Key = "CMXKC-GUQRW-EJUGS-RRPUR" });

  Assert.IsTrue(res != null && res.Result == ResultType.Success);
  ```

  ```java Java theme={null}
  BasicResult res = Subscription.RecordUsage(APIKey.get("subscriptionmethods"),
          new RecordUsageModel(3349, "CMXKC-GUQRW-EJUGS-RRPUR", 1));

  if(!Helpers.IsSuccessful(res)) {
      fail("Could not register usage");
  }
  ```
</CodeGroup>

* [Record Usage method](https://help.cryptolens.io/api/dotnet/api/SKM.V3.Methods.Subscription.html#SKM_V3_Methods_Subscription_RecordUsage_System_String_SKM_V3_Models_RecordUsageModel_) in .NET API docs.
* [Record Usage method](https://help.cryptolens.io/api/java/io/cryptolens/methods/Subscription.html#RecordUsage-java.lang.String-io.cryptolens.models.RecordUsageModel-) in Java API docs.
