How to implement usage based entitlements

To implement usage-based entitlements such as API tokens, credits, or other metered resources, you can use the gater entitlement API to assign a consumable entitlement (e.g. tokens) with a specific usage limit.

Use the /set endpoint to define how many tokens a user has available, and call /increment each time tokens are consumed.

You can optionally define a reset interval when setting the entitlement (e.g. "month"), or manage token top-ups manually.

Before processing a request that consumes tokens, you can use the /check endpoint to confirm that the user has enough tokens remaining.

const gater = axios.create({
  baseURL: "https://api.gater.dev",
  headers: { "X-Api-Key": process.env.GATER_SECRET },
});
 
await axios.post("/set", {
  user: "user_123",
  feature: "tokens",
  quota: 10000, // or no quota for unlimited tokens where the first 10000 are free
  reset: "month",
});
 
// user consumes tokens...
const response = await gater.post("/increment", {
  user: "user_123",
  feature: "tokens",
  amount: 1200,
});
 
if (response.data.ok) {
  const { usage, quota } = response.data;
  console.log(`Tokens consumed. Usage: ${usage}/${quota}`); // 1200/10000
} else {
  console.log("Not enough tokens available. Please upgrade the plan."); // e.g. 9000/10000
}