@utilsio/react package provides a UtilsioProvider component and useUtilsio hook to integrate utilsio into your React application. This is the main interface for building subscription experiences.
UtilsioProvider
TheUtilsioProvider is the root component that manages the SDK state and provides context to your application. It should wrap your app’s root or the entire section that needs access to utilsio.
Always place UtilsioProvider as high in your component tree as possible. If you only wrap a part of your app, only the wrapped components will have access to
useUtilsio().Props
string
required
Your unique utilsio Application ID. This identifies your app to utilsio’s servers. You can find this in your utilsio dashboard.Example:
"550e8400-e29b-41d4-a716-446655440000"function
required
An async function that returns authentication headers for signed requests. This function is called automatically whenever the SDK needs to make a request to utilsio (fetching subscriptions, canceling, creating new ones).
See Server SDK Reference for implementation details of the signing endpoint.
Parameters
Parameters
ReactNode
required
Your application components. Everything wrapped by the provider can access
useUtilsio().string
default:"https://utilsio.dev"
The base URL for the utilsio API and embed page. This will be useful later as utilsio rollout sandbox environment.Example:
For now, keep the default value for this prop
"https://utilsio.dev"string
default:"window.location.origin"
Your app URL. This is used to establish secure connection with utilsio.Example:
"https://myapp.com"Complete Usage Example (Next.js 16 - App Router)
First, create a server action for signing:src/app/actions.ts
src/app/layout.tsx
useUtilsio Hook
TheuseUtilsio hook provides access to the current state and actions of the SDK. This is how you interact with utilsio from your components.
Basic Usage
State Props
UtilsioUser | null
The currently authenticated user (happy path), or
null if not logged in to utilsio.dev yet (normal path).string | null
A unique identifier for the current device/browser. This is generated automatically by the SDK and persisted in a cookie on the utilsio origin.
This ID is just for reference, DO NOT rely on this for critical authentication flows as it might change across browser sessions and data clearance.
UtilsioSubscription | null
The active subscription for the current user/device combination. Returns
null if there’s no active subscription.boolean
Whether the SDK is currently fetching initial state from the server. Starts as
true while the SDK initializes, becomes false once ready.string | null
An error message if something went wrong during initialization or while performing actions.Common errors:
"Failed to authenticate"- Signing endpoint is down or unreachable"Network error"- Connection issues with utilsio servers"Invalid credentials"- App ID or signing is incorrect"User must be authenticated to cancel subscription"- User is not logged in"Either deviceId or appUrl is required to cancel subscription"- Safari users must provide appUrl
Actions
function
Manually refresh the user state and subscription info from the server. Useful after making changes or periodically polling for updates.
When to use:
Parameters
Parameters
This function does not accept any parameters
Return Values
Return Values
This function does not return any values
- After subscribing to / cancelling a subscription
- Periodically in long-running apps
- When returning from external authentication flows
- When you suspect subscription state is stale
- Or just simply every time page reload - it’s a simple function
This function:
- Sets
loadingtotruewhile operating - Returns early if deviceId is null (no user authenticated)
- Updates
currentSubscriptionwith the response
function
Cancel one or more subscriptions. For Safari compatibility, provide
When to use:
appUrl to enable server-side signature generation.Parameters
Parameters
string[]
required
Array of subscription IDs to cancel (typically just one)
string
Your app’s URL (e.g.,
"https://yourdomain.com"). Required for Safari users where deviceId is not available due to cookie blocking. When provided, the SDK will use a server-side callback flow to generate signatures.Return Values
Return Values
This function does not return any values
- After subscribing to / cancelling a subscription
- Periodically in long-running apps
- When returning from external authentication flows
- When you suspect subscription state is stale
- Or just simply every time page reload - it’s a simple function
After cancellation completes,
currentSubscription becomes null.function
Cancel one or more subscriptions.
Parameters
Parameters
string
required
Your utilsio app ID (same as in UtilsioProvider).
string
required
Display name of your app shown during the subscription flow.Example:
"My Premium App"string
required
The daily subscription amount as a string. This is the billing amount per day that will be charged to the user.
The amount is in USD/day unit, so passing
1 would mean that the user will be charged 1 USD/day or 30 USD/monthstring
URL to your app’s logo. Displayed during the subscription flow.
string
Your app’s URL. Used for redirects and context.
string
required
URL to redirect the user to after successful subscription. Typically your success page or home page.Example:
"https://myapp.com/success"string
required
URL to redirect the user to if they cancel the subscription flow. Typically back to your home page.Example:
"https://myapp.com/cancelled"Return Values
Return Values
This function does not return any values
Complete Usage Example (Next.js 16 - App Router)
Here’s a fully functional subscription component using all the pieces:src/components/SubscriptionWidget.tsx