Skip to main content
Use NextJS with 4Mica when your app needs paid API routes, paid server actions, or server-side calls to paid APIs. For route protection, run NextJS behind a Node server entry and mount the 4Mica Express middleware before the Next handler.
This integration is not stable yet. For now, use the NodeJS quick start for the supported implementation.

Prerequisites

  • NextJS running in the Node runtime.
  • A custom server entry such as server.ts.
  • A seller wallet address for payTo.
  • A buyer wallet with deposited 4Mica collateral for paid requests.

Install

Create wallet

For wallet setup, see Wallet.
Use the private key only for buyer-side signing. Use PAY_TO as the seller address advertised by protected routes.

Deposit collateral

Deposit collateral for the buyer wallet on the same network your NextJS server advertises. Start with Base Sepolia (eip155:84532) and a small test price. To learn more about collateral setup, see Deposits and Withdrawals.

Protect a route

Create a Node server entry and mount the payment middleware before nextRequestHandler.

Support payment

Use the wrapped client anywhere you run trusted server-side code, such as a route handler, server action, or background job.
Then call the protected route from server code: