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.
Live demo
GitHub template
What you get
| Surface | Route | After payment | Refresh |
|---|---|---|---|
| Pure API | GET /api/quote | JSON only, no cookie | Pay again |
| Fullstack page | GET /article + GET /api/article/unlock | HMAC cookie | RSC renders paid HTML |
Also included:
- Same-origin config proxy
GET /api/transx402/config(the browser never hits facilitator CORS) - Canonical
serversettlement — MetaMask signs; Route Handlers callPOST /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_...oripk_live_...) - Visitors: MetaMask (EIP-1193) with IDRX on the matching network
Clone and run
Open http://localhost:3421. Try Paid JSON (/quote) and Paid article (/article).
Deploy on Vercel from the same repo: vercel.com/new.
Environment
| Variable | Where | Purpose |
|---|---|---|
TRANSX402_API_KEY | Server | ipk_sandbox_* or ipk_live_* |
MERCHANT_WALLET | Server | payTo — dashboard payout wallet |
PRICE_IDR | Server | Whole IDR (default 5000) |
ACCESS_TOKEN_SECRET | Server | HMAC secret for page cookies |
ACCESS_TTL_SECONDS | Server | Optional; default 7 days |
FACILITATOR_URL | Server | Optional local Docker (http://localhost:3402) |
AGENT_PRIVATE_KEY | Smoke | Optional; 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 ifFACILITATOR_URLis 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.
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 settlegetAccess/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:
- Pick a
resourceId(cookie name becomestx402_access_<resourceId>) - Unlock Route Handler settles, then
setAccessCookie - Page RSC calls
getAccess(resourceId)and branches teaser vs paid HTML - Use
UnlockPanelwithmode="cookie"sorouter.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
Go live
- Swap
TRANSX402_API_KEYtoipk_live_... - Confirm
MERCHANT_WALLETis the production payout wallet - Set a strong
ACCESS_TOKEN_SECRET(not the sandbox placeholder) - Clear
FACILITATOR_URLif 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.
Related
- Quickstart — register, keys, first sandbox payment
- Server Library —
@transx402/server(processGate, config proxy) - JavaScript Library —
createBrowserClient - Sandbox — faucet, CAMP vs Anvil