Offerwall Postback in Node.js, Express and Next.js
How do you handle an offerwall postback in Node.js? Expose one GET route that reads the transaction ID, user ID, reward, status and hash as plain strings, recomputes the HMAC-SHA256 of tx:user:reward:status with your postback secret key, and compares it with crypto.timingSafeEqual after a length check.
If it matches, apply the event inside a database transaction that locks one row per transaction ID: add the reward on status 1, take it back on status 2 only if you credited it, and skip repeats. Answer 200 for everything you accepted or ignored, 403 for a bad hash, and 500 when your own code fails, so the call is retried.
This guide gives you that as one small module you can share between an Express app and a Next.js App Router project, plus the signed offerwall link. It assumes you know why each check exists. If you do not, read Postback security first, and What is a postback URL? for the basics. The PHP version is in Offerwall PHP integration: Laravel and plain PHP .
Who does what
| Part | Does | Never does |
|---|---|---|
| Browser or app | Shows the signed link it got from your server | Holds the link hash salt or the postback secret key, adds currency |
| Your Node.js server | Signs the link, verifies postbacks, owns the wallet table | Trusts a balance change that did not come from a verified postback |
| Moonetix | Shows offers, tracks results, sends a signed postback for every credit and every reversal | Pays your users directly |
You need three values: the app ID in your offerwall link and the link hash salt from your offerwall’s settings page, and the postback secret key from the Postback page. The salt signs links and the key signs postbacks. They are different values, and mixing them up is the most common reason for a Link not valid page or a 403 on every postback.
Step 1: one shared module
Both frameworks call the same file, so the security logic exists once. It uses node:crypto and the pg client for PostgreSQL. Store the keys in environment variables ( MOONETIX_APP_ID , MOONETIX_LINK_SALT , MOONETIX_POSTBACK_KEY , DATABASE_URL ) and keep the .env file out of your repository.
First the table. One row per transaction ID, with the last status you applied:
CREATE TABLE moonetix_tx ( transaction_id text PRIMARY KEY , user_id text NOT NULL , reward numeric NOT NULL , last_status smallint , updated_at timestamptz NOT NULL DEFAULT now ( ) ) ;
Then the module. The examples credit a coins column of type numeric on your users table:
// lib/moonetix.js (ES module)
import crypto from 'node:crypto' ; import pg from 'pg' ; const pool = new pg . Pool ( { connectionString : process . env . DATABASE_URL } ) ; const KEYS = [ 'tx' , 'user' , 'reward' , 'status' , 'hash' ] ; function env ( name ) { const value = process . env [ name ] ; if ( ! value ) throw new Error ( `${name} is not set` ) ; return value ; } export function wallUrl ( userId ) { const id = String ( userId ) ; const hash = crypto . createHmac ( 'sha256' , env ( 'MOONETIX_LINK_SALT' ) ) . update ( id ) . digest ( 'hex' ) ; return `https://wall.moonetix.com/${env('MOONETIX_APP_ID')}?user_id=${encodeURIComponent(id)}&hash=${hash}` ; } export function readParams ( get ) { const p = { } ; for ( const key of KEYS ) { const value = get ( key ) ; if ( typeof value !== 'string' ) return null ; p [ key ] = value ; } return p ; } function validHash ( p ) { const expected = Buffer . from ( crypto . createHmac ( 'sha256' , env ( 'MOONETIX_POSTBACK_KEY' ) ) . update ( `${p.tx}:${p.user}:${p.reward}:${p.status}` ) . digest ( 'hex' ) ) ; const given = Buffer . from ( p . hash ) ; return given . length === expected . length && crypto . timingSafeEqual ( given , expected ) ; } async function applyEvent ( p ) { const status = Number ( p . status ) ; const client = await pool . connect ( ) ; try { await client . query ( 'BEGIN' ) ; await client . query ( 'INSERT INTO moonetix_tx (transaction_id, user_id, reward) VALUES ($1, $2, $3) ON CONFLICT (transaction_id) DO NOTHING' , [ p . tx , p . user , p . reward ] ) ; const { rows } = await client . query ( 'SELECT user_id, reward, last_status FROM moonetix_tx WHERE transaction_id = $1 FOR UPDATE' , [ p . tx ] ) ; const row = rows [ 0 ] ; if ( status === 1 && row . last_status === null ) { const r = await client . query ( 'UPDATE users SET coins = coins + $1::numeric WHERE id = $2' , [ p . reward , p . user ] ) ; if ( r . rowCount === 0 ) console . warn ( 'moonetix: unknown user' , p . user , p . tx ) ; await client . query ( 'UPDATE moonetix_tx SET last_status = 1, updated_at = now() WHERE transaction_id = $1' , [ p . tx ] ) ; } else if ( status === 2 && row . last_status === 1 ) { await client . query ( 'UPDATE users SET coins = coins - $1 WHERE id = $2' , [ row . reward , row . user_id ] ) ; await client . query ( 'UPDATE moonetix_tx SET last_status = 2, updated_at = now() WHERE transaction_id = $1' , [ p . tx ] ) ; } else if ( status === 2 && row . last_status === null ) { await client . query ( 'UPDATE moonetix_tx SET last_status = 2, updated_at = now() WHERE transaction_id = $1' , [ p . tx ] ) ; } await client . query ( 'COMMIT' ) ; } catch ( err ) { await client . query ( 'ROLLBACK' ) ; throw err ; } finally { client . release ( ) ; } } export async function handlePostback ( p ) { if ( ! p || ! validHash ( p ) ) return 403 ; if ( p . tx . startsWith ( 'test_' ) ) { console . info ( 'moonetix: test postback' , p . tx , p . status ) ; return 200 ; } await applyEvent ( p ) ; return 200 ; }
Why it is written this way
One string per parameter. readParams takes a getter, so each framework decides how to read the query, and anything that is not exactly one string (missing, or an array from a repeated parameter) is rejected with 403 before hashing.
Byte length, not string length. The Node.js docs say timingSafeEqual throws if the two inputs have different byte lengths. A hash with non-ASCII characters can have the right number of characters and still a different number of bytes, so the check compares the two Buffers, and a forged hash gets a 403 instead of an exception.
Values hashed unchanged. The reward goes into the HMAC exactly as it arrived. Convert nothing before the check.
No floats. The reward goes from the query string to PostgreSQL as text and is added as numeric . Parsing it with Number() first would push a decimal reward through binary floating point for no reason.
One client for the whole transaction. The node-postgres docs say you must use the same client instance for every statement in a transaction and must not use pool.query for it. That is why the code calls pool.connect() and releases the client in finally .
The row lock does the idempotency. ON CONFLICT DO NOTHING creates the row on the first event, and FOR UPDATE makes a second copy of the same event wait until the first commits. It then sees last_status already set and changes nothing.
Reversals use the stored values. Status 2 takes back the reward saved with the credit, from the user it went to. A reversal for a transaction you never credited is recorded as 2, so a late copy of that credit, for example one you resend by hand, cannot pay afterwards.
Lazy key checks. env() throws when a key is missing, so a server without its key answers 500 and the event is retried, instead of hashing with an empty secret. Reading the keys inside the functions, not at import time, also keeps next build from failing on a build machine that has no secrets.
Test postbacks sent from the dashboard always have a transaction ID that starts with test_ and offer_id 0. The module checks their hash and logs them without crediting anyone. Remove that branch if you prefer to credit a test user and take the coins back by hand.
Step 2: the Express route
With the module in place, the Express side is two short routes: one that hands a signed link to your logged-in front end, and one that receives postbacks.
// server.js (Express 5, "type": "module" in package.json)
import express from 'express' ; import { wallUrl , readParams , handlePostback } from './lib/moonetix.js' ; const app = express ( ) ; app . get ( '/api/offerwall-link' , requireLogin , ( req , res ) => { res . set ( 'Cache-Control' , 'private, no-store' ) ; res . json ( { url : wallUrl ( req . user . id ) } ) ; } ) ; app . get ( '/moonetix/postback' , async ( req , res ) => { const status = await handlePostback ( readParams ( ( key ) => req . query [ key ] ) ) ; res . sendStatus ( status ) ; } ) ; app . listen ( 3000 ) ;
requireLogin stands for your own session middleware. Your React, Vue or mobile front end calls /api/offerwall-link , gets the URL, and puts it in an iframe or a WebView. no-store stops any cache from handing one user’s signed link to another.
The Express 5 migration guide says rejected promises in handlers are now passed to the error handler as if you called next(err) , and Express’s default error handler answers 500. So a database error during handlePostback becomes a 500, and Moonetix retries the event. On Express 4 you need a try / catch that sends 500 yourself.
The same guide says Express 5 changed the default query parser from “extended” to “simple”. With either one, ?tx=a&tx=b gives you an array, which is why readParams checks the type.
Keep the postback route outside anything built for browsers: login checks, CSRF middleware, per-IP rate limiters. Every postback comes from Moonetix’s servers, so a per-IP limit can block a busy day’s worth of genuine calls. The hash is what protects the route.
Set the postback URL on the Postback page of your dashboard with the macros the module reads, using the exact final address:
https://yoursite.com/moonetix/postback?tx={transaction_id}&user={user_id}&reward={reward}&status={status}&hash={hash}
You can add more macros for your logs, such as {event_id} , {offer_name} , {payout} , {sub1} or {integration} . Only the four signed values decide what you credit.
Step 3: the Next.js route handler
In the App Router, a route.ts file exports a function per HTTP method. The Next.js docs pass it a NextRequest and read query values with request.nextUrl.searchParams :
// app/api/moonetix/postback/route.ts
import type { NextRequest } from 'next/server' ; import { readParams , handlePostback } from '@/lib/moonetix' ; export const runtime = 'nodejs' ; export async function GET ( request : NextRequest ) { const sp = request . nextUrl . searchParams ; const params = readParams ( ( key ) => ( sp . getAll ( key ) . length === 1 ? sp . get ( key ) : undefined ) ) ; try { return new Response ( null , { status : await handlePostback ( params ) } ) ; } catch ( err ) { console . error ( 'moonetix postback failed' , err ) ; return new Response ( null , { status : 500 } ) ; } }
Notes specific to Next.js:
searchParams.get()returns only the first value of a repeated parameter, so the handler counts values withgetAll()and rejects anything that is not exactly one.- Pin the Node.js runtime. The module needs
node:cryptoand a TCP database connection.runtime = 'nodejs'is the default, and writing it down stops someone from switching the route to the Edge runtime later. - Caching. The Next.js version history says GET handlers changed from static to dynamic by default in version 15. If you are still on 14 or earlier, add
export const dynamic = 'force-dynamic'. - No redirects. By default Next.js redirects
/path/to/path, and withtrailingSlash: trueit does the opposite. Moonetix does not follow redirects, so register the path in the form your config serves, with no redirect in between. - Proxy (formerly middleware). In Next.js 16 the middleware file was renamed
proxy, and without a matcher it runs on every request. If it redirects signed-out visitors to a login page, exclude/api/moonetix/postbackfrom its matcher, or every postback gets a redirect.
The signed link in a Server Component
Pages in the App Router are Server Components by default, so they can sign the link directly. Next.js only puts environment variables that start with NEXT_PUBLIC_ into the browser bundle, so never give the salt that prefix.
// app/earn/page.tsx
import { redirect } from 'next/navigation' ; import { getCurrentUser } from '@/lib/auth' ; import { wallUrl } from '@/lib/moonetix' ; export default async function EarnPage ( ) { const user = await getCurrentUser ( ) ; if ( ! user ) redirect ( '/login' ) ; return ( < main > < h1 > Earn coins </ h1 > < iframe title = "Earn coins" src = { wallUrl ( user . id ) } style = { { border : 0 , width : '100%' , height : 'calc(100vh - 64px)' } } /> </ main > ) ; }
getCurrentUser stands for your auth library’s session lookup. When it reads the session cookie, Next.js renders the page per request, so every user gets their own link.
In a Next.js-only project, add import 'server-only' at the top of lib/moonetix.js , and the Next.js docs say importing it into a Client Component then fails at build time. Leave it out if Express shares the file: outside the Next.js server bundle, the package throws as soon as it is imported.
The iframe only loads on https pages of the website you registered for the app and its subdomains. If you send a Content Security Policy, allow https://wall.moonetix.com in frame-src . To open the wall in a pop-up from any page instead, see Add a floating Earn button to any website .
Express and Next.js side by side
| Concern | Express 5 | Next.js App Router |
|---|---|---|
| Postback route | app.get('/moonetix/postback', ...) |
app/api/moonetix/postback/route.ts exporting GET |
| Reading a value | req.query.tx |
request.nextUrl.searchParams.get('tx') |
| Repeated parameter | Becomes an array | get() returns the first value |
| Uncaught error | Default error handler answers 500 | Catch it and return 500 |
| Redirect traps | Your own http to https or www rules, reverse proxy | trailingSlash , proxy redirects |
| Where the link is signed | A JSON endpoint behind your login | A Server Component page |
Credits, reversals and retries
Every event of one result carries the same {transaction_id} , and there are only two statuses:
- Incoming status
- Last status in your table
- What the module does
- 1 (credited)
- none
- Adds the rewa
Comments
No comments yet. Start the discussion.