Skip to main content

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_URL is a reference for your app; changing this won’t change where your app is hosted
Then, you have 3 options to get started. Pick the one that works best for you:

Option 1: Clone the Template (5-10 minutes)

Clone the pre-configured template with everything set up and working.
This is your best resource. Use the template code alongside this documentation to understand how everything works together.

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

Create src/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, create src/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

The UtilsioProvider 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 create src/app/page.tsx to use the SDK:
src/app/page.tsx
Key points:
  • UtilsioProvider is now in layout.tsx and wraps your entire app
  • page.tsx components can directly use useUtilsio() without wrapping in the provider
  • getAuthHeadersAction is called automatically when the SDK needs to authenticate
  • useUtilsio() provides access to user state, subscription data, and actions
Safari & Privacy Extensions: The user object from useUtilsio() may be null even when users are logged into utilsio.dev. This happens in browsers with strict third-party cookie blocking (Safari, Brave) or privacy extensions.This is expected behavior. Don’t block your UI if user is null - just show your subscribe button anyway. When users click subscribe, they’ll be redirected to utilsio.dev which will handle authentication automatically. The user object is provided for convenience (e.g., displaying user email), not as a gate to functionality.

Step 5: Add Success and Cancelled Pages

Create src/app/success/page.tsx for successful subscriptions:
src/app/success/page.tsx
Create 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

Last modified on March 5, 2026