NPM Package: @flowglad/react
Overview
The React SDK provides components, hooks, and context for managing billing and subscriptions in your React applications. It works with any React framework and requires a backend server running the Flowglad Server SDK.Installation
Note: This package requires
@flowglad/server to be set up on your backend. See the Server tab or Server SDK documentation for setup instructions.Quick Start
1. Wrap Your App with FlowgladProvider
2. Use the useBilling Hook
Key Features
- Type-safe Hooks: Access billing data with full TypeScript support
- React Context: Global state management for billing information
- Feature Access Control: Check user access to features
- Checkout Sessions: Create and manage payment flows
- Subscription Management: Handle subscription lifecycles
API Reference
FlowgladProvider
The main provider component that wraps your application. See FlowgladProvider.tsx for more detailsProps
Example
useBilling Hook
Access billing data and functions throughout your application.Load State Helpers
The hook surfaces billing lifecycle metadata you can use to gate UI:loaded: boolean–trueonce billing data has settled (success or error).loadBilling: boolean– Mirrors the provider prop; iffalse, billing is intentionally skipped.errors: Error[] | null– Populated when the last fetch failed.reload: () => Promise<void>– Invalidates and refetches billing data.
Return Value
Example Usage
createCheckoutSession
Create a new checkout session for subscriptions or one-time payments. Checkout sessions for specific prices can be made using eitherpriceSlug (an identifier that you define), or priceId (an identifier Flowglad defines) as a parameter.
priceSlug is recommended, so that you don’t need to store any references to Flowglad ids in your application database or environment variables.
Parameters
Example
createAddPaymentMethodCheckoutSession
Open a Flowglad-hosted flow that collects and stores a new payment method for the signed-in customer.Parameters
Example
createActivateSubscriptionCheckoutSession
Trigger the activation flow for an existing subscription that needs a payment method before billing can start. Activations can happen for subscriptions on trial, or subscriptions where payment is due but has not yet been completed.Parameters
Example
cancelSubscription
Cancel a subscription owned by the current customer and refresh billing data.Parameters
Example
checkFeatureAccess
Check if the current customer has access to a specific feature.Parameters
featureSlug: string- The slug of the feature to check
Returns
boolean-trueif the customer has access,falseotherwise
Example
checkUsageBalance
Get the remaining balance for a usage meter tied to the customer’s subscriptions.Parameters
usageMeterSlug: string– Slug of the usage meter to inspect.options.subscriptionId?: string– Limit the check to a specific subscription (optional).
Returns
{ availableBalance: number } | null
Example
getProduct
Look up a specific product from the catalog that ships with billing data.Parameters
productSlug: string
Returns
Product | null
Example
getPrice
Look up a price by slug from the live catalog.Parameters
priceSlug: string
Returns
Price | null
Example
Other billing records
The billing payload also exposes higher-level billing records you can use to build richer UI:customer– The Flowglad customer record (including email, name, IDs).subscriptions– All subscriptions (active and past) returned for the customer.purchases– Historical purchases for the customer.invoices– Invoice records (including status and totals).currentSubscriptions– Subscriptions currently active or pending.paymentMethods– Saved payment methods for the customer.billingPortalUrl– Deep link to Flowglad’s hosted billing portal (if enabled).pricingModel– Resolved pricing model associated with the customer’s plan.
Common Patterns
Feature Gating Component
Subscription Status Display
Custom Upgrade Button
Server Integration
This package requires a backend server running the Flowglad Server SDK. The provider automatically makes requests to/api/flowglad by default.
Make sure you have set up the server routes:
- See Next.js SDK for Next.js setup
- See Express SDK for Express setup
- See Server SDK for custom integrations