Reading Subscription Data
When the MonetizationInboundPolicy authenticates a request, it looks up the
caller's subscription — their plan, entitlements, payment status, and billing
dates — and stores it on the request context. Read that data from your own code
with the static MonetizationInboundPolicy.getSubscriptionData method to make
decisions, personalize responses, or log which plan a request ran on.
Where you can call it
getSubscriptionData returns data that the monetization policy puts on the
context, so it only returns a value after the monetization-inbound policy
has run. Call it from:
- A custom code inbound policy placed after
monetization-inboundin the route's inbound pipeline. - The route handler, which always runs after inbound policies.
On a route without the monetization policy — or in a policy that runs before it
— the method returns undefined. Always handle that case.
The monetization-inbound policy must come before any policy that reads the
subscription. If your policy runs first, the subscription data isn't on the
context yet. See
pipeline ordering.
Basic usage
Code
The subscription object
getSubscriptionData returns a MonetizationSubscription. The fields you reach
for most are the plan and the entitlements map:
Code
Switch on plan.key rather than plan.name in your logic — the key is a stable
identifier, while the name is a display label that can change.
Reading entitlements
Each entry in entitlements describes one metered feature or static feature on
the subscription. The key is the meter or feature key; the value reports the
caller's standing against it:
Code
hasAccessis the quickest check for "can this caller use this feature" — it'sfalsewhen the plan doesn't include the feature or the quota has run out.balanceis the remaining allowance. A balance of0or less means no allowance remains.usageandoveragereport consumption this billing period.
Caveats
- Returns
undefinedwhen the monetization policy hasn't run. Guard every call. - The policy caches the data. Subscription and entitlement data is cached
for up to
cacheTtlSeconds(60 seconds minimum), sobalance,usage, andoveragecan lag real-time consumption by the length of the cache window. Treat them as recent, not exact.
Next steps
- Programmatic Monetization — gate operations by plan and meter requests based on the response.
- Dynamic Metering — set meter values at runtime from code.
- Monetization Policy Reference — every policy configuration option.