This guide shows how to enforce Metering & Billing entitlements on Kong Gateway API traffic with the Entitlement Enforcement plugin. Unlike the Metering & Billing plugin, which only meters usage, the Entitlement Enforcement plugin actively blocks requests: it polls the Metering & Billing Entitlement Access API for each customer and returns an error when the customer has no access to a feature or has exhausted a usage limit.
In this guide, you’ll:
- Create a Kong Gateway Consumer that you’ll map to a customer
- Set up a meter for Kong Gateway API requests
- Create a metered feature
- Create a plan that grants that feature as an entitlement, and publish it
- Create a customer and start a subscription so the entitlement is active
- Enable the Metering & Billing plugin to report usage, and the Entitlement Enforcement plugin to enforce the entitlements
- Verify that traffic is allowed within the limit and blocked once the limit is reached
Enforcement needs configuration on both sides, and the pieces reference each other. The following table lists what you’ll create, why each piece is needed, and what it connects to:
|
What you configure |
Where |
Why it’s needed |
|---|---|---|
example-service and example-route
|
Prerequisite | The entities the plugins attach to. The Entitlement Enforcement plugin is enabled on the Route, so it runs for traffic matching that Route only. |
| Two Konnect system account tokens | Prerequisite | One with the Ingest role for reporting usage, one with the Entitlement Access role for reading entitlements. The roles are separate, so a single token can’t do both unless it has the Admin role. |
| Redis | Prerequisite | Where the plugin caches enforcement state: a background timer writes the state it fetches from Metering & Billing, and each worker syncs its local cache from Redis. The plugin can’t enforce without a reachable Redis. |
Consumer with a pinned id
|
Kong Gateway |
Identifies the client. The pinned id fixes the consumer:<id> subject key that both plugins use, so the customer you create later can be matched to it.
|
| Key Auth plugin | Globally | Authenticates the request so Kong Gateway can resolve a Consumer. Without an authenticated Consumer, there’s no customer to enforce against. |
| Meter | Metering & Billing | Counts raw API requests. This is the usage the entitlement limit is measured against. |
| Metered feature | Metering & Billing |
Makes the meter’s usage enforceable. Its key is what you set as the plugin’s feature.key.
|
| Plan with a metered entitlement, published | Metering & Billing | Defines the allowance, for example 5 requests per month, on a rate card that references the feature. |
| Customer with a matching subject key | Metering & Billing |
The entity whose access is enforced. Its usage_attribution.subject_keys must contain the Consumer’s subject key, or usage and enforcement won’t resolve to this customer.
|
| Subscription to the plan | Metering & Billing | Materializes the entitlement onto the customer. Until the subscription starts, the customer has no entitlement to enforce. |
| Metering & Billing plugin, with the Ingest token |
example-service
|
Reports usage events. Nothing counts against the limit unless usage is reported, and allow_status_codes keeps failed responses out of that usage.
|
| Entitlement Enforcement plugin, with the Entitlement Access token |
example-route
|
Reads the customer’s entitlement for the configured feature and blocks the request when they’re over the limit. |
The following diagram shows how those pieces relate:
flowchart TB
client(["Client (API key)"])
subgraph gateway["Kong Gateway"]
route["example-route"]
service["example-service"]
consumer1["Consumer (Kong Air)"]
enforcement["Entitlement Enforcement plugin"]
metering["Metering & Billing plugin"]
end
redis[("Redis
enforcement cache")]
subgraph mb["Konnect Metering & Billing"]
events["Events API"]
access["Entitlement Access API"]
meter["Meter"]
subgraph plan["Premium Plan"]
feature2["Metered feature + entitlement (limit)"]
end
subgraph subscription["Premium Subscription"]
customer1["Customer (Kong Air)"]
end
end
client -->|request| route
route -.->|enforced by| enforcement
route -->|allowed| service
service -.->|metered by| metering
client -.->|authenticates as| consumer1
consumer1 -->|subject key| customer1
metering -->|usage events, Ingest token| events
events -->|aggregated by| meter
meter -->|referenced by| feature2
subscription -->|activates| plan
enforcement -->|queries, Entitlement Access token| access
access -->|reads entitlement| customer1
enforcement <-->|cached state| redis
feature2 -.->|feature.key| enforcement