MonetizationOS Docs
Getting StartedHello, World!

Part 3: Metered access

This tutorial will walk you through adding metered access to your Features via Surfaces. As noted previously, Feature properties can have different types, one of which is Meterable.

Prerequisites

Before you begin, ensure you have the following:

  • Every step from Part 2 completed

Update the Registered Plan

To enable metered access on the Registered Plan, we will update the configured "Widget" Feature's "Enabled" property to be metered.

  1. Navigate to the Plans page in the MonetizationOS console
  2. Click on the "Registered" Plan
  3. Click on "Configure Features"
  4. Switch the "Enabled" property to "Metered"
  5. Set the limit to 3 in a lifetime
  6. Click "Update Plan"

Rerun the registered Surface decision

Rerun the same registered Surface decision request as Part 2 to see the updated response with your new Surface workflow properties.

You should receive a response with changes similar to the following:

Example response
{
    ...
        "enabled": {
            "counterId": "default:feat_abcd1234.enabled",
            "type": "meterable",
            "hasAccess": true,
            "consumedUnits": 0, 
            "remainingUnits": 3,
            "totalUnits": 3,
            "uniqueResources": false,
            "resourceIdUsed": false,
            "periodStart": "Thu, 01 Jan 1970 00:00:00 GMT",
            "isFallback": false
        }
    ...
}

Repeat the request multiple times and you may notice that consumedUnits never increases.

Meters in MonetizationOS are used to enforce limits and are backed by counters that track usage. There are two ways to increment a counter, directly via API or via workflow. Incrementing counters via workflow is the preferred method when working with Surface decisions if you want to keep all logic centralized. However this may not make sense for all use cases, as you may want to increment counters based on outside events.

Add metering to the Surface workflow

Let's now update our Surface workflow to increment the "Enabled" feature property's counter on every Surface decision.

Replace the existing code with the following, and click "Update Workflow" to save your changes:

Surface Workflow
const :  = async ({ ,  }) => {
    // On every run, use one unit of the enabled property meter
    const  = await ...();

    return {
        : {
            : `Hello, ${.}`,
            : . === "authenticated",
            : ..,
            : .,
            : .,
        },
    };
};

export default ;

This is just a basic example of how to increment a counter via a Surface workflow, but in reality you may want to implement more complex logic such as only incrementing on certain conditions based on request data.

Rerun the registered Surface decision again

Rerun the registered Surface decision request three times to exhaust the meter.

You should receive a response with changes similar to the following:

Example response
{
    ...
        "enabled": {
            "counterId": "default:feat_abcd1234.enabled",
            "type": "meterable",
            "hasAccess": true,
            "consumedUnits": 2, 
            "remainingUnits": 1,
            "totalUnits": 3,
            "uniqueResources": false,
            "resourceIdUsed": false,
            "periodStart": "Thu, 01 Jan 1970 00:00:00 GMT",
            "isFallback": false
        }
    ...
    "surfaceBehavior": {
        "properties": {
            "userMessage": "Hello, test+9@example.com",
            "isAuthenticated": true,
            "widgetAccess": true, 
            "widgetAccess": false, 
            "premiumWidgets": false,
            "remainingWidgets": 0
        }
    },
    ...
}

Once the meter is exhausted, you will see that widgetAccess is now false and remainingWidgets is 0.

You've reached the end of Part 3 of this tutorial. You have successfully created a metered Feature property and added meter handling to Surface decisions.

Proceed to Part 4 to continue the tutorial.