Tenant entitlements: freshness, ordering and notifications
This page is the contract between TenancyEngine and your backend for what a customer tenant has paid for: how to read it, how long you may cache it, what happens when billing changes, and what to do when TenancyEngine is unreachable. Contract schema version: 1.
It covers entitlement (which offering a tenant is on, and which billing-controlled features and limits that grants). It does not cover who is calling your API (machine credentials) or usage you bill for. Those are separate contracts.
Giving a new tenant its first offering
A new tenant has no offering. Until it has one, billing-controlled features are off and metered calls fail with No active entitlement grant for this metered feature. There are three ways to start one:
Free offering, from your backend. Pass
offeringSlugwhen you create the tenant with atenants.writekey. The offering must be active, belong to the same application and environment as the tenant, and cost 0 in every region (a country price above 0 makes it paid). If the free offering has a billing product code, the tenant is registered in billing on that free plan and nothing else.httpPOST /api/v1/runtime/applications/{applicationId}/tenants {"name":"Acme Treasury","slug":"acme","environment":"Production","offeringSlug":"free"}With the .NET SDK:
new CreateTenantRequest("Acme Treasury", "acme", Environment: "Production") { OfferingSlug = "free" }.Paid offering. Start a checkout for the tenant. When the payment completes, billing activates the offering. An API key can never start a paid plan on its own; asking for one at tenant creation returns
400and no tenant is created.By hand. In the Console, open Tenants, choose Edit on the tenant, open the Entitlements tab and click Assign plan (requires the billing-grant permission).
Where the snapshot comes from
Call the runtime context with a tenant id:
GET https://api.saasruntime.com/api/v1/runtime/applications/{applicationSlug}/context?environment=Production&tenantId={tenantId}With the .NET SDK (TenancyEngine.Sdk 2.2.0 or later):
var context = await client.GetRuntimeContextAsync("bankweave", "Production", tenantId, ct);
var snapshot = context?.Entitlement;The response carries features and limits already resolved for the tenant, plus an entitlement block:
| Field | Meaning |
|---|---|
schemaVersion | 1. Treat any other value as unusable. |
applicationId, tenantId, environment | What the snapshot is about. |
entitlementId, offeringSlug | The offering in force, or null when there is none. |
status | Active, Trial, or None (nothing in force: no purchase, cancelled, suspended for non-payment, or expired). |
effectiveFromUtc, effectiveToUtc | The window of the offering in force. The start is inclusive and the end is exclusive. |
revision | A counter that increases by one on every entitlement change for the tenant. It does not change when time alone moves a tenant across a boundary. |
fetchedAtUtc, validUntilUtc | When the snapshot was built and the last moment you may use it, on the platform's clock. |
validForSeconds | validUntilUtc − fetchedAtUtc. Expire the snapshot this many seconds after you received it, on your own clock. |
maxStaleSeconds | The platform's cache bound (default 60, never above 300). |
nextChangeAtUtc | The next scheduled boundary (trial end, period-end downgrade or cancellation), if any. |
entitlement is null when you did not pass a tenantId, or when the tenant does not belong to this application. The platform never tells you whether that tenant exists in another application.
A Development or Staging offering never counts in a Production context: a Production call for a tenant whose only offering is a sandbox one reports None.
Why a feature is on or off
The runtime context also carries featureDecisions, keyed by feature key, for every active feature. It never changes features: enabled here is true exactly when the key is in features. Use it to pick the message to show a customer.
| Field | Meaning |
|---|---|
enabled | The feature is on for this tenant. |
allowed | enabled and, for a metered feature, allowance left in this period. |
reason | One value from the table below. |
source | Where the feature comes from: plan, trial, grant (a time-limited grant you gave this tenant, more generous than its plan), override (switched on for this tenant by you), rollout (your app-wide rollout), or none when it is off. |
limit, used, resetsAtUtc | For an enabled metered feature: the cap, what is used, and when the period resets. Absent otherwise. |
reason | Suggested message |
|---|---|
allowed | Show the feature. |
limit_reached | "You have used all of your X this period. Upgrade or wait until resetsAtUtc." Call the upgrade-options endpoint for plans that raise the limit. |
no_active_subscription | "Choose a plan to use this feature." |
plan_lacks_feature | "This feature is not part of your plan." Offer the upgrade options. |
tenant_inactive | "This account is not active. Contact your administrator." |
no_tenant_context | You called the context without a tenant id. Pass one to get an answer for a customer. |
rollout_not_reached | "This feature is not available to you yet." |
targeting_excluded | Same message as above; the feature is limited to other plans or a share of customers. |
not_enabled | The feature is off and no more specific reason applies. |
Older clients ignore the new block. New values may be added to reason; treat an unknown value like not_enabled.
How long you may use it
validUntilUtc is the earliest of fetchedAtUtc + maxStaleSeconds, nextChangeAtUtc and effectiveToUtc. A cached decision can therefore never outlive a trial end or a scheduled plan change.
Your server's clock and the platform's may differ. Measure the lifetime from when you received the snapshot (validForSeconds), not by comparing validUntilUtc with your own clock. The .NET SDK does this for you: GetRuntimeContextAsync records when the snapshot arrived, and IsUsableAt expires it UsableFor later (never more than maxStaleSeconds, and never more than 300 seconds).
- Before
validUntilUtc: use the cached snapshot. You do not need to call TenancyEngine per request. - After
validUntilUtc: fetch the context again before granting a billing-controlled feature. - If that fetch fails (timeout, 5xx, network): fail closed for billing-controlled features. Return
503to your caller with a retry hint. Do not extend the old snapshot. Features that are not billing-controlled are not part of this rule.
if (snapshot is null || !snapshot.IsUsableAt(DateTimeOffset.UtcNow))
{
// refetch; on failure deny billing-controlled access with 503
}IsUsableAt also returns false for a schema version the SDK does not know, so an older SDK fails closed instead of misreading a newer contract.
Notifications
Every change to a tenant's entitlement publishes entitlement.changed to the webhook endpoints of the application that owns the tenant (never to another application in the same organization). Subscribe to it under Applications → Webhooks. The payload:
{
"schemaVersion": 1,
"applicationId": "…", "tenantId": "…", "tenantSlug": "acme",
"entitlementId": "…", "offeringSlug": "standard", "status": "Active",
"effectiveFromUtc": "…", "effectiveToUtc": null,
"changedOfferingSlug": "premium", "changedStatus": "Active",
"changedEffectiveFromUtc": "…", "changedEffectiveToUtc": null,
"environment": "Production",
"nextChangeAtUtc": "…", "revision": 42,
"source": "tenabill_sync", "factOccurredAtUtc": "…", "occurredAtUtc": "…"
}The first group of fields is the tenant's state in force after the change; status is None when nothing is in force. environment is the tenant's environment, so a receiver that serves several environments can ignore events that are not its own. factOccurredAtUtc is when the billing fact happened (null for Console and signup changes); occurredAtUtc is when the platform published the event. The notification is sent only after the change is saved. The changed… fields describe the row the change created or ended, which can be a scheduled one. source is tenabill_sync for billing events and assignment for a plan set in the Console, at signup or at checkout.
Treat the notification as a cache-invalidation hint: when its revision is higher than the one you cached, drop the cache and refetch. Do not use the payload as your source of access. A notification can arrive late, twice, or not at all. If it is lost, the validUntilUtc bound still makes you refetch within maxStaleSeconds. Time-based transitions (a trial reaching its end, a scheduled change starting) send no notification, because validUntilUtc already ends at that boundary.
Billing transitions
TenaBill owns subscription and payment facts. TenancyEngine projects them onto the tenant's entitlement:
| Billing fact | Access |
|---|---|
| Subscription activated | Offering features from now |
| Trial with an end date | Features until the end date, None after |
| Upgrade | New features immediately |
| Downgrade at period end | Current features until the boundary, lower plan after |
| Immediate cancellation | None now |
| Cancellation at period end | Features until the boundary, None after |
| Non-payment (suspended) | None immediately, even if a later date was supplied |
| Payment recovered | Features restored |
| Same event delivered twice | No change |
| An older event arriving after a newer one | No change (acknowledged as stale) |
A product code is resolved only among offerings in the tenant's own environment. A Production tenant can only receive a Production offering; a Development or Staging offering that carries the code is never used, and an event whose code exists only there is rejected.
Billing events are ordered by when the fact happened. An event that is older than the newest fact already applied to the tenant (including a cancellation that had nothing left to end) is acknowledged and changes nothing. A fact time more than five minutes ahead of the platform's clock is rejected, so one bad clock cannot freeze a tenant. A trial or window whose end has already passed when the event arrives grants nothing.
Known limitation. If a cancellation is scheduled for the end of a period and billing then sends an immediate active event for the same plan (for example a renewal notice), the scheduled end is cleared. Billing must not send same-plan active events after a period-end cancellation unless the customer reactivated.
Purchases are per application. If you sell two products (for example BankWeave and Financial Center), each has its own tenants, offerings and subscriptions. The same customer business on both products is two tenants, and a purchase in one never grants anything in the other.
What entitlement does not do
- It does not authenticate the caller. Establish who is calling first, then check entitlement for that caller's tenant.
- It does not grant access to the customer's own data or third-party consent (for example bank consent). Your application enforces resource ownership and consent independently.
- It does not decide what happens to resources above a new limit after a downgrade. Whether to block new creates, allow a grace period or suspend specific resources is your application's policy. The platform never deletes your data as a billing side effect.
Related
- SDK reference
- Platform caching
- Linux API and worker example:
examples/linux-api-workerin the TenancyEngine repository