TransX402 Docs
Integrations

Next.js Starter

Clone-and-run Next.js App Router template for TransX402 — gated JSON APIs and paid pages with canonical server settlement.

The TransX402 Next.js starter is the recommended way to build with App Router. Clone it, add a sandbox key and merchant wallet, then extend the two included patterns instead of wiring @transx402/server from a blank app.

What you get

SurfaceRouteAfter paymentRefresh
Pure APIGET /api/quoteJSON only, no cookiePay again
Fullstack pageGET /article + GET /api/article/unlockHMAC cookieRSC renders paid HTML

Also included:

  • Same-origin config proxy GET /api/transx402/config (the browser never hits facilitator CORS)
  • Canonical server settlement — MetaMask signs; Route Handlers call POST /facilitate
  • Cursor skills in .cursor/skills/ for adding more gated APIs and pages

The monorepo example examples/next-paid-content is the minimal recipe (no cookies, no Tailwind). This starter is the opinionated product template you should build on.

Requirements

  • Node.js 20.19+
  • pnpm
  • A TransX402 account (dashboard.transx402.com) with a secret API key (ipk_sandbox_... or ipk_live_...)
  • Visitors: MetaMask (EIP-1193) with IDRX on the matching network

Clone and run

git clone https://github.com/campinvestment/transx402-next-starter-template.git
cd transx402-next-starter-template
pnpm install
cp .env.local.example .env.local
# edit TRANSX402_API_KEY, MERCHANT_WALLET, ACCESS_TOKEN_SECRET
pnpm dev

Open http://localhost:3421. Try Paid JSON (/quote) and Paid article (/article).

Deploy on Vercel from the same repo: vercel.com/new.

Environment

VariableWherePurpose
TRANSX402_API_KEYServeripk_sandbox_* or ipk_live_*
MERCHANT_WALLETServerpayTo — dashboard payout wallet
PRICE_IDRServerWhole IDR (default 5000)
ACCESS_TOKEN_SECRETServerHMAC secret for page cookies
ACCESS_TTL_SECONDSServerOptional; default 7 days
FACILITATOR_URLServerOptional local Docker (http://localhost:3402)
AGENT_PRIVATE_KEYSmokeOptional; Anvil account #2 default

Never put secret keys in NEXT_PUBLIC_*.

Key family selects the network. There is no CAMP/Base toggle in the template:

  • ipk_sandbox_* → sandbox config (CAMP testnet, or a local chain if FACILITATOR_URL is localhost)
  • ipk_live_* → production config (Base mainnet, real IDRX)

Chain id, IDRX, and Permit2 come from facilitator GET /config via the same-origin proxy. Do not hardcode them.

@transx402/client pulls @x402/*. The starter pins @x402/core (and siblings) to 2.18.0 so IDRX payments are not rejected by later spendControls defaults.

How it works

Settlement is always server: the browser signs; the Route Handler calls payments.processGate (which POSTs /facilitate). The browser never talks to /facilitate.

Browser                         Next                              Facilitator
  GET /api/transx402/config ──► proxy GET /config ───────────────►
  GET /api/quote ─────────────► 402 Payment Required
  MetaMask sign
  GET + PAYMENT-SIGNATURE ─────► processGate → POST /facilitate
  ◄── 200 JSON ───────────────

Paid pages add Set-Cookie: tx402_access_<resourceId> after unlock. The Server Component reads the HMAC cookie and renders HTML. /api/quote never sets that cookie.

Helpers live in lib/transx402/:

  • gateRequest / jsonFromUnsettledGate — 402 then settle
  • getAccess / setAccessCookie — HMAC access token (v1.<payload>.<sig>)
  • getClientEnvironment() — sandbox vs production from the key family

Extending

Copy the two patterns rather than inventing a third settlement mode.

New gated JSON API

Stateless: every request pays. Do not set or read tx402_access_* cookies. Copy app/api/quote/route.ts and createBrowserClient({ settlement: "server", configProxyPath: "/api/transx402" }).fetch("/api/your-route").

New paid page

Pay once, then the Server Component renders HTML from the cookie:

  1. Pick a resourceId (cookie name becomes tx402_access_<resourceId>)
  2. Unlock Route Handler settles, then setAccessCookie
  3. Page RSC calls getAccess(resourceId) and branches teaser vs paid HTML
  4. Use UnlockPanel with mode="cookie" so router.refresh() re-reads the cookie

Do not treat paid HTML in the JSON unlock response as the source of truth — the RSC must gate on the cookie.

Cursor skills in the repo (see AGENTS.md): transx402-setup, transx402-add-gated-api, transx402-add-gated-page, transx402-smoke-test.

Scripts

pnpm dev          # :3421
pnpm test         # HMAC access-token unit tests
pnpm type-check
pnpm smoke        # agent e2e (dev server must be running)

Go live

  1. Swap TRANSX402_API_KEY to ipk_live_...
  2. Confirm MERCHANT_WALLET is the production payout wallet
  3. Set a strong ACCESS_TOKEN_SECRET (not the sandbox placeholder)
  4. Clear FACILITATOR_URL if you used a local facilitator

The facilitator host stays https://api.transx402.com. You do not change environment in app code — the starter derives it from the key prefix.

Already have an App Router app?

You can still copy lib/transx402/ plus the config proxy into an existing project. Full APIs: Server Library and JavaScript Library.

On this page