Guide

Check what a customer has paid for, and fail safely when you cannot

Every paid feature rests on one question: is this customer entitled to it right now? The answer comes from another system, so you need a rule for how long it stays valid and what to do when you cannot reach it.

6 min read · Last checked

In short

Key points

  • Ask for the customer’s runtime context once and use the features and limits it returns, instead of checking plan names in code.
  • Each snapshot says how long it may be used. Expire it on your own clock, counted from when you received it.
  • If a fresh fetch fails for a billing-controlled feature, deny with 503 and a retry hint. Do not extend the old answer.
  • Use the decision reason to tell the customer why, so a limit and a missing plan do not look the same.

Check features, not plan names

Code that says if plan equals pro breaks the first time you rename a plan or add a tier. Code that asks whether the customer has the reports feature does not. The runtime context returns the features and limits already resolved for the tenant, so your code never needs to know which plan produced them.

Fetch, cache, and expire

The context for a tenant includes an entitlement snapshot. It carries a lifetime in seconds, which the platform caps (the default is 60 seconds and it is never above 300). The earliest of that cap, the next scheduled change such as a trial end, and the end of the current offering decides when it expires, so a cached answer can never outlive a trial.

Measure the lifetime from the moment you received the snapshot, not by comparing the platform’s timestamps with your own clock. The .NET SDK records the arrival time for you, and its IsUsableAt method returns false for an expired snapshot and for a schema version it does not know.

Gate a request (.NET, TenancyEngine.Sdk 2.2.0 or later)
using TenancyEngine.Sdk;

static async Task<IResult?> DenyUnlessEntitledAsync(
    TenancyEngineClient te, string tenantId, string feature, CancellationToken ct)
{
    RuntimeContext? context;
    try
    {
        context = await te.GetRuntimeContextAsync("my-app", "Production", tenantId, ct);
    }
    catch (Exception ex) when (ex is HttpRequestException or TenancyEngineApiException or TaskCanceledException)
    {
        // Cannot confirm: fail closed for a billing-controlled feature, and tell the caller to retry.
        return Results.StatusCode(StatusCodes.Status503ServiceUnavailable);
    }

    var snapshot = context?.Entitlement;
    if (snapshot is null || !snapshot.IsUsableAt(DateTimeOffset.UtcNow))
        return Results.StatusCode(StatusCodes.Status503ServiceUnavailable);

    return context!.Features.Contains(feature) ? null : Results.StatusCode(StatusCodes.Status403Forbidden);
}

Fail closed, on purpose

When the fetch times out or the platform returns a server error, the safe default for something the customer pays for is to refuse and ask the caller to retry. Extending the previous answer means a customer whose subscription lapsed an hour ago keeps access for as long as the outage lasts. Features that are not billing-controlled are not part of this rule, so keep your free, core paths working.

Show the reason, not just a no

The context also carries a decision per active feature, with a reason. Typical values are allowed, limit reached, no active subscription, the plan lacks the feature, and tenant inactive. A message that says "You have used all of your exports this period" converts differently from one that says "Forbidden", and it saves a support ticket. Treat an unknown reason like not enabled, because new values can be added.

Hear about changes instead of polling

Every change to a tenant’s entitlement publishes an entitlement.changed webhook to the application that owns the tenant, with a revision counter that rises by one on each change. Use it to drop a cache early. It is not a replacement for the expiry rule: the webhook can arrive late or not at all, and the snapshot lifetime is the bound you can rely on.

Honest limits

What to know before you rely on this

  • A new tenant has no offering until you start one, so billing-controlled features are off until then.
  • A Development or Staging offering never counts in a Production context.
  • The built-in entitlement cache class is in the SDK source but not in the published 2.3.0 package.

Questions people ask

Can I cache for longer than the snapshot says?

No. A cached decision should never outlive its stated lifetime, which is never more than 300 seconds.

What if my users are on a flaky connection to the platform?

Return 503 with a retry hint for billing-controlled features and keep non-billing paths working.

Sources

Keep reading

Try it on the free plan

One app and five customers, no credit card required.

Request access More guides