How it works
Your server mints a short-lived token for the signed-in customer. The widget uses that token and your publishable key to load the customer’s balance. When the customer adds credits, hosted Stripe Checkout takes the payment, and the widget refreshes the balance once payment completes. Your secret key never reaches the browser. Use@withlocus/credits-react in React or @withlocus/credits-js in any web app.
Mint a token on your server
Authenticated server route
renewUserToken callback to fetch a fresh one.
Add the widget
- React
- Plain JavaScript
Install the React package
Billing.tsx
topupUsd or topupCredits to change the amount.
Show transaction history
In the plain JavaScript widget, setshowPerCallPricing: true to show the customer’s five most recent charges, top-ups, allocations, and refunds. The history refreshes with the balance.
The React package does not render history yet. For React or other custom UI, call Own transaction history with the same publishable key and user token.
Render the provider catalog
Show customers which tools they can use and what they cost:Load the customer catalog
fetchCatalog loads every page of results for you. Results include provider names, logos, allowed endpoints, and final credit prices. Variable-price tools are marked as dynamic.
Build your own balance UI
Custom balance hook
Customization
Other options hide the purchase button, change its label, set Checkout return pages, and style the React container. The widget also supports checkout lifecycle callbacks, top-up metadata, and session binding.
Next steps
Node SDK
Mint tokens and charge usage from your server.
Funding & billing
Choose how customer balances are funded.