# Metering Policy Configuration

{% hint style="info" %}
The Revenium metering policy relies on DataWeave expressions, which are only available in MuleSoft Enterprise Edition.
{% endhint %}

### Installing the Revenium Metering Policy <a href="#installing-the-profitstream-metering-policy" id="installing-the-profitstream-metering-policy"></a>

The Revenium Metering Policy can be obtained from "Connect Data" section in the Revenium UI. Once you’ve obtained the policy, you can uncompress it and follow these steps:

1. Open the pom.xml file and replace the two instances of the "ANYPOINT\_GROUPID" placeholder with your Anypoint Organization ID:
   1. "groupId" field
   2. “exchange.url” field
2. Ensure your Maven settings.xml file has an "exchange-server" defined along with the MuleSoft Enterprise repository with appropriate access credentials. Here is a minimal settings.xml file to publish a Custom Policy:

Run the following command:

```shell
mvn --settings hc-settings.xml clean deploy \
-Dapplication.groupId=292a3440-884f-49d7-8c82-b1a1d3641367 \
-Dexchange.username=YOUR_EXCHANGE_USERNAME \
-Dexchange.password=YOUR_EXCHANGE_PASSWORD \
-Dmule.username=MULE_NEXUS_REPO_USERNAME \
-Dmule.password=MULE_NEXUS_REPO_PASSWORD
```

To use a different control plane, such as the EU control plane, you can pass a base platform URL using the `mule.platform.baseUrl` variable. For example:

```shell
mvn --settings hc-settings.xml clean deploy \
-Dapplication.groupId=292a3440-884f-49d7-8c82-b1a1d3641367 \
-Dexchange.username=YOUR_EXCHANGE_USERNAME \
-Dexchange.password=YOUR_EXCHANGE_PASSWORD \
-Dmule.username=MULE_NEXUS_REPO_USERNAME \
-Dmule.password=MULE_NEXUS_REPO_PASSWORD \
-Dmule.platform.baseUrl=eu1.anypoint.mulesoft.com
```

{% hint style="info" %}
If you are using MFA then you will need to create a Connected App within Anypoint Platform (<https://docs.mulesoft.com/access-management/connected-apps-overview>) and follow the instructions here: <https://docs.mulesoft.com/exchange/connected-app-authentication>.

Specifically you must:

* set "exchange.user" to `~~client~~~`
* set "exchange.password" to `CLIENT_ID~?~CLIENT_SECRET`
  {% endhint %}

{% hint style="warning" %}
The Exchange Maven plugin requires Java 8 to run properly. You can set your JAVA\_HOME for Maven like this:

`export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home`
{% endhint %}

The Revenium Metering Policy should now be available in the Anypoint Platform Exchange:

<figure><img src="https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-0e2b1ac68b9e93e58377a9dfbaa6ce0b6a2ac44f%2FScreen%20Shot%202023-01-30%20at%208.28.08%20PM.png?alt=media" alt=""><figcaption></figcaption></figure>

### Configuring the Revenium Metering Policy <a href="#configuring-the-profitstream-metering-policy" id="configuring-the-profitstream-metering-policy"></a>

In order to configure the policy we need to have created a Source, a Product, a Subscription, and Credentials, which are described in depth [here](https://docs.hypercurrent.io/user-guide/manage).

In the Anypoint API Manager, navigate to the API to be metered:

![Selecting the API to be metered](https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-d02ff7a51d39a136748636b9693b30df95c977a2%2FScreen%20Shot%202021-11-28%20at%201.59.46%20PM.png?alt=media)

Navigate to “Policies” and then “Apply New Policy”:

![](https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-86196e0bc68262204d6b452add49d869d8cf1f38%2FScreen%20Shot%202022-06-17%20at%203.55.13%20PM.png?alt=media)

Select the Revenium Metering Policy:

<figure><img src="https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-2ce8ef4b3f115de53101af8177edf72a6eafc0c0%2FScreenshot%202024-09-10%20at%202.34.48%E2%80%AFPM.png?alt=media" alt=""><figcaption></figcaption></figure>

The Metering Policy takes the following configuration parameters:

* **Revenium Platform URL:** Base URL of the Revenium platform API
* **Revenium API Key:** The Revenium API integration key. This is obtained via Manage -> External Configurations -> [API Integration Keys](https://docs.revenium.io/ai-and-api-monetization/api-integration-keys).
* **Revenium Source ID:** The ID of the API. Use the ID of the API we copied earlier from the Revenium User Interface.
* **Metering Expression:** An optional DataWeave expression that determines whether or not an API request should be metered. The default behavior is to meter any request that has a 2xx series (successful) response from the API implementation.
* **Subscription Expression:** An optional DataWeave expression to extract the Revenium Subscription from the API request. The default behavior is to determine the header from either the X-REVENIUM-PRODUCT-KEY \_\_ or, if its not present, using the "client\_id" header.
* **Subscription Credential Expression:** An optional DataWeave expression to extract the Anypoint Client ID from the API request. The default behavior is to use the "client\_id" header. For more information on linking Anypoint Client IDs to Revenium please see the documentation [here](https://docs.hypercurrent.io/user-guide/manage/applications).
* **Metadata Expression:** An optional DataWeave expression to extract arbitrary metadata from the API request.
* **Metering Header Name:** An optional backend-provided header that contains a dynamic call count for metering
* **Reject Requests with Invalid Subscriptions:** If a request contains an invalid Subscription (ie, the key is expired, its quota has been exceeded, etc) then the policy will block the request and return a 402 "Payment Required" response.
* **Subscription Cache TTL (Minutes):** If **Reject Requests with Invalid Subscriptions** is enabled the amount of time to cache invalid keys.

The Policy should now be applied and your API is now ready to be metered.

<figure><img src="https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-bdd93636da438c84610200a63c972dee5abd6c87%2FScreenshot%202024-09-10%20at%202.40.02%E2%80%AFPM.png?alt=media" alt=""><figcaption><p>Configure the Revenium Metering Policy</p></figcaption></figure>

### Configuring Revenium Metering as an Automated Policy

Revenium Metering Policy can be configured as an Automated Policy to ensure that all APIs registered with the Anypoint Platform are metered.

To configure an Automated Policy navigate to "Automated Policies" and click on "Add Automated Policy".

<figure><img src="https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-8adbe750fd6a0dcf2eb9cb320b60320079f56b19%2FScreenshot%202023-02-07%20at%204.06.52%20PM.png?alt=media" alt=""><figcaption></figcaption></figure>

You can then configure the policy as defined in the previous step making sure to leave the "Revenium Source ID" to be blank.

<figure><img src="https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-c1c45268ac9a30b9ce7963ae9d4854b9edaddd2f%2FScreenshot%202024-09-10%20at%202.41.06%E2%80%AFPM.png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
API URL/URIs **must be unique** for the Revenium Metering Policy to function correctly when applied automatically. The Revenium Source ID must be set in APIs with non-unique URLs.
{% endhint %}

### Configuring Metering Elements

When handling API traffic that includes [metering elements](https://docs.revenium.io/ai-and-api-monetization/assets/metering-elements), a DataWeave expression is used to extract the map of element key value pairs from the API transaction. The expression can be applied to the API request, response or both. In the following example a metering element called "country\_code" is populated by the "country" header in the HTTP request.

<figure><img src="https://2470865788-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUfCzMW8qWeXstipFXEh%2Fuploads%2Fgit-blob-80ddabd996eb08e2f995618b93d201c1c7e61557%2FScreenshot%202024-11-07%20at%209.39.15%E2%80%AFAM.png?alt=media" alt=""><figcaption><p>Use the value of the "country_code" header as a metering element</p></figcaption></figure>
