Prerequisites
- A Next.js 13+ application with the App Router
- A utilsio account with credentials (you can get them at https://utilsio.dev/creator/apps):
-
NEXT_PUBLIC_UTILSIO_APP_ID- Your public app identifier -
UTILSIO_APP_SECRET- Your private app secret (backend only) -
UTILSIO_APP_SALT- Your app salt (backend only) -
NEXT_PUBLIC_UTILSIO_APP_URL- The utilsio service URL (default:https://utilsio.dev) -
NEXT_PUBLIC_APP_URL- Your application’s public URL (e.g.,http://localhost:3001),NEXT_PUBLIC_APP_URLis a reference for your app; changing this won’t change where your app is hosted
-
Option 1: Clone the Template (5-10 minutes)
Clone the pre-configured template with everything set up and working.Option 2: Manual Setup (15-20 minutes)
Follow these steps to integrate utilsio into an existing Next.js application.Step 1: Install the SDK
Install the utilsio React SDK package:Step 2: Set Up Environment Variables
Create a.env.local file in your project root with your utilsio credentials:
.env.local
Variables prefixed with
NEXT_PUBLIC_ are accessible in the browser. Never put secrets in these variables. Your UTILSIO_APP_SECRET and UTILSIO_APP_SALT must ONLY be used on the backend.Step 3: Create Server Action and Safari Callback
3a. Create the signing server action
Createsrc/app/actions.ts:
src/app/actions.ts
3b. Create the Safari-compatible callback endpoint
For Safari and browsers that block third-party cookies in iframes, createsrc/app/api/signature-callback/route.ts:
src/app/api/signature-callback/route.ts
Safari Compatibility: Safari blocks third-party cookies in iframes, preventing the SDK from reading
deviceId. This callback endpoint allows utilsio.dev (running in first-party context where it CAN read cookies) to request a signature from your server. The SDK automatically uses this fallback flow when deviceId is unavailable. The additionalData parameter works for both subscribe (amountPerDay) and cancel (sorted list containing userId and subscriptionIds) flows.Step 4: Set Up UtilsioProvider in Your Layout
TheUtilsioProvider initializes the SDK and makes utilsio functionality available to your components. It creates a hidden iframe that handles authentication securely via postMessage.
Create or update src/app/layout.tsx:
src/app/layout.tsx
Server Actions: The
getAuthHeadersAction is a server action that generates signatures server-side. This keeps your app secret secure and works seamlessly with the SDK.Step 5: Create Your First Page
Now createsrc/app/page.tsx to use the SDK:
src/app/page.tsx
UtilsioProvideris now inlayout.tsxand wraps your entire apppage.tsxcomponents can directly useuseUtilsio()without wrapping in the providergetAuthHeadersActionis called automatically when the SDK needs to authenticateuseUtilsio()provides access to user state, subscription data, and actions
Step 5: Add Success and Cancelled Pages
Createsrc/app/success/page.tsx for successful subscriptions:
src/app/success/page.tsx
src/app/cancelled/page.tsx for cancelled subscriptions:
src/app/cancelled/page.tsx
Step 6: Run Your App
Option 3: Use an AI Agent (5-15 minutes)
See instructions here: Using AI Agents →Resources
- Learn the SDK: Client SDK Reference →
- Understand security: Server SDK Reference →
- Explore the API: API Reference →
- Clone the template: github.com/utilsio/templates