@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:
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.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:
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
amountPerDayto bind the signature to that amount - For subscription cancellation: pass comma-separated subscription IDs
- For basic requests: omit this parameter
src/utilsio/sign.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.buildSignatureMessage()
Builds the message that gets signed. Useful for debugging or implementing custom signing logic.
Parameters:
Device identifier
Your app ID
Unix timestamp in seconds
Additional data to include in message
Basic Signing Endpoint
app/api/sign/route.ts