Skip to content

Plan and usage widget ​

Let your customers see their own plan, limits and usage inside your product, so "why am I blocked?" answers itself. The widget shows the plan name and price, one bar per metered feature (used of limit, what is left, the reset date), a 30 day usage trend, and an Upgrade button that opens your own upgrade page.

It shows exactly what the runtime context endpoint returns for that tenant: the same meters, limits and usage, in the same order. There is no second source of truth to drift.

How it works ​

  1. Your backend asks TenancyEngine for a widget token for one tenant. The token is read-only, valid for that tenant only, and expires after 15 minutes.
  2. Your page renders the widget with that token, either the React component or a hosted iframe.
  3. The widget loads the tenant's plan and usage with the token. Nothing else can be read with it, and no cookies are involved.

Mint a fresh token for each page view. Do not store it or log it.

1. Mint a token (your backend) ​

The call needs an organization API key with the tenants.read scope.

http
POST /api/v1/runtime/applications/{app}/tenants/{tenant}/usage-widget/token?environment=Production
X-TenancyEngine-Api-Key: te_...
Content-Type: application/json

{ "upgradeUrl": "https://my-app.example/billing/upgrade" }
json
{
  "token": "CfDJ8...",
  "expiresAtUtc": "2026-10-17T12:15:00Z",
  "expiresInSeconds": 900,
  "embedUrl": "https://auth.example.com/api/v1/public/usage-widget/frame#token=CfDJ8...",
  "dataUrl": "https://auth.example.com/api/v1/public/usage-widget/data"
}

With the .NET SDK (2.5.0 and later):

csharp
var widget = await client.CreateUsageWidgetTokenAsync("my-app", "acme", "Production", "https://my-app.example/billing/upgrade");

The upgrade button ​

upgradeUrl is your own page, because you decide how a customer buys. It must be an https URL (plain http is accepted only for localhost) with no credentials and no #fragment. For every plan priced above the tenant's current plan (up to three), the widget shows Upgrade to {plan} that opens your page with offering={planSlug}&tenant={tenantSlug} appended, so your page can start the right checkout. Leave upgradeUrl out and the widget shows no upgrade button.

2. Show it ​

React ​

tsx
import { PlanUsageWidget } from '@tenancyengine/react';
import '@tenancyengine/react/styles.css';

<PlanUsageWidget token={widget.token} dataUrl={widget.dataUrl} onExpired={refreshToken} />

It uses your --te-* theme tokens, so it matches your product's colours and follows your dark mode. Pass onUpgrade to run your own in-app flow instead of following the link. For server rendering, fetch the data yourself and pass it as data.

Hosted iframe ​

tsx
import { PlanUsageFrame } from '@tenancyengine/react';

<PlanUsageFrame src={widget.embedUrl} />

or plain HTML: <iframe src="{embedUrl}" title="Plan and usage"></iframe>. The frame sizes itself to its content (the React component listens for its height message), follows the visitor's light or dark setting, and uses your tenant's brand colour and logo when set. Add &theme=dark or &theme=light after the token in the URL fragment to force a theme. The token sits in the URL fragment, so it is never sent to a server or written to a log.

Good to know ​

  • Works at 200% browser zoom: layout wraps and every size is in rem.
  • The widget never changes anything. It cannot consume, adjust or reset usage.
  • If the token expires the widget says so; call onExpired (React) or reload the frame with a new token.
  • Usage history shows the end-of-day value for the last 30 days.

TenancyEngine platform documentation