Tutorial: Warn Users About Low Credits
In this tutorial, you’ll display a user’s remaining credits and surface a warning placement when the balance runs low — at 80% consumed — so users top up before they’re blocked. Takes about 15 minutes.
What You’ll Build
Section titled “What You’ll Build”A credits flow that:
- Reports credit consumption to the SDK with
update() - Reads the remaining balance with
useEntitlement() - Fires a low-credits warning placement when consumption crosses 80%
Prerequisites
Section titled “Prerequisites”- A React 18+ application with
@revturbine/sdkinstalled - A
RevTurbineProviderset up (see Quickstart)
-
Add a credits entitlement and a low-credits placement
A
creditsentitlement with a per-plan allowance, plus ausage_credit_seatplacement triggered bycredit_thresholdat 80%. This is a complete, validPlaybook.revturbine.playbook.json {"artifact_type": "playbook","format_version": "1.0.0","plans": [{ "unique_handle": "free", "name": "Free", "tier_position": 0, "sort_order": 0 },{ "unique_handle": "pro", "name": "Pro", "tier_position": 1, "sort_order": 1 }],"entitlements": [{ "unique_handle": "credits", "name": "Render Credits", "type": "credits", "unit": "credits" }],"entitlement_rules": [{"id": "er_credits_free","entitlement_id": "credits","targets": [{ "kind": "plan", "id": "free" }],"segment_ids": [],"kind": "credits","allowance_value": 20,"rollover_enabled": false,"max_balance": null}],"segments": [],"content_ui_paths": [],"surface_templates": [{ "id": "banner_placement", "surface_type": "banner", "fields": [] }],"placements": [{"id": "pl_credits_low","name": "Low credits warning","category": "usage_credit_seat","trigger": { "type": "credit_threshold", "entitlement_handle": "credits", "threshold_percent": 80 },"payloads": [{"id": "pl_credits_low_p0","target": { "plan_ids": ["free"], "segment_chips": [] },"surfaces": [{"template_id": "banner_placement","fields": {"header": "{{usage_remaining}} credits left","body": "Top up to keep rendering.","dismissible": "Yes"},"ctas": [{ "label": "Buy credits", "path": "open_checkout", "config": { "purchase": "credit_pack" } }]}],"caps": { "max_per_period": { "count": 1, "period": "day" } },"status": "active"}],"order": 0}]} -
Report credit usage as it’s consumed
Each time a user spends credits, report the new running total. This re-evaluates the warning’s threshold.
src/hooks/useRender.ts import { useRevTurbine } from '@revturbine/sdk';import { useCallback, useRef } from 'react';export function useRender() {const { sdk } = useRevTurbine();const spent = useRef(0);return useCallback((cost: number) => {spent.current += cost;sdk?.update({ usage: { credits: spent.current } });}, [sdk]);} -
Show the remaining balance
Read the live balance with
useUsageSnapshot— thecreditsentry reflects everyupdate()call.src/components/CreditBadge.tsx import { useUsageSnapshot } from '@revturbine/sdk';export function CreditBadge() {const { usage } = useUsageSnapshot();const credits = usage['credits']; // { current, limit? }if (!credits) return null;const remaining =credits.limit != null ? Math.max(0, credits.limit - credits.current) : undefined;const low = credits.limit != null && credits.current / credits.limit >= 0.8;return (<span style={{ color: low ? '#b45309' : 'inherit' }}>{remaining} / {credits.limit} credits</span>);}limitedflips totruewhen the balance is at or past the allowance — not as it approaches one. There is no “approaching” state: to warn before the cap, comparecurrentagainstlimityourself (the placement below triggers at 80%). And readallowedbefore granting access — under the default enforcement alimitedresult isallowed: false. -
Render the warning slot
Place the slot where the banner should appear. It stays hidden until consumption crosses 80%, then renders the warning with the balance interpolated into the copy via
{{usage_remaining}}.src/components/AppHeader.tsx import { Slot } from '@revturbine/sdk';export function AppHeader() {return (<header><Slot id="app_header_banner" surfaceTemplateIds={['banner_placement']} /><nav>{/* ... */}</nav></header>);}
How It Works
Section titled “How It Works”update()updates the SDK’s credit balance and re-evaluates placements.- The
credit_thresholdtrigger keeps the placement hidden until 80% of the allowance is consumed. - Token expansion fills
{{usage_remaining}}from the live balance. - The CTA opens your top-up flow through the resolvers from Quickstart.
- To sell the top-up itself, model it as a credit pack: an add-on whose credits rule puts the pack size in
one_time_grant. If the pack comes in several sizes, the CTA’spurchasenames the variation of the size it sells. See Credit grants and credit packs.
Try It Live
Section titled “Try It Live”Next Steps
Section titled “Next Steps”- Entitlements Guide — credits, usage limits, and the
limitedstate - Tutorial: Track Usage and Show a Quota Meter — the usage-limit equivalent
- Component Gallery — every built-in slot component