Skip to content

Entitlements

An entitlement is a business quota: “100 AI calls per month”, “5 exports per day”, “10 seats per team”. The check and the charge happen in one atomic statement, so concurrent requests can never go over the quota, and a charge rolls back with the transaction that made it. It is different from API rate limits (requests per second): entitlements are durable and belong to a user or a tenant.

Function Does
auth.entitled(key, n := 1, scope := 'auto') Charges n and returns true if used + n <= quota; otherwise returns false and charges nothing. No quota row, or no signed-in identity, returns false.
auth.entitlement_remaining(key, scope := 'auto') Units left (never below 0), or NULL when there is no row. Does not charge.

Because the auth schema is not exposed through the Data API, signed-in users read the same number with public.base_entitlement_remaining(p_key, p_scope := 'auto') (base.entitlements.remaining).

scope is user (the JWT sub), tenant (the JWT claim tenant_id) or auto (the user’s own row if it exists, otherwise the tenant’s shared row). Both functions are callable by authenticated, not by anon.

select public.base_set_entitlement('ai.calls', 100, 'month', p_user_id => '<uuid>');
select public.base_set_entitlement('seats', 10, 'none', p_tenant_id => 'acme');
select public.base_reset_entitlement('ai.calls', p_user_id => '<uuid>'); -- used = 0
select public.base_delete_entitlement('ai.calls', p_user_id => '<uuid>');

The same functions are available as POST /rest/v1/rpc/base_set_entitlement and so on with a secret key. period is none, hour, day or month (UTC boundaries); used returns to 0 on the first charge after a boundary, with no job needed. Changing only the quota keeps used; changing the period resets it. Read quotas with GET /rest/v1/base_entitlements?user_id=eq.<uuid> and a secret key. End users cannot read or change them.

The dashboard (Authentication > Entitlements), a personal access token with scope auth:admin and the management SDK use the Management API at /v1/projects/{ref}/entitlements. It lists with cursor pagination, grants (POST, 409 if the quota row exists), updates (PATCH), resets usage and revokes. Every change is validated, rate limited to 120 writes per project per minute, and recorded in the project audit log with the values before and after.

import { createManagement } from "@potalab/base-management"
const app = createManagement({ apiUrl, token }).project("my-app")
await app.entitlements.grant({ key: "ai.calls", quota: 100, period: "month", user_id })
await app.entitlements.update("ai.calls", { user_id }, { quota: 200 })
for await (const e of app.entitlements.iterate({ key: "ai.calls" })) console.log(e.user_id, e.used, e.quota)
await app.entitlements.reset("ai.calls", { user_id })
await app.entitlements.revoke("ai.calls", { user_id })

base.entitlements wraps the calls above. Reading what is left works for a signed-in user; changing quotas needs a secret key, so do it on your server.

// in the browser or the app: the signed-in user
const { data: left } = await base.entitlements.remaining("ai.calls") // number, or null when there is no quota row
const { data: seats } = await base.entitlements.remaining("seats", { scope: "tenant" })
// on your server, with a secret key
await base.entitlements.set("ai.calls", { userId }, { quota: 100, period: "month" })
await base.entitlements.set("seats", { tenantId: "acme" }, { quota: 10 })
const { data: row } = await base.entitlements.get("ai.calls", { userId }) // { quota, used, remaining, period, reset_at, ... } or null
const { data: rows } = await base.entitlements.list({ key: "ai.calls", limit: 50 })
await base.entitlements.reset("ai.calls", { userId }) // used = 0
await base.entitlements.remove("ai.calls", { userId })

Every call resolves to { data, error } and never throws. A bad key, subject, quota or period is rejected before any request (error.code is entitlement_invalid). There is no entitled() in the SDK on purpose: the charge must happen inside the database, in the same transaction as the work it pays for (see the next two sections).

create function public.generate_report() returns jsonb
language plpgsql as $$
begin
if not auth.entitled('reports') then
raise exception 'quota exceeded' using errcode = 'P0001';
end if;
-- do the work; if it raises, the charge is rolled back too
return '{}'::jsonb;
end $$;
const { error } = await base.rpc("generate_report")
create policy exports_insert on public.exports for insert to authenticated
with check (user_id = (select auth.uid()) and auth.entitled('exports')); -- charges 1 per inserted row

Under REPEATABLE READ a contended charge fails with 40001; retry the transaction. At the default READ COMMITTED level it waits for the other charge and checks again.

  • Charges to the same user or tenant and key run one after another (they lock one row). That suits business quotas, not per-request counting at very high rates.
  • Periods follow UTC; there is no per-tenant time zone.
  • There is no history of single charges and no low-quota alert. Keep your own log table if you need them.
  • There is no dashboard screen for entitlements yet: use the SDK, SQL, RPC or the Data API as above.