@utilsio/react/server package provides cryptographic signing functions for your backend. These functions prove to utilsio that requests come from your authorized application, enabling secure Superfluid subscription operations without exposing your app secret to the client.
How It Works: The Signing Flow
Understanding the authentication flow helps you implement signing correctly. Here’s the complete flow:Why this is secure:
- Your
UTILSIO_APP_SECRETnever leaves your server - Client can’t forge requests without the secret
- Each request includes a fresh timestamp (prevents replay attacks)
- HMAC-SHA256 ensures integrity of the request
Core Functions
deriveAppHashHex()
Derives a deterministic hex string from your app secret using scrypt key derivation. This is the HMAC key used for signing requests.
Parameters
Parameters
Your app secret from the utilsio dashboard. This is sensitive and should never be exposed to the client.Keep this in
UTILSIO_APP_SECRET environment variable on your backend.A hex-encoded salt string for key derivation. This is application-specific and stored in your utilsio dashboard.Keep this in
UTILSIO_APP_SALT environment variable on your backend.Return Values
Return Values
A 64-character hex string (256 bits, 32 bytes)
Example
Example
src/utilsio/sign.ts
signRequest()
Creates an HMAC-SHA256 signature for a request. This signature is sent to utilsio to prove the request is authorized.
Parameters
Parameters
The derived app hash from
deriveAppHashHex(). This is the HMAC key.The unique device identifier from the client. Identifies which device is making the request.This comes from the
useUtilsio() hook’s deviceId property.Your utilsio App ID (public, safe to expose). This identifies your application to utilsio.
Unix timestamp in seconds when the request is signed. Use
nowUnixSeconds() to get the current time.This prevents replay attacks - signatures are only valid for a short time window.Optional additional data to include in the signature. Used to bind the signature to specific subscription operations.
When to use
When to use
- For subscription creation: pass
amountPerDay(in USD unit) to bind the signature to that amount - For subscription cancellation: pass comma-separated string containing
userIdand subscription IDs (sorted alphabetically) - For basic requests: omit this parameter
Return Values
Return Values
A 64-character hex string (HMAC-SHA256 digest)
Example
Example
app/actions.ts
nowUnixSeconds()
Returns the current Unix timestamp in seconds. This is a utility function for generating the timestamp parameter for signing.
Ensures consistent time reference across requests. Using
Date.now() directly could cause issues if you’re not dividing by 1000. Still, it’s up to you to use Date.now() or this function.Parameters
Parameters
This function does not accept any parameters
Return Values
Return Values
Current Unix timestamp as an integer (seconds, not milliseconds)
Example
Example
buildSignatureMessage()
Builds the message that gets signed. Useful for debugging or implementing custom signing logic.
Parameters
Parameters
Return Values
Return Values
The message string that will be signed with HMAC-SHA256
Example
Example
Server Action Example
Next.js Server Actions provide a simpler and more secure way to sign requests compared to API routes. The server action pattern keeps your secret secure while being easier to use.app/actions.ts
app/layout.tsx
Safari-Compatible Callback Endpoint
For Safari and browsers with strict third-party cookie blocking, you need a server-to-server callback endpoint that utilsio.dev can call to generate signatures.Why this is needed: Safari blocks third-party cookies in iframes, preventing the SDK from reading
deviceId from cookies. The callback endpoint allows utilsio.dev (running in first-party context) to request a signature from your server with the deviceId it can read.Handles both flows: This endpoint supports both subscription creation (where additionalData is amountPerDay) and cancellation (where additionalData is a sorted list containing userId and subscription IDs).app/api/signature-callback/route.ts
- Validates
X-utilsio-Originheader to ensure requests come from utilsio.dev - Requires HTTPS in production
- Validates timestamp is recent (within 60 seconds)
- Verifies appId matches your application