Amberflo Metering / Billing Policy
Amberflo (amberflo.io) is a usage metering and billing service. This policy allows you to meter API calls going through Zuplo and send them to your Amberflo account using your Amberflo API key.
Add the policy to each route you want to meter. Note you can specify the Meter API Name and Meter Value (meter increment) at the policy level.
Configuration
{
"name": "my-amberflo-metering-inbound-policy",
"policyType": "amberflo-metering-inbound",
"handler": {
"module": "$import(@zuplo/runtime)",
"export": "AmberfloMeteringInboundPolicy",
"options": {
"apiKey": "YOUR_API_KEY",
"customerIdPropertyPath": ".data.accountNumber",
"meterApiName": "api-calls",
"meterValue": 1,
"statusCodes": "200-299, 304"
}
}
}
You can set the customerId globally (not recommended) by setting it at the policy level or use the customerIdPropertyPath
to read the customerId from the user object on each request. For example, if you're using API Key auth or JWT auth and want to use the sub
property as the customerId, you would set the value as follows
"customerIdPropertyPath" : ".sub"
You can also dive into the properties of the metadata. Imagine the request.user
property is as follows (either based on contents of a JWT token or API Key metadata)
{
"sub": "bobby-tables",
"data": {
"email": "bob@example.com",
"name": "Bobby Tables",
"accountNumber": 1233423,
"roles": ["admin"]
}
}
You could access the accountNumber
property as follows. Note the required preceding '.'
.
"customerIdPropertyPath" : ".data.accountNumber"
You can also set many of the properties of the meter payload programmatically, either in a custom policy or handler. Here is some example code in a custom inbound policy:
import {
AmberfloMeteringPolicy,
ZuploContext,
ZuploRequest,
} from "@zuplo/runtime";
export default async function (
request: ZuploRequest,
context: ZuploContext,
options: MyPolicyOptionsType,
policyName: string
) {
AmberfloMeteringPolicy.setRequestProperties(context, {
customerId: request.user.sub,
meterApiName: request.params.color,
});
return request;
}
Read more about how policies work