← All examples

Feature Entitlement

SaaS

Gate a feature behind plan tier and monthly usage limits.

Policy rule

A **request** is authorized
  if §request.plan_check is valid
  and §request.usage_check is valid.

request.plan_check. A **request** passes plan_check
  if the __plan__ of the **account** is in ["pro", "enterprise"].

request.usage_check. A **request** passes usage_check
  if the __api_calls_this_month__ of the **account** is less than 10000.

Input schema

{
"properties": {
"account": {
"properties": {
"plan": {
"type": "string"
},
"api_calls_this_month": {
"type": "number"
}
},
"required": [
"plan",
"api_calls_this_month"
],
"type": "object"
}
},
"required": [
"account"
],
"title": "Account Model",
"type": "object"
}

Test cases

Pro plan under limit

Expect pass
{
"account": {
"plan": "pro",
"api_calls_this_month": 2500
}
}

Free plan

Expect fail
{
"account": {
"plan": "free",
"api_calls_this_month": 100
}
}

Integration

Execute this policy from your app using one of the official SDKs.

import { ExecutionClient } from "@policies2/sdk";

const client = new ExecutionClient({
	apiKey: process.env.POLICY_API_KEY!,
	transport: { kind: "rest", baseUrl: "https://api.policy2.net" },
});

const result = await client.executePolicy({
	id: "your-policy-id", // replace with your published policy ID
	reference: "base",
	data: {
		"account": {
			"plan": "pro",
			"api_calls_this_month": 2500
		}
	},
});

if (result.result) {
	console.log("policy matched");
} else {
	console.log("policy did not match");
}

Ready to try this policy?

Open in editor